Named Events

C

Corey Lusher

I'm currently looking into setting up some named events for 2 separate
programs running on WinCE5.0 using NETCF.

As a starting point I'm using the example provided by Matt Dotson at:
http://blogs.msdn.com/mattdotson/archive/2006/03/03/543143.aspx

However, because EventWaitHandle does not exist in NETCF I've added the
OpenNETCF library to include it. When running this the events do not seem
to ever trigger correctly, as applications 2 and 3 are never able to detect
that application 1 has completed.

Any help would be much appreciated.
 
C

Corey Lusher

I started over trying to use a very simple program, here it is:


Program 1, open EventWaitHandle, wait 30 seconds until triggering it to
ensure Program 2 has started and is waiting:

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using OpenNETCF.Threading;

namespace App1
{
class Program
{
static void Main(string[] args)
{
// Create event
EventWaitHandle ewh = new EventWaitHandle(false, EventResetMode.
ManualReset, "Event1");

// Wait 30 seconds then fire it
Console.WriteLine("waiting 30 seconds...");
for (int i = 1; i < 31; i++)
{
Console.WriteLine("la la la... " + i);
Thread.Sleep(1000);
}

// Trigger event
ewh.Set();

// Close, we're done for now
ewh.Close();
Console.WriteLine("Goodbye");

}
}
}

Program 2, wait 10 seconds to ensure Program 1 has already created the EWH,
open said EWH and wait for it to be triggered:

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using OpenNETCF.Threading;

namespace App2
{
class Program
{
static void Main(string[] args)
{
// Wait 10 seconds to make sure app1 has started
Thread.Sleep(10000);

// Subscribe to app1's event
EventWaitHandle ewh = EventWaitHandle.OpenExisting("Event1");

// Now sit here and wait for it
Console.WriteLine("i'm waiting...");
ewh.WaitOne();

// It happened!
for (int i = 0; i < 10; i++)
{
Console.WriteLine("event 1 happened!");
Thread.Sleep(500);
}

// ok quit
ewh.Close();
Console.WriteLine("goodbye");

}
}
}
 
P

Paul G. Tobey [eMVP]

The docs for the createdNew return from the EventWaitHandle constructor are
wrong and there's a bug in the createdNew out parameter, but I was able to
use this constructor to do what you're trying to do with two applications.
Always use the constructor that creates the event, regardless of which
application is calling. That way, you simply don't care who created the
event; either application can win that race and it doesn't matter to either
one.

My test has an edit field for the event name and four buttons (create/open,
wait, set, and reset). I'm also using an auto-reset event, rather than
manual, but it shouldn't make any difference. Here's my basic code (same
for both programs):

-----

private void OpenCreateButton_Click(object sender, System.EventArgs e)

{

// Open the event.

bool createdNew;

ewh = new EventWaitHandle( false, EventResetMode.AutoReset,
EventNameEdit.Text, out createdNew );

if ( createdNew )

{

System.Diagnostics.Debug.WriteLine( "The event was created, not just
opened." );

}

else

{

System.Diagnostics.Debug.WriteLine( "The event was opened, not
created." );

}

}

private void WaitButton_Click(object sender, System.EventArgs e)

{

System.Diagnostics.Debug.WriteLine( "Waiting for the event..." );

if ( ewh.WaitOne( 10000, false ) )

{

System.Diagnostics.Debug.WriteLine( "Wait successful." );

}

else

{

System.Diagnostics.Debug.WriteLine( "Wait timed-out." );

}

}

private void SetButton_Click(object sender, System.EventArgs e)

{

System.Diagnostics.Debug.WriteLine( "Setting event..." );

ewh.Set();

System.Diagnostics.Debug.WriteLine( "Set done." );

}

private void ResetButton_Click(object sender, System.EventArgs e)

{

System.Diagnostics.Debug.WriteLine( "Resetting event..." );

ewh.Reset();

System.Diagnostics.Debug.WriteLine( "Reset done." );

}

-----

I can connect to the event from either program first, neither cares. I can
wait for the event in either program and set it from the other program and
everything works exactly as expected. Note that I've modified my version of
the EventWaitHandle source code for OpenNETCF to fix the createdNew bug,
but, other than that, it's the code found in SDF 1.4 that I'm using.

Paul T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top