ianinspain
asked on
500pts: Not all events are arriving in my thread, please help
Hi there,
I wonder if anyone can help..
I have a thread which i start like this..
_workerThread = new Thread(new ThreadStart(threadMarket.P rocessMark et));
_workerThread.Name = market;
_threads.Add( market, _workerThread );
_workerThread.IsBackground = true;
_workerThread.Start();
But once in my thread i subscribe to some events on an object, generally what happens is the event arrives with some arguments saying "LOADING" and then later "LOADED" but i am finding that the event "LOADED" never arrives unless i do a Thread.Sleep (500) and even then not all the events are arriving... This doesn't happen if i subscribe to the events in the gui....
I can't do a InvokeRequired because ThreadMarket is a class...
I am really stuck ... does _workerThread not have enough threads or something....
I look forward to any inspiration or help
Thanks in advance
Ian
I wonder if anyone can help..
I have a thread which i start like this..
_workerThread = new Thread(new ThreadStart(threadMarket.P
_workerThread.Name = market;
_threads.Add( market, _workerThread );
_workerThread.IsBackground
_workerThread.Start();
But once in my thread i subscribe to some events on an object, generally what happens is the event arrives with some arguments saying "LOADING" and then later "LOADED" but i am finding that the event "LOADED" never arrives unless i do a Thread.Sleep (500) and even then not all the events are arriving... This doesn't happen if i subscribe to the events in the gui....
I can't do a InvokeRequired because ThreadMarket is a class...
I am really stuck ... does _workerThread not have enough threads or something....
I look forward to any inspiration or help
Thanks in advance
Ian
Please show your code.
ASKER
Hi alexfm,
Thanks... Ok yes of course... its quite simple.. the thing to remember is that it does work as far as the events do arrive with statusCode LOADING but never loaded unless i put the thread.sleep .... i think its something to do with threads... but a little unsure.
This is from the main GUI form ... i start the thread passing in
// This is just a class which i use to pass in a variable to a thread..
ThreadMarket threadMarket = new ThreadMarket(market);
_workerThread = new Thread(new ThreadStart(threadMarket.P rocessMark et));
_workerThread.Name = market;
_threads.Add( market, _workerThread );
_workerThread.IsBackground = true;
_workerThread.Start();
Then in the thread itself U(the class)... i have the following ....... _stcs is a hashtable which holds a number of stock items.
private void SubscribeToMarkets(string market)
{
// Iterate through all of the stocks
for (int j = 0; j < _stcs.Count; j++)
{
if ( (_stcs[j] as Stock).Market == market) // Do we have an stock in the current market?
{
// Yes - then lets process it
string stockInstance = (_stcs[j] as Stock).STC;
string marketInstance = (_stcs[j] as Stock).Market;
User localmyUser = MonitorForm.MainForm.GetUs er;
Console.WriteLine("sub to : " + marketInstance + "/" + stockInstance );
CliCommon.Stock stock= new Stock (localmyUser,stockInstance , marketInstance );
_myStocks.Add(stockInstanc e ,stock);
stock.view.xml = MonitorForm.MainForm.GetXm l;
stock.view.np = 1;
// Subscribe to the event for receiving
stock.statusChanged +=new StatusChanged(stock_status Changed);
Thread.Sleep(5000);
stock.load();
}
}
}
and the event
private void stock_statusChanged(object sender)
{
//System.Windows.Forms.App lication.D oEvents();
Stock stock= sender as Stock;
if(stock.status == Stock.statusCode.LOADED)
{
stock.statusChanged -=new StatusChanged(stock_status Changed);
///////////// NEVER ARRIVES HERE UNLESS I PUT A THREAD.SLEEP but even then some things are missing..
}
else
{
//Thread.Sleep(5000);
}
}
Thanks... Ok yes of course... its quite simple.. the thing to remember is that it does work as far as the events do arrive with statusCode LOADING but never loaded unless i put the thread.sleep .... i think its something to do with threads... but a little unsure.
This is from the main GUI form ... i start the thread passing in
// This is just a class which i use to pass in a variable to a thread..
ThreadMarket threadMarket = new ThreadMarket(market);
_workerThread = new Thread(new ThreadStart(threadMarket.P
_workerThread.Name = market;
_threads.Add( market, _workerThread );
_workerThread.IsBackground
_workerThread.Start();
Then in the thread itself U(the class)... i have the following ....... _stcs is a hashtable which holds a number of stock items.
private void SubscribeToMarkets(string market)
{
// Iterate through all of the stocks
for (int j = 0; j < _stcs.Count; j++)
{
if ( (_stcs[j] as Stock).Market == market) // Do we have an stock in the current market?
{
// Yes - then lets process it
string stockInstance = (_stcs[j] as Stock).STC;
string marketInstance = (_stcs[j] as Stock).Market;
User localmyUser = MonitorForm.MainForm.GetUs
Console.WriteLine("sub to : " + marketInstance + "/" + stockInstance );
CliCommon.Stock stock= new Stock (localmyUser,stockInstance
_myStocks.Add(stockInstanc
stock.view.xml = MonitorForm.MainForm.GetXm
stock.view.np = 1;
// Subscribe to the event for receiving
stock.statusChanged +=new StatusChanged(stock_status
Thread.Sleep(5000);
stock.load();
}
}
}
and the event
private void stock_statusChanged(object
{
//System.Windows.Forms.App
Stock stock= sender as Stock;
if(stock.status == Stock.statusCode.LOADED)
{
stock.statusChanged -=new StatusChanged(stock_status
///////////// NEVER ARRIVES HERE UNLESS I PUT A THREAD.SLEEP but even then some things are missing..
}
else
{
//Thread.Sleep(5000);
}
}
stock.statusChanged -=new StatusChanged(stock_status Changed);
This line unsubscribes from statusChanged event. After this line stock_statusChanged function is not called. Is this your intention?
This line unsubscribes from statusChanged event. After this line stock_statusChanged function is not called. Is this your intention?
ASKER
Yes ... because ... once its status is loaded.. i unsubscribe so that i don't receive more evetns for that stock..
It is in the IF statement when its loaded but without the Thread.sleep ... it never arrives...
Ian
It is in the IF statement when its loaded but without the Thread.sleep ... it never arrives...
Ian
What happens in the Stock class? Does it raise this event, what is thread context where this event is raised? Event is always raised in the context of thread where raising code is executed.
What is the purpose of this line:
Thread.Sleep(5000); // SubscribeToMarkets
What is the purpose of this line:
Thread.Sleep(5000); // SubscribeToMarkets
ASKER
Hi AlexFM... The stock component belongs to somebody else... i am only using it... but I have just been in contact with him... and he has informed me that we might be running out of threads in thread pool....
Be in contact shortly
Ian
Be in contact shortly
Ian
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks AlexFM,
its sorted now.... it appears that the class (not mine) was using too many threads... it is now resolved...
thanks
ian
its sorted now.... it appears that the class (not mine) was using too many threads... it is now resolved...
thanks
ian