Re: Executing a method in a given thread context



The only built-in way to force execution on a specific thread, is via
the 'Invoke' method of a control.

Internally this uses sends a windows message to the control's message
loop, thus crossing the thread boundary.

So, your options are:

1) Create yourself a control on your worker thread. Force it to run a
message loop via a call to Application.Run(). Then push the required
calls onto the thread by the caller calling .Invoke()

2) Do it some other way by coding yourself e.g. have your own message
queue managed by a mutex or something at both sides.


Either way, it's not pretty :-(


In article <#eh#A4suGHA.3496@xxxxxxxxxxxxxxxxxxxx>,
fpiette@xxxxxxxxxxxxxxxxx says...
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: NullReferenceException in UnsafeNativeMethods.PeekMessage and multithreading concepts in general
    ... this Invoke method but it haven't dawned on me till now. ... > This comes from Windows OS and because .NET controls use internaly Windows ... To handle those problems Control ... >> reason, ApplicationContext context) ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: CoBOL moved to OO
    ... of a list of file names' by using the Windows GUI and the keyboard alone ... > Alternatively write the collection elements to your text file with a Callback (see ... > invoke FileNames2 "new" returning os-Filenames ...
    (comp.lang.cobol)
  • Executing a method in a given thread context
    ... In win32 code, I was using Windows messages between threads to signal events ... messages from the worker thread it created. ... without having to resort to complex invocation of InvokeRequired and Invoke. ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Advanced Windows Form message priority / threading.
    ... updates if necessary. ... sending hundreds of Invoke or BeginInvoke messages ... > When my form loads I start two worker threads and these threads just loop> around and call Invoke on my main Form with a string to display. ... > I'm thinking that Inovke is basically like SendMessage so maybe somehow i> can replace the windows message loop to do the following, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: NullReferenceException in UnsafeNativeMethods.PeekMessage and multithreading concepts in general
    ... This comes from Windows OS and because .NET controls use internaly Windows ... To handle those problems Control ... control's Invoke method. ... "Stefan Kiryazov" wrote in message ...
    (microsoft.public.dotnet.framework.windowsforms)

Loading