Main Site Documentation

Async event firing


I want to fire an event asynchronously.

In the full .NET framework one would use ThreadPool.QueueUserWorkItem for that. Since that is not available I tried to do it by calling BeginInvoke on a delegate.

private delegate void AsyncDelegate();        
private static AsyncDelegate asyncOnPlaybackFinished = new AsyncDelegate(OnPlaybackFinished);

public delegate void PlaybackFinishedHandler();
public static event PlaybackFinishedHandler PlaybackFinished;
private static void OnPlaybackFinished()
    if (PlaybackFinished != null)

// This line throws a System.NotSupportedException
                delegate(IAsyncResult ar)
                }, null);

Anyone ever did something like this before on NETMF?



Ok, I’ve found another method.

I started a Dispatcher on a seperate thread:

// Start dispatcher thread
dispatcherThread = new Thread(
        dispatcher = Dispatcher.CurrentDispatcher;
        while (true)
dispatcherThread.Priority = ThreadPriority.BelowNormal;

Later you can async run some code. (The code will be executed by the Dispatcher.Run on the seperate thread):

// Asynchronous fire the OnPlaybackFinished event
if (dispatcher != null)
    dispatcher.BeginInvoke(delegate(object arg)
            return null;
        }, null);


First, BegilnInvoke is used with GUI programming to run something in the thread created the GUI control.

Using firing an event means you notify all those that have registered for the event. It has no semantics for running in a seperate thread.

I assume you want to handle the event in a seperate thread.

You can build you own worker thread pool.

Create a queue for your work item, based upon the event.

Associated with the queue is an AutoReset object. Each time you place something in the queue you set the event.

You can then start a number of threads, which wait on the AutoReset object. When the AutoReset is set, one thread wakes up. This thread then extracts a work item from the queue, and processes it. When the thread is done, it then waits on the AutoReset event.

This is a basic description, you have to worry about locking on the queue, having a thread check the queue before doing a wait, and waking up and finding an empty queue.


@ Mike: thanks for you answer. I think what you are talking about is exaclty what the Dispatcher class does in the background.


Hi Wouter,
you may want to look into the DispatcherTimer…

maybe this will help with knowing why you might be getting the exception when calling the BeginInvoke, read the remarks:

i hope this helps.


Yes, it also uses a Dispatcher. But since there is no Application started, there is no dispatcher running on the NETMF system. So you need to create your own dispatcher so you can call the Run method in a while loop yourself. Else nothing gets dispatched…



hi there,
i do not have any hardware to test anything yet, still waiting for the spider to show up :wink:

i guess you are not using any UI Elements, so in that case a timer maybe more appropriate …

//when using UI Element you can hook into the UI Dispatcher.

dispatchTimer timer = new DispatcherTimer();
timer.Tick += new EventHandler(OnTimer);
timer.Interval = new TimeSpan(0, 0, 1);

public void OnTimer(object sender, EventArgs e)
   //DO something... Refresh

maybe peaking at the source code of the dispatcher.cs and dispatcherTimer.cs might help:

once i get my hardware and get familiar with it I’ll revisit this thread…

This is for my personal note: maybe look into how Dispatching work in DPWS and WCF …

Good luck…


you may also want to look into thread synchronization:

read the article all the way down to : Thread synchronization - Events

i hope this helps…