Re: Mobile Service & Auto Start



First off let me say I really appreciate your help, usually I post on msdn
and 6 weeks later someone gets back to me with something I already figured
out.

Now I have written quite a few mobile device apps, including a lot of apps
that do pinvoke calls, but as I mentioned it does always seem like a pain to
find any good info on the pinvoke calls when they are needed. up till now I
have never needed to write an app like this and I just figured since i have
written Windows Services before that do certain things at certain times, or
because of certain events that it would not be difficult. Boy I was wrong.

The init thing works great, I just played with what exe inits in what order
and that works out good for me with an implementation to unlock a service
from accessing a file and i knew I could always copy in the contents of
pim.vol from one file from another based on each individual value I just
wanted to make it easy on myself.

Now as for this CeRunAppAtTime or
OpenNetCF.WindowsCE.Notification.NotifyRunAppAtTime which are technically
both exactly the same by sending the app location and the SYSTEMTIME struct,
the only difference being is theres converts the datetime object where mine
expects the SYSTEMTIME struct.

Now just that you do understand I do kinda know what I am doing, I am really
just very dumb about the CeRunAppAtTime call and why it may not work.

So I have an exe that I start 5 min after I push a button. Now for SAG I use
calc.exe
and that didnt work either just to see if there was a problem with open my
exe.

I have:

[DllImport("coredll.dll", SetLastError=true)]
internal static extern bool CeRunAppAtTime(string appName, ref SYSTEMTIME
time);

and a SYSTEMTIME struct.

and i call

