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)
        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

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

1 Like

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…

[url]Microsoft Learn: Build skills that open doors in your career

maybe this will help with knowing why you might be getting the exception when calling the BeginInvoke, read the remarks:
[url]Microsoft Learn: Build skills that open doors in your career

i hope this helps.
Jay

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

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

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:

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.

1 Like