Re: Executing a method in a given thread context
- From: "Francois PIETTE" <fpiette@xxxxxxxxxxxxxxxxx>
- Date: Tue, 8 Aug 2006 13:22:14 +0200
The only built-in way to force execution on a specific thread, is via
the 'Invoke' method of a control.
This is valid only for the UI thread, not any arbitrary thread.
1) Create
2) Do
I have a third option which I already experimented and which works
perfectly. I simply find not "clean" to call the Win32 API to create a
hidden Windows and PostMessage/GetMessage and the likes. IMO there should be
a way to do that using .NET framework. I looked at the source code for a lot
of Microsoft made assemblies and the all use win32 API, hidden windows and
the likes. Using Reflector (http://www.aisto.com/roeder/dotnet/) I can see
very interesting functions in Microsoft.Win32.UnsafeNativeMethods, for
example CreateWindowEx, PostMessage, GetMessage and many other win32 API
functions. Unfortunately, Microsoft.Win32.UnsafeNativeMethods is private so
it seems I can't call his methods.
--
Francois PIETTE
http://www.overbyte.be
"AJ" <noone@xxxxxxxxxxx> a écrit dans le message de
news:MPG.1f42785c4e68b21398968c@xxxxxxxxxxxxxxxxx
The only built-in way to force execution on a specific thread, is viaevents
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
eventform a worker thread to another thread. This was done to make sure an
instancehandler was executed in the context of the thread having created an
theof the component class.
The [win32 non visual] component class use a hidden window to receive
socketmessages from the worker thread it created.
Now I'm porting this code to .NET using C# as language. I'm using the
Socket.BeginAccept toclass and use the asynchronous operation, for example,
mymake the runtime use a thread to wait for incomming connections and call
incallback function when a connection is available. From the callback
function, I trigger an event. So far, so good. But the event is executed
tothe context of the worker thread and it is bad since it make difficult
Youuse it to update the display. Winforms components are not thread safe.
Invoke.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
solveBut displaying something on the form is not the only problem I want to
want(Don't talk me about InvokeRequired and Invoke, I know about them). I
ofthat my component user is able to run his event handlers in the context
componentthe thread which created my component (not necessary the UI thread).
I want to move all the burden of multithreading issues into the
it. AsI'm writing instead of leting the developer using my component handle
customexplained in the introduction, I solved this problem in Win32 using a
properlymessage posted to a hidden window. The question is: How to do that
postusing C# in the .NET environment ?
btw: For testing, I used the Windows API to create a hidden window and
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
.
- References:
- Executing a method in a given thread context
- From: Francois PIETTE
- Re: Executing a method in a given thread context
- From: AJ
- Executing a method in a given thread context
- Prev by Date: Re: Executing a method in a given thread context
- Next by Date: Re: Executing a method in a given thread context
- Previous by thread: Re: Executing a method in a given thread context
- Next by thread: Re: Executing a method in a given thread context
- Index(es):
Loading