Executing a method in a given thread context



I'm porting win32 code (non visual component or "user control") to .NET.

In win32 code, I was using Windows messages between threads to signal events
form a worker thread to another thread. This was done to make sure an event
handler was executed in the context of the thread having created an instance
of the component class.

The [win32 non visual] component class use a hidden window to receive the
messages from the worker thread it created.

Now I'm porting this code to .NET using C# as language. I'm using the socket
class and use the asynchronous operation, for example, Socket.BeginAccept to
make the runtime use a thread to wait for incomming connections and call my
callback function when a connection is available. From the callback
function, I trigger an event. So far, so good. But the event is executed in
the context of the worker thread and it is bad since it make difficult to
use it to update the display. Winforms components are not thread safe. You
can't - for example - add a line to an edit control from a worker thread
without having to resort to complex invocation of InvokeRequired and Invoke.
But displaying something on the form is not the only problem I want to solve
(Don't talk me about InvokeRequired and Invoke, I know about them). I want
that my component user is able to run his event handlers in the context of
the thread which created my component (not necessary the UI thread).

I want to move all the burden of multithreading issues into the component
I'm writing instead of leting the developer using my component handle it. As
explained in the introduction, I solved this problem in Win32 using a custom
message posted to a hidden window. The question is: How to do that properly
using C# in the .NET environment ?

btw: For testing, I used the Windows API to create a hidden window and post
a message. It works, but there should be a better way doing that, not
relying on Windows native API and PInvoke.

Sorry for my long message.

Any help appreciated.

--

Francois PIETTE

http://www.overbyte.be




.



Relevant Pages

  • Re: Executing a method in a given thread context
    ... Create yourself a control on your worker thread. ... I was using Windows messages between threads to signal events ... without having to resort to complex invocation of InvokeRequired and Invoke. ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Closing modeless dialogs
    ... This changes the thread context back to that of the main thread. ... Currently I'm simply calling a member function of my dialog called SetText from the worker thread, ... In order to maintain the multiple windows "illusion" on the screen that Windows depends upon the dialog must promptly process messages that are sent by the operating system. ... Yes, SetText might happen to work because it is such a simple call, but you are treading in "not designed for this" waters, breaking the rules laid down by the MFC designers. ...
    (microsoft.public.vc.mfc)
  • Re: Problem with multithreaded C# app on Windows CE
    ... The exception is thrown elsewhere, probably in the worker thread itself. ... I'm having a problem running a C# multithreaded application on Windows CE ... Everything in the source code is supported by the .Net Compact Framework ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Cross Thread Calls
    ... > worker thread to put messages on a queue. ... In the same way windows' UI works. ... In windows both SendMessages and PostMessages do thread switching. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: thread exists spontaneously and freezes
    ... >and the following message appears in the output of the debugger when it ... >freezes (the thread is NOT a worker thread, ... it should communicate with other windows pretty ... includes calling functions such as SetWindowText which may use SendMessage ...
    (microsoft.public.vc.language)