Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

500pts: Not all events are arriving in my thread, please help

Posted on 2006-10-31
8
Medium Priority
?
158 Views
Last Modified: 2010-04-16
Hi there,

I wonder if anyone can help..

I have a thread which i start like this..

      _workerThread = new Thread(new ThreadStart(threadMarket.ProcessMarket));
                        _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
0
Comment
Question by:ianinspain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 17840598
Please show your code.
0
 

Author Comment

by:ianinspain
ID: 17840656
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.ProcessMarket));
                        _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.GetUser;
                              
                              Console.WriteLine("sub to : " + marketInstance + "/" + stockInstance );
                              
                        
                              CliCommon.Stock stock= new Stock (localmyUser,stockInstance , marketInstance );
                              _myStocks.Add(stockInstance ,stock);
                              stock.view.xml = MonitorForm.MainForm.GetXml;
                              stock.view.np = 1;


                              // Subscribe to the event for receiving
                              stock.statusChanged +=new StatusChanged(stock_statusChanged);
                              Thread.Sleep(5000);
                              stock.load();
                        }
                  }
            }


and the event


            private void stock_statusChanged(object sender)
            {
                  //System.Windows.Forms.Application.DoEvents();
                  Stock stock= sender as Stock;
                  if(stock.status == Stock.statusCode.LOADED)
                  {
                        stock.statusChanged -=new StatusChanged(stock_statusChanged);
                        

                              ///////////// NEVER ARRIVES HERE UNLESS I PUT A THREAD.SLEEP but even then some things are missing..

                  }
                  else
                  {
                        //Thread.Sleep(5000);
                  }
            }
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17840703
stock.statusChanged -=new StatusChanged(stock_statusChanged);

This line unsubscribes from statusChanged event. After this line stock_statusChanged function is not called. Is this your intention?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ianinspain
ID: 17840721
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
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17840857
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

0
 

Author Comment

by:ianinspain
ID: 17840923
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
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 2000 total points
ID: 17840989
BTW, does your class exist when event is raised? Maybe it is already released at this time. Adding Sleep can explain this - class remains alive for some time and its event handler is working.
Other version: adding Sleep to event handler actually keeps caller's raising event call. This can change Stock class behavior if Stock is some complicated class which has different threads, synchronization etc. Sleep changes timing of this class, this can be question to class developer.
0
 

Author Comment

by:ianinspain
ID: 17864876
Thanks AlexFM,

its sorted now.... it appears that the class (not mine) was using too many threads... it is now resolved...

thanks

ian
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question