I'm trying to use CeSetUserNotificationEx to run my app at a specified time. I'm getting weird behavior and I'm not sure why. Here's what's happening:
1) Start app, set new notification for Now + 2 min.
2) App exits.
3) Confirm (using Scarybear's Check Notifications) that the notification was created (it was).
4) Wait until 2 minutes has elapsed.
5) App has not started - the notification has not fired.
6) Start app again, another notification is set for Now + 2 min.
7) Immediately, app start again and shows the args from the original notification.
No matter what, the notification only runs if I set another notification for the app! Here's the code:
Code:
static void Main(string[] args)
{
MessageBox.Show("Args: " + (args != null && args.Length > 0 ? string.Join(", ", args) : "(None)"));
if (args == null || args.Length == 0)
{
DateTime nextRun = DateTime.Now;
string newArg = null;
nextRun = new DateTime(nextRun.Year, nextRun.Month, nextRun.Day, nextRun.Hour, nextRun.Minute, 0);
nextRun = nextRun.AddMinutes(2);
newArg = nextRun.ToString("h:mm:sstt");
CoreDLL.StartExecutableAtTime(nextRun, Assembly.GetExecutingAssembly().GetName().CodeBase, newArg);
MessageBox.Show("Next run: " + newArg);
}
else
CoreDLL.HandleStartExecutableAtTime(Assembly.GetExecutingAssembly().GetName().CodeBase);
}
public static unsafe int StartExecutableAtTime(DateTime timeToRun, string executablePath, string arguments)
{
long fileTimeUTC = timeToRun.ToFileTime();
long fileTimeLocal = 0;
SystemTime systemTime = new SystemTime();
FileTimeToLocalFileTime(ref fileTimeUTC, ref fileTimeLocal);
FileTimeToSystemTime(ref fileTimeLocal, ref systemTime);
return AddJob(systemTime, executablePath, arguments).ToInt32();
}
private static unsafe IntPtr AddJob(SystemTime systemTime, string appName, string args)
{
NotificationTrigger notificationTrigger = new NotificationTrigger();
UserNotification userNotification = new UserNotification();
IntPtr handle;
fixed (char* f_appName = appName.Trim().ToCharArray())
{
fixed (char* f_args = (args == null ? null : args.Trim().ToCharArray()))
{
notificationTrigger.Size = (UInt32)Marshal.SizeOf(notificationTrigger);
notificationTrigger.Type = 2;
notificationTrigger.pAppName = f_appName;
notificationTrigger.pArgs = f_args;
notificationTrigger.StartTime = systemTime;
notificationTrigger.EndTime = new SystemTime();
handle = CeSetUserNotificationEx(IntPtr.Zero, notificationTrigger, null);
}
}
return handle;
}
public static unsafe bool HandleStartExecutableAtTime(string executablePath)
{
return CeHandleAppNotifications(executablePath);
}