using RasNotificationHandler in Console Application

Apr 10, 2012 at 1:33 PM

The OpenNETCF.Net.Ras Library has the possibility, to call Dial() in async mode. The callback is a MessageWindow-Handle.

If i use Dial() (async-Mode) in a console application, i have to poll the Dial-state due the lack of a Message-Pump. I could start a Form in a thread handling the windows messages...

It is possible to adapt the lib for using Dial() (async-Mode + RasNotificationHandler) in a console application too ?


Coordinator
Apr 11, 2012 at 3:28 PM

Unfortunately the RAS subsystem uses windows messages to communicate state changes.  You must have a message pump in order to receive these messages.  You could adapt your console app to have a background thread with a message pump, which would effectively enable the events in the library.

Apr 12, 2012 at 8:07 AM
Edited Apr 12, 2012 at 1:51 PM

Yes i thinked about a thread creating the message pump like this:

AutoResetEvent myEvent = new AutoResetEvent(false);

RasEntry myEntry = new RasEntry();
myEntry.RasStatus += (hConn, state, errorCode) => {
  switch (state) {
    case RasConnState.Connected:
    case RasConnState.Disconnected:
      myEvent.Set();
      break;
    default:
        break;
  }
};
myEntry.Error += (entry, errorCode) => {
  if (errorCode != RasError.Success)
    myEvent.Set();
};

ThreadPool.QueueUserWorkItem(delegate(object o) { OpenNETCF.Windows.Forms.Application2.Run(); });
RasError err = myEntry.Dial(true);
if (err == RasError.Success) {
  int num = 0;
  do {
    OpenNETCF.Windows.Forms.Application2.DoEvents();
    if (myEvent.WaitOne(1000, false))
      break;
  } while (++num < 50);
}

OpenNETCF.Windows.Forms.Application2.Exit();

I need the Async mode because sometimes the RasDial() call doesn't return (did you see that ?).

For now i poll the RasEntry.Status using a timeout:

RasError err = entry.Dial(true);
if (err == RasError.Success) {
  int start = Environment.TickCount;
  do {
    if (entry.Status.State == RasConnState.Connected)
      break;
    else
      Thread.Sleep(500);
  } while ((Environment.TickCount - start) < 50000);
}

In case of a timeout it is allowed to call RasHangUp for freeing the ressources ?

Jun 20, 2012 at 8:36 AM

I am using the Dial asynchronly. But when i get a timeout on Dial a call of RasHangUp will block ...

MSDN: The connection is terminated even if the RasDial call has not yet been completed.