CeRunAppAtTime(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase, SYSTEMTIME.FromDateTime(DateTime.Now.AddMinutes(5));

Application.Exit();

----The above does nothing----

So this is what I am getting fom what you posted. CeRunAppAtTime doesnt
allow you to run an exe at a later time? I need it to run even if my app has
closed ( a real close using Application.Exit() not a minimize ) So I would
have to thread an exe that does nothing but define an event, then I could run
CeRunAppAtTime to execute an event within that exe which is running in the
background.

Is this correct?

This is one of the most confusing calls I have ever encountered, what I want
is a call that says

Execute "FileName.exe" at "DateTime" no matter what is going on.

Let me know if I am correct in my assumption of what you posted.

Thank you again!




"Paul G. Tobey [eMVP]" wrote:

I've replied on the registry and error code finding to the last message
because I didn't want those points to get lost...

Are you sure you *closed* your application? Remember that, on Windows
Mobile devices, the X box on the title bar *does not close the application*.
It minimizes it (called Smart Minimize, if you want to search the archives
for info).

As I mentioned in my original description of how I'd handle this, you might
want to have CeRunAppAtTime() fire an event, not launch a program.
OpenNETCF has a class library for doing that. Check the Notify class. It's
in the Smart Device Framework 1.4, I know. Ah, here's the sample I wrote
for firing an event at a time. I'll copy it below and hopefully it won't be
overly long...

Whoops, you started too close to the deadline! Always allow several months
for understanding a completely new technology! You'll hardly ever be wrong.

Paul T.

----

code for form1.cs from notification sample of OpenNETCF. You need Smart
Device Framework (open source), 1.4 to use it.

/// This Notification sample uses the Notification Manager in Windows CE.NET
/// and later to notify the application that a given time of day has been
/// reached via an event, rather than by launching the application, as the
/// older version OSes will do. For this, you create a named event, call
/// CeRunAppAtTime() with a very special application name parameter (and the
/// time at which the notification should fire, of course).

namespace Notification
{
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using OpenNETCF;
using OpenNETCF.Drawing;
using OpenNETCF.Windows.Forms;
using OpenNETCF.Win32;
using OpenNETCF.Win32.Notify;
using OpenNETCF.Threading;
using System.Threading;

/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private OpenNETCF.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.RadioButton HoursRadio;
private System.Windows.Forms.RadioButton MinutesRadio;
private OpenNETCF.Windows.Forms.TextBoxEx DelayTimeEdit;
private System.Windows.Forms.Button GoButton;

private OpenNETCF.Threading.ThreadEx thread = null;
private bool appExiting = false;
private int delay = 1;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

// 1.3-based stuff.
this.DelayTimeEdit = new OpenNETCF.Windows.Forms.TextBoxEx();
this.DelayTimeEdit.Style = OpenNETCF.Windows.Forms.TextBoxStyle.Numeric;
this.groupBox1 = new OpenNETCF.Windows.Forms.GroupBox();
//
// DelayTimeEdit
//
this.DelayTimeEdit.Location = new System.Drawing.Point(8, 33);
this.DelayTimeEdit.Size = new System.Drawing.Size(104, 20);
this.DelayTimeEdit.Text = "5";
// groupbox1
this.groupBox1.Location = new System.Drawing.Point( 120, 25 );
this.groupBox1.Size = new System.Drawing.Size(110, 60);
this.groupBox1.Text = "Units";
this.groupBox1.Controls.Add( this.MinutesRadio );
this.groupBox1.Controls.Add( this.HoursRadio );
// form
this.Controls.Add( this.DelayTimeEdit );
this.Controls.Add(this.groupBox1 );
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.HoursRadio = new System.Windows.Forms.RadioButton();
this.MinutesRadio = new System.Windows.Forms.RadioButton();
this.GoButton = new System.Windows.Forms.Button();
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Size = new System.Drawing.Size(184, 20);
this.label1.Text = "Delay before notification:";
//
// HoursRadio
//
this.HoursRadio.Location = new System.Drawing.Point(16, 16);
this.HoursRadio.Size = new System.Drawing.Size(88, 16);
this.HoursRadio.Text = "Hours";
//
// MinutesRadio
//
this.MinutesRadio.Checked = true;
this.MinutesRadio.Location = new System.Drawing.Point(16, 40);
this.MinutesRadio.Size = new System.Drawing.Size(88, 16);
this.MinutesRadio.Text = "Minutes";

//
// GoButton
//
this.GoButton.Location = new System.Drawing.Point(96, 112);
this.GoButton.Text = "Go";
this.GoButton.Click += new System.EventHandler(this.GoButton_Click);
//
// Form1
//
this.ClientSize = new System.Drawing.Size(250, 143);
this.Controls.Add(this.GoButton);
this.Controls.Add(this.label1);
this.Text = "Notification Sample";
this.Closing += new
System.ComponentModel.CancelEventHandler(this.Form1_Closing);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>

static void Main()
{
ApplicationEx.Run(new Form1());
}

private void GoButton_Click(object sender, System.EventArgs e)
{
// The user has triggered the delay. First, calculate
// when the notification will be fired, then set
// up an event to be triggered by the notification,
// then fork off a thread to wait for the notification.
// When the notification is fired, the thread
// will invoke a method to show a dialog and exit,
// returning us to the idle state.
delay = Int32.Parse( DelayTimeEdit.Text );
if ( HoursRadio.Checked )
{
delay *= 60;
}

// Delay is now in minutes.

// Start thread to monitor event.
thread = new OpenNETCF.Threading.ThreadEx( new ThreadStart(
this.ThreadProc ) );
thread.Start();
}

public void Notify_Fired(object sender, System.EventArgs e)
{
// The notify event fired. Notify the user.
MessageBox.Show( "The notification has fired.",
"Notification" );
}

public void ThreadProc()
{
// The thread procedure simply waits for the
// notification event to fire (or for the main
// application thread to set it, because it's
// time to leave the thread).

// Figure out when.
DateTime runtime = DateTime.Now;
runtime = runtime.AddMinutes( delay );

// Create event for notification.
EventWaitHandle notifyEvent = new OpenNETCF.Threading.EventWaitHandle(
false,
OpenNETCF.Threading.EventResetMode.AutoReset,
"OpenNETCFSampleNotifyEvent" );

// Set up notification.
Notify.RunAppAtTime(
"\\\\.\\Notifications\\NamedEvents\\OpenNETCFSampleNotifyEvent", runtime );

// Wait for the notification event to fire.
notifyEvent.WaitOne();

// Let the notification subsystem know that it's
// done with the event.
try
{
Notify.RunAppAtTime(
"\\\\.\\Notifications\\NamedEvents\\OpenNETCFSampleNotifyEvent",
DateTime.MinValue );
}
catch( Exception e )
{
}

// Close event handle.
notifyEvent.Close();

// If the event was fired because the application
// is exiting, do nothing else; just return.
if ( !appExiting )
{
// Notify the user that the event fired.
this.Invoke( new EventHandler( Notify_Fired ) );
}

// Clear thread to indicate that we're done.
thread = null;
}

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
// When we're ready to leave, we have to be sure
// that we don't leave any threads running.
appExiting = true;

if ( thread != null )
{
EventWaitHandle notifyEvent = new OpenNETCF.Threading.EventWaitHandle(
false,
OpenNETCF.Threading.EventResetMode.AutoReset,
"OpenNETCFSampleNotifyEvent" );

// Notify the thread to stop.
notifyEvent.Set();

notifyEvent.Close();
}
}
}
}



.


Loading