Your code look very much flawed or you have not put here correctly. I took your second code which was there in comment did formatting as it lost formatting when i copied in visual Studio.
I noticed following points
1. stocksCollec = new Dictionary<string,>(); another type parameter missing.
Replaced with
stocksCollec = new Dictionary<string,stock>();
2. Shouldn't mktData.StockUpdateHandler += new StockUpdateEventHandler (mktData_StockUpdateHandler);
be the other way round.
I mean StockUpdateHandler should be Delegate name and StockUpdateEventHandler should be Event name.
but then its just naming convention issue.
3. Queue workQueue and List tempStocksCollec has type parameter missing
Updated to Queue<Stock> workQueue;
List<Stock> tempStocksCollec;
4. stocksCollec is Dictionary and tempStocksCollec is List. you can't add stackCollecs to tempStocksCollecs
unless you take only key or value from the dictionary. like what i did
tempStocksCollec.AddRange(stocksCollec.Values.ToList());
5. You are initializing MktDataSim object in the constructor of StocksManager and listening to event fired. Who is then firing
the event. I don't know about your requirement but looks like you should have MktDataSim as parameter in the Stock Manager constructor
Someone can correct me if i m wrong on this.
Anyway i modified code and now I can see
Symbol,Price and Size columns in the grid.
This is the code after modification.Hope it will help you bit or atleast will point you in right direction
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var stkMngr = new StocksManager();
dataGridView1.DataSource = stkMngr.StockCollection;;
}
}
public class StocksManager
{
int NUM_PROCESSORS = Environment.ProcessorCount;
MktDataSim mktData;
List<thread> workers;
EventWaitHandle signal;
Queue<stock> workQueue;
object sync;
Dictionary<string,stock> stocksCollec;
List<stock> tempStocksCollec;
public StocksManager()
{
mktData = new MktDataSim();
workers = new List<thread>();
signal = new AutoResetEvent(false);
workQueue = new Queue<stock>();
sync = new object();
stocksCollec = new Dictionary<string,stock>();
tempStocksCollec = new List<stock>();
mktData.StockUpdateHandler += new StockUpdateEventHandler (mktData_StockUpdateHandler);
for (int i = 0; i < NUM_PROCESSORS; i++)
{
workers.Add(new Thread(worker));
workers[i].IsBackground = true;
workers[i].Start();
}
}
public List<stock> StockCollection
{
get { return tempStocksCollec; }
}
private void mktData_StockUpdateHandler(string symbol, double price, int size)
{
getMktData(new Stock(symbol, price, size));
}
private void getMktData(Stock stock)
{
lock (sync) workQueue.Enqueue(stock);
signal.Set();
}
private void worker()
{
while (true)
{
Stock stock = null;
lock (sync)
{
if (workQueue.Count > 0)
stock = workQueue.Dequeue();
}
if (stock != null)
{
string sym = stock.Symbol;
double prc = stock.Price;
int sz = stock.Size;
tempStocksCollec.Clear();
if (!stocksCollec.ContainsKey(sym))
stocksCollec.Add(sym, new Stock(sym, prc, sz));
else
stocksCollec[sym].Price = prc; stocksCollec[sym].Size = sz;
tempStocksCollec.AddRange(stocksCollec.Values.ToList());
}
}
}
}
public delegate void StockUpdateEventHandler(string symbol, double price, int size);
public class MktDataSim
{
public event StockUpdateEventHandler StockUpdateHandler;
}
public class Stock : INotifyPropertyChanged
{
private string symbol;
private double price;
private int size;
public event PropertyChangedEventHandler PropertyChanged;
public Stock(string symbol, double price, int size)
{
this.symbol = symbol; this.price = price; this.size = size;
}
public string Symbol
{
get { return symbol; }
}
public double Price
{
get { return price; }
set
{
if (price != value)
{
price = value;
OnPropertyChanged("Price");
}
}
}
public int Size
{
get { return size; }
set
{
if (size != value)
{
size = value;
OnPropertyChanged("Size");
}
}
}
private void OnPropertyChanged(string propertyName)
{
if(PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}