Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Threading Support in the .NET Framework : Page 2

To provide a great user experience applications need to do a lot of thingsmany of them seemingly at the same time. Professional applications do this using multi-threading. Until recently however, creating true multi-threaded applications wasn't a simple task and wasn't available to most developers. Thankfully, multi-threading is a fundamental feature of the .NET Framework and developers can write multi-threaded applications with relative ease, no matter whether they use managed C++ code, C#, VB.NET, or any other .NET language.


advertisement
Integrating with Secondary Threads
At this point you have a nice multi-threaded system that doesn't do much. The secondary thread runs for 10 seconds and goes away. Normally methods return some result you're interested in. However, this wouldn't make sense in this environment. Why would you spin up a new thread if you make the original thread wait for a result?

There are a number of ways to overcome this problem. One of them employs a technique called polling where the original thread goes on about its business until it absolutely needs the result created by the second thread. The second thread could put a return value into a property of the class and the first thread could simply run in a loop until the value has been set. The parents among you will intuitively know that this is not a very good approach, ("Are we there yet? Are we there yet? Are we there yet? No? How about now?"). We will not explore this approach further and instead investigate more elegant solutions.

A more elegant solution is to use what is known as a callback where you tell a thread to call a certain method whenever it is done with whatever it does. (You can also use callbacks to report on the progress of a method while the thread runs.)

In .NET you implement callbacks through the native Delegate mechanism (see sidebar). Delegates allow you to provide information about a certain method to another process so the process can dynamically call that method without having to know beforehand what method is to be called.

Let's say you want your secondary thread to call the following method whenever it''s done (VB.NET):

Public Sub ShowProgress(ByVal Text As String) MsgBox(Text) End Sub

In the first step towards passing this information to the second thread, create the associated Delegate:

Delegate Sub ShowProgressDelegate( _ ByVal Text As String)

You can see that a Delegate is basically a method definition with the same parameters and return values as the actual methods.

Here's the same construct in C#:


public void ShowProgress(string Text) { MessageBox.Show(Text); } delegate void ShowProgressDelegate(string Text);

We'll now instantiate this Delegate before we start the secondary thread, point it to the ShowProgress() method, and make it available for the secondary thread. The easiest way to do that is to add a new field to the class:

Private oCallback As ShowProgressDelegate

We'll change the DoSomething() method to create the Delegate before we start the secondary thread:

Public Sub DoSomething() Dim oThread As New Thread(AddressOf _ Me.DoSomethingSlow) Me.oCallback = New ShowProgressDelegate( _ AddressOf Me.ShowProgress) oThread.Start() MsgBox("Done!") End Sub

Now that the Delegate is available, the DoSomethingSlow() method can invoke it:

Public Sub DoSomethingSlow() Thread.Sleep(10000) Me.oCallback("Done") End Sub

This will call the ShowProgress() method, which will in turn show a message box. Here's the C# version of the field as well as the DoSomething() method:

private ShowProgressDelegate oCallback; public void DoSomething() { ThreadStart oTS = new ThreadStart(this.DoSomethingSlow); Thread oThread = new Thread(oTS); this.oCallback = new ShowProgressDelegate(this.ShowProgress); oThread.Start(); MessageBox.Show("Done!"); }

And this is how it gets called:

public void DoSomethingSlow() { Thread.Sleep(10000); this.oCallback("Done"); }

(See Sidebar: Fibers)


Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date