Re: Cross Thread Calls

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Stoitcho Goutsev \(100\) [C# MVP] (100_at_100.com)
Date: 03/23/04


Date: Tue, 23 Mar 2004 10:50:54 -0500

Hi sharp,

> I have a console app that has a main thread and a worker thread. I
> would like for the main thread to be able to make an async call to the
> worker thread to put messages on a queue. The worker thread is also
> responsible for processing messages that are on the queue when it has
> free cycles.

I think you should give more details. What queue? I assume that it is not a
windows message queue as long as you are talking about console application,
but it could be.

> Is there anyway that the main thread can make direct calls to the
> worker thread?

Strictly speeking you cannot call a thread. However, you can let an object's
method to be executed by particular thread. To do that you should make the
code executed in the worker thread to work in event (message) driven
fashion. In the same way windows' UI works. Then you can wrap method calls
in messages and call a method in the worker thread by posting that message
in the worker thread's message queue.

Examples of where you can find that kind of thread switching:

1. In windows both SendMessages and PostMessages do thread switching. For
PostMessage it's obvious how the thread switching is done. It is not so
obvious for SendMessage, though. SendMessage calls dicrectly winprocs when
the window is created by the caller thread and no switching is necessary.
When the window is created by different thread, though, the winproc has to
be called in the context of the owner thread. That's why when a message is
send accross thread boundaries it goes thru the message queue.

2. Calling COM objects creatred in "Single-Threaded Apartment" (STA) from
different STA thread or from "Mutlithreaded Apartment" (MTA) thread. In this
case before entering the apartment thread has to be switched. The calls goes
thru registered Proxy/Stub and it uses windows messages for thread
switching. That's why STA threads have to have message loop running.

3. Windows Forms' Control.Invoke and Control.BeginInvoke methods use
PostMessage to switch the thread.

-- 
HTH
B\rgds
100 [C# MVP]


Relevant Pages

  • RE: Problems Printing After XP Upgrade from 98SE
    ... following steps to clean the printer subsystem completely on the ... Windows NT Fax Monitor ... AppleTalk Printing Devices ... | workstation, but if I do that, I'll have to create the queue and re-share ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: as400 print que on windows 2003
    ... If the firewall doesn't fix your problem, try using RPM Select from ... The culprit may be the Windows Firewall. ... configuring TCP print services on another device (you can use Windows ... and the printer queue name exsists with no special characters. ...
    (comp.sys.ibm.as400.misc)
  • Re: Delaying events for fixed amounts of time.
    ... amount of time after the *FIRST* event with a given key arrives. ... I have anything Windows has to offer. ... Pause for, say, 250ms each time through the loop ... Have a queue of keys, each time a new key arrives, add it to the end ...
    (comp.programming.threads)
  • RE: what type of memory do C# events reside in?
    ... //to fire event the worker thread calls: ... on the listener GUI form: ... The Invoke mechanism uses the message queue to notify the UI thread that it ... will run until it is preempted by an even higher priority thread or until it ...
    (microsoft.public.windowsce.app.development)
  • Re: fully cancelable
    ... that means it will be holding a lock ... in your code) a worker thread which just puts stuff onto a blocking queue, ... Look this is the greatest news for JAVA on the Web since it's GUI ...
    (comp.lang.java.programmer)