Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am writing an application - I want to pass a set of parameters to a method and run that method Asynchronously and Continually after calling it, While it is running I want the values it sees to be returned to the calling routine and I want to be able to do that continuously [via an event is fine.] So when the monitoring thread senses a change it will raise and event - my calling application / method will process the event. The Asynchronous method would continue to run in the background. So I would like some help with how to do this , best practice guidelines , .NET 4 or 4.5 is fine - the preference is dot net 4.0 . Any help guidelines and especially examples are very much appreciated. Thank you all so much in advance for your time and effort ..
Posted

There is nothing absolutely continuous and asynchronous at the same time. When you want to monitor a thread, you should synchronize your calling thread and another thread. The minimal synchronization facility can be a lock: http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx[^].

It would mean that threads sometimes wait for each other a bit, which is already non-continuous.

Now, if you want pull processing (monitoring is done periodically by the initiative of the calling thread, not a thread being monitoring), this solution is inherently ineffective. This is important thing to understand:
http://en.wikipedia.org/wiki/Pull_technology[^],
http://en.wikipedia.org/wiki/Push_technology[^].

Push technology is much better. In your case, it is implemented in the following way: a calling (monitoring) thread is subscribed to the thread to be monitored. Essentially, it adds an event handler to an invocation list of some event instance of the code related to the thread to be monitored. Then this thread invokes this event (or some of the set of such notification events), only when something is essentially changed in its state. For such code, it's important to have a thread wrapper. Please see my past answer showing such a wrapper with an events:
MultiThreading in C#[^].

See also my other answers on the topic:
Change parameters of thread (producer) after it is started[^],
How to pass ref parameter to the thread[^].

The example with the lock referenced above shows the pull approach. The lock is all you need for thread polling, but this is not a good way. If you poll it periodically, you waste performance. If you do it frequently, you do a lot of cycles in vain, when nothing really happens. In all cases, you miss some events, especially with non-frequent polling, and have some probability to miss them regardless of timing and priorities. Polling, push processing is bad.

There is one problem you have to address. The monitoring thread should responsively handle the events. For example, if it should show some monitoring results in UI, you cannot do it in the thread invoking the event, because it would be a thread to be monitored, not a UI thread. Hence, a UI thread invocation mechanism should be used. This is just one example of handling thread notification results. Please see my past answers on the problem:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

—SA
 
Share this answer
 
Comments
stixoffire 16-Apr-14 10:02am    
sergey thanks for your response - but I think you misinterpret what I was looking for, I am not monitoring a thread but a process using a separate thread - for example communication from a network it is continually listening for this data to come across and on a change of certain data an event will fire. This process must run continuously while the application continues other work. I am guessing that some chat programs out there might have an asynchronous communication or perhaps FTP so I am looking for more information.
Sergey Alexandrovich Kryukov 16-Apr-14 11:48am    
First of all, let me put things straight: that was not me who misinterpreted anything. That was you who did not even mention that "process", which means a big difference. Please, be very careful: your inaccuracy in questions cause people wasting a lot of time.

My enthusiasm to help you further is almost lost, but I'll give you just few ideas: to monitor a separate process, you need to understand that processes are well isolated and are executed in separate address spaces. To be able to monitor a process, you need to design some collaboration facility in the process to be monitored itself. You can implement the same subscription mechanism, but, for processes, you need to use, apparently, some IPC. It could be just communication via TcpListener/TcpClient, or something else (named pipes, remoting, WCF).

—SA
Encapsulate the parameters, the routine, and the event in a class.
Your main application creates an instance of it, subscribes the event, then starts the routine
Thread thread = new Thread(() => myObject.StartRoutine()); thread.Start();
Note that the event is fired in a different thread than the UI thread, hence you will have to call Invoke if you need to update the GUI here.
 
Share this answer
 
Comments
stixoffire 16-Apr-14 10:00am    
Your solution seems to be on the path that I am looking at. Since the thread that is monitoring another process i.e data arriving via a network. when a particular data point changes that thread fires an event, since the thread fired the event the event is considered on that thread. Now I wish to use that event to activate a different thread [process] updating a database, sending data across a network. So what you are saying is I do not need to use callbacks - I can simply call an asynchronous thread let it run to its hearts content, and everytime it has an event ; it will fire my event in the class that called the thread - and I can do other work.
Hi All - after seeing Sergey and Bernhards answers - I decided to do a bit more searching using the guidance provided; Thank you Bernhard for stating simply , Thank you Sergey for your list of gotchas and things to remember. The article which I found and is most excellent for me I found here: Asynchronous Method Invocation[^]
 
Share this answer
 

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