- Implementing a Remote Server
- Defining Shared Code
- Handling Server Events on the Client
- Multithreading Caution
- Summary
Multithreading Caution
There are a lot of details with advanced subjects such as .NET Remoting. An additional consideration is that events raised by remote servers arrive on your client on their own thread (see Figure 2).
Figure 2 Note the number of threads created in this simple application.
In Figure 2, the client is running on thread 1172, General.Client.RaiseRemoteEvent, but when the event handler on the client is invoked it comes back on a different thread, 3512. Ignoring the multiple-threads issue works fine for console applications, but Windows Forms is not thread-safe. The upshot is that if your client has elements that are not thread-safe, such as Windows Forms, you'll need to marshal the data returned by the server back onto the calling thread. This is accomplished with the Invoke method and a delegate. For example, to marshal data onto a Windows Forms thread, use formname.Invoke, passing the address of a delegate that resides on the form itself. In our scenario, interactions with a Windows Form must occur on thread 1172, not thread 3512.