Ian and I have had a correspondence on implementing ISynchronizeInvoke. I
finally got around to it.
You can download from www.idesign.net a generic implementation of
ISynchronizeInvoke, and use it as a starting point for your need.

Thanks,

Juval.


[EMAIL PROTECTED]
IDesign Inc.
.NET Design and
Business Solutions
http://www.idesign.net/

-----Original Message-----
From: dotnet discussion [mailto:[EMAIL PROTECTED]]On Behalf Of Ian
Griffiths
Sent: Monday, March 25, 2002 1:45 AM
To: [EMAIL PROTECTED]
Subject: Re: [DOTNET] Calling method across threads

Windows Forms supplies precisely this facility.  The implementation of
ISynchronizeInvoke provided by the Control class works exactly like this -
it delivers calls to the correct thread via its event loop.

There's nothing stopping you writing something similar that doesn't require
the creation of any UI stuff though. You could write your own implementation
of ISynchronizeInvoke which kept a Queue of method requests, and supplied
its own DoEvents method to be called periodically by the thread that is
going to run the methods.  So you would use it something like this:

private void MyMainThreadProc()
{
  do
  {
    // SyncInvoke is the class I'm suggesting you write
    SyncInvoke syncInv = new SyncInvoke (Thread.CurrentThread);

    ... do whatever your thread does ...

    ... At some point you'll need to make sure other threads have access
    to syncInv so that they can call Invoke or BeginInvoke on it...

    // Give SyncInvoke a chance to process any requests that must
    // be marshaled to this thread:
    syncInv.DoEvents ();
  } while (threadStillRunning);
}

SyncInvoke would implement ISynchronizeInvoke.  In the Invoke and
BeginInvoke methods you would just add the delegate and its parameter list
to some internal Queue.  In DoEvents you would drain this queue, and call
each of the delagates.  This is clearly conceptually equivalent to sending
stuff via an HWND.  You've replaced the Windows message queue with the Queue
inside this SyncInvoke class, and you've replaced the event loop calling
GetMessage,DispatchMessage with a look calling DoEvents on your SyncInvoke
object.

The only slightly tedious aspect of this is that you need to supply your own
implementation of IAsyncResult - the implementations in the framework don't
appear to be suitable for reuse in your own implementations.  But it's not
especially hard - something based around ManualResetEvent is probably fine.


But I guess what I'd really like to know is why you want to do this.  What
are you doing that requires this thread affinity?


--
Ian Griffiths
DevelopMentor

----- Original Message -----
From: "Erick Thompson" <[EMAIL PROTECTED]>


> I see what you mean about it being not a very nice thing to do. If I
control
> both threads, is there some way I post an async event to thread A from
> thread B, where thread A can get it whenever it can?
>
> In Windows (pre .net), I would create a hwnd for each thread and pass a
> message to thread A's hwnd, which it would check and handle in it's
winproc.
> In the winproc, I could then raise an event if needed. That way, I could
at
> least emulate a cross thread event. I can't figure out to do something
> similar in .net. Should I go back to using a message queue (which .net
> one?)?
>
> Erick
>
>
> ----- Original Message -----
> From: "Brown, Keith" <[EMAIL PROTECTED]>
>
>
> So you mean thread A is just kicking along through some code, and all of
> a sudden it's interrupted and jumps to your event handler? How would
> that work without you explicitly giving control of thread A back to the
> system (e.g. the way you do in Windows by calling GetMessage)?
>
> If you're willing to give up control on thread A, you can block on an
> Auto/ManualResetEvent and signal that event from thread B.
>
> OR
>
> You can call Thread.Interrupt to cause an exception to be thrown on
> thread A (not a very nice thing to do though, since you've no idea what
> state A is in).
>
> -----Original Message-----
> From: Erick Thompson [mailto:[EMAIL PROTECTED]]
>
> I'm completely stumped here. Is there any way to a call a
> method from thread B that will execute on thread A? I've
> looked into async delegates, with no luck. I've spent most
> of the day reading MSDN and google, with no results. I
> am trying to raise an event on thread A from thread B,
> which seems like a common enough problem. Any ideas?

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to