I’ve been reading a lot about asynchronous programming recently, as I need to create a multi-threaded application.
Unfortunately I can’t seem to bring my newly acquired knowledge together into one cohesive and useful unit!
I’m hoping someone can give me some pointers on how to construct the following:
-
I have a class that does a lot of different (and time-consuming) tasks in a specified sequence.
-
I’d like to instantiate this class in my Winforms UI thread. eg:
TaskRunner tr = new TaskRunner(); -
I’d like to be able to call a BeginAsync() method (as you can do with lots of the .NET built-in objects). eg:
tr.BeginAsync(); -
I’d like my class to callback to my UI thread when certain events arise (for logging, completion etc).
-
I’d like to be able to cancel the execution of my class. eg:
tr.CancelAsync();
How do I go about building the internals of that class? I can’t seem to find anything that talks about how the internals of SqlCommand or WebRequest might work.
For this operation, you want to use the event-based asynchronous pattern (as opposed to the IAsyncResult design pattern). For more information, see the section of the MSDN documentation titled ‘Event-based Asynchronous Pattern Overview’, located at:
http://msdn.microsoft.com/en-us/library/wewwczdw.aspx