Main Site Documentation

Async event firing


#1

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)
        PlaybackFinished();
}

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

Anyone ever did something like this before on NETMF?

Thanks


#2

Ok, I’ve found another method.

I started a Dispatcher on a seperate thread:


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

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)
        {
            OnPlaybackFinished();
            return null;
        }, null);
}


#3

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.


#4

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


#5

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

http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.aspx

maybe this will help with knowing why you might be getting the exception when calling the BeginInvoke, read the remarks:
http://msdn.microsoft.com/en-us/library/cc531880.aspx

i hope this helps.
Jay


#6

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…

Regards


#7

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);
timer.Start();

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:
http://www.java2s.com/Open-Source/ASP.NET/Framework/netmf/Microsoft/SPOT/CatalogSPOT.htm

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…


#8

you may also want to look into thread synchronization:

http://blogs.msdn.com/b/netmfteam/archive/2011/02/01/thread-communication-and-synchronization.aspx

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

i hope this helps…

Jay.