Re: beginners question about UI threading

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



On Tue, 04 Aug 2009 12:15:02 -0700, Colin Peters <cpeters@xxxxxxxxxxxx> wrote:

[...]
Now, I want to add a component to the mix. It is periodically polling another DB for updates and needs to inform my service when interesting changes have taken place. The polling rate is outside my concern. The component will call a callback function when a change is detected. Since the component is to function fairly autonomously I guess it runs in its’ own thread (call it Thread3), probably a MP thread because it will have to handle timer messages.

The question is: How can I detect when the callback is called? Is it possible to execute the callback in a thread other than Thread3?

My first concern is your subject text, "...about UI threading". AFAIK, a service and a UI are mutually exclusive. So it's unclear to me what you mean by "UI threading".

As far as the question itself goes, you can invoke a delegate (i.e. callback) on a specific thread in a variety of ways:

-- Where there is a Forms or WPF message pump, you can use the normal Forms or WPF mechanisms (e.g. Control.Invoke() for Forms, )
-- Alternatively, in Forms or WPF you can use a SynchronizationContext as an abstraction away from the GUI-specific mechanisms to accomplish the same thing

Absent specific support, you can implement support yourself. IMHO the simplest way is to just create a producer/consumer queue, where the thread that should be executing the delegate is the consumer, and the threads that want the delegate to be executed are the producers. Alternatively, you can write your own SynchronizationContext sub-class that does the equivalent.

I believe that in the past, there have been threads in this newsgroup regarding producer/consumer delegate queues, so Google Groups may be able to turn up useful information, including possibly one or more implementation examples.

Pete
.



Relevant Pages

  • Re: beginners question about UI threading
    ... how can you create a thread with a message pump. ... The component will call a callback function when a change is detected. ... IMHO the simplest way is to just create a producer/consumer queue, where the thread that should be executing the delegate is the consumer, and the threads that want the delegate to be executed are the producers. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Delegates and pointers
    ... > public static extern int ptRegisterLinkStateCallback(int callbackID, ... This, of course, means that you have to declare the delegate in your library and not the consuming program. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Cant find the DbAsyncResult type. Whats going on?
    ... There I learned that the delegate instance was lurking inside the System.IAsyncResult parameter of the callback method. ... delegate int MyDelegate; ... static void CallBack ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Callbacks and Threads
    ... I DO need to be worried about accessing/modifying ... calling thread. ... > The threadpool - by the time the callback is ready to be called, ... > multicast delegate calls each delegate in turn, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Passing reference of the main thread control
    ... puplic delegate void SimbuttonCaptureStream( ... string client_location, string client_url, uint client_port) ... -- Move the call to Control.Invokeinto your callback method ... Then, the DLL can invoke the delegate directly method), and the method referenced by the delegate will handle the call to Control.Invoke. ...
    (microsoft.public.dotnet.languages.csharp)