Click here to Skip to main content
14,869,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Does anyone know how to call a method from within a class when a data message arrives in the class List?
For example, say we have a List, that receives data messages, France, Germany and Italy. As soon as the first message arrives in the List, a class method is called, say to print all the data messages to the console. Once the List is empty, the method ends and waits until it is called again.
I do not want to use a Sleep() iin the method. The data arrives from another component in the application.
Thank you for any help in advance.
Posted

1 solution

I'm not 100% clear on what you're wanting to do, but it sounds like your best bet is to wrap the methods of the List class to perform the logic you need.

In other words, create a new class and create an internal List class and implement the necessary methods you need by calling the internal List.

--------------------------------

Static does not prevent multiple executions, just means its not tied to a specific instance of a class. You'll want to read this[^].

You'll want to execute a method on the threadpool, but just have a critical section around a flag indicating that it is executing. You'll then check that every time you get a new message, if its not set execute the thread, otherwise sit tight and let the thread handle it.

Just make sure that if your thread fails or throws an exception that you unset that flag, so it won't clog up your system. Take care.
   
v2
Comments
picardy 19-Jul-10 14:31pm
   
I have a component, myComponent, that receives data messages from other components, those data messages from other components are sent to myComponent.List so that they can be processed. myComponent.ProcessMessages method runs continuously in a loop processing all data messages in myComponent.List until the list is empty then sleeps for 100ms then checks the contents of the List again.

I want to remove the Sleep and only have myComponent.ProcessMessages invoked when data arrives in the list then stop when the list is empty, then restart when new data messages arrive in the list.
Andrew Rissing 19-Jul-10 14:47pm
   
You could go a few routes. One would be to use the ThreadPool to kick off a new task each time (plus a little logic to make sure you're not spawning off more than one at a time). This would ultimately be the better if you're wanting to get away from doing a Thread.Sleep.

Another would be to use an object like the ManualResetEvent to wait for a signal from the event that adds to the list. But the ManualResetEvent will essentially do a Thread.Sleep internally...
picardy 19-Jul-10 15:19pm
   
How do I kick off a new task (using ThreadPool) each time a message arrives in the list? If I make the method static that should prevent more than one thread running (is this correct?).
The reason I don't want to use a Sleep(100) is because the List should empty as quickly as possible when data messages arrive.
Thanks for taking the tine to answer my question.
Andrew Rissing 19-Jul-10 17:12pm
   
Updated answer above...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900