Let me start by saying that I had the PeriodicTask already working a couple of days back, but when I came back to do something else I noticed the PeriodicTask's OnInvoke is not called anymore.
I think I am doing the basics correct: removing existing PeriodTask if found, calling LaunchForTest only in debug build and I've checked that the ScheduledAgent is referenced properly in the project and the WMAppManifest.xml.
This is how I setup the PeriodicTask:
try
{
PeriodicTask backgroundTask = null;
backgroundTask = ScheduledActionService.Find(BGTASK_NEW_EPISODES) as PeriodicTask;
if (backgroundTask != null)
{
ScheduledActionService.Remove(backgroundTask.Name);
}
// Start our background agent.
backgroundTask = new PeriodicTask(BGTASK_NEW_EPISODES);
backgroundTask.Description = "Foobar";
ScheduledActionService.Add(backgroundTask);
#if DEBUG
ScheduledActionService.LaunchForTest(BGTASK_NEW_EPISODES, TimeSpan.FromSeconds(5));
#endif
}
catch (InvalidOperationException e)
{
if (e.Message.Contains("BNS Error: The action is disabled"))
{
App.showNotificationToast("Background tasks have been disabled from\nsystem settings.");
}
}
catch (Exception) { }
}
Here's my WMAppManifest.xml:
<ExtendedTask Name="BackgroundTask">
<BackgroundServiceAgent Specifier="ScheduledTaskAgent" Name="PodcatcherBackgroundService" Source="PodcatcherBackgroundService" Type="PodcatcherBackgroundService.ScheduledAgent" />
</ExtendedTask>
When I install the app for the first time, then the OnInvoke is called. But if I restart the app, it's not called. The same is true for both device and emulator.
I've also verified that the background task is enabled in settings and I have a fully charged battery (device is a WP7 device and it's connected via USB to PC. For the emulator, of course, this doesn't matter).
So what should I check next?
Thanks!
Ok, seems I got it resolved.
In certain cases I forgot to call NotifyComplete() in the background task. In that case Windows Phone seems to just ignore subsequent tries to invoke the background worker.
Related
I know that when Windows is shutting down, it sends a WM_QUERYENDSESSION message to each application. This makes it easy to detect when Windows is shutting down. However, is it possible to know if the computer going to power-off or is it going to restart after Windows has shutdown.
I am not particularly hopeful, considering the documentation at MSDN has this to say about WM_QUERYENDSESSION: "...it is not possible to determine which event is occurring," but the cumulative cleverness of stackoverflow never ceases to amaze me.
In Windows 7 (and probably also in Vista / 8 / Server) you could use the system events to track whether Windows is shutting down (and powering off the computer) or just restarting. Every time a shutdown/reboot is initiated (by any means - clicking the button in Start menu, or programmatically), Windows 7 writes one or two events in the System log, source USER32, event ID 1074. You can see these events recorded if you open the Event Viewer from Administrative Tools (filter the System log to see only ID 1074). The description (message) of these events contains the shutdown type. So you could parse the description of the most recent event of this type (after the shutdown was initiated), looking for the necessary word (shutdown, reboot/restart).
I didn't try to see the shutdown type written in the event when using the power button to gracefully shutdown Windows (I usually disable this function), but some site suggests that it states a "power off" type instead of "shutdown" - so check it out, if you need to be sure. Or simply look for a "reboot" type - if it's not found, then a "shutdown" type is assumed.
In Windows XP, from my experience, an event 1074 is recorded only if the shutdown/reboot is done programmatically (e.g. during a program install or using the shutdown.exe utility). So it does not register the shutdowns initiated from the shell (Explorer), but perhaps you could combine this method with reading the value from registry as proposed in another answer. Also, keep in mind that in WinXP the message of event 1074 contains the word "restart" no matter what the real type of shutdown is, so you should look at the "Shutdown Type:" field, which will state either "shutdown" or "reboot".
Related to this, an event ID 1073 is recorded whenever Windows fails to shutdown/reboot for some reason (e.g. if an application doesn't allow to shutdown as a response to WM_QUERYENDSESSION). In that case the message will also contain words as "shutdown", "reboot" or "power off" - in WinXP. For Win7 this type of event is less useful in our case, since it won't make any difference between shutdown and reboot. But for WinXP - if you only need to intercept the shutdown/reboot, perform some actions, then continue the corresponding shutdown or reboot process - it should work as expected.
From here:
You can read the DWORD value from
"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shutdown
Setting" to determine what the user
last selected from the Shut Down
dialog.
A bit of a roundabout solution, but it should do the trick.
A trick that usually works is to trap WM_ENDSESSION and log it. Now keep track of the time. If the system comes back up within a reasonable peroid (say 5 minutes). Then that was a reboot, not a shutdown.
Idea: If the system comes back up within 5 minutes, does it really matter if the user clicked 'shutdown' or 'reboot'?
If you really need to detect a shutdown (and the only reason I think you'd need to do this is if you're depending upon an obscure behavioral software difference between a shutdown vs a reboot) you could investigate API hooking of ExitWindowsEx and related functions but I don't recommend this approach. Rethink if you really need to detect this directly.
Possible experimental solution for Windows7 could be the following. (I'm not sure if this works well with other localizations, therefore I would call it a workaround)
using System.Diagnostics.Eventing.Reader;
namespace MyApp
{
public class RestartDetector : IDisposable
{
public delegate void OnShutdownRequsted(bool restart);
public OnShutdownRequsted onShutdownRequsted;
private EventLogWatcher watcher = null;
public RestartDetector()
{
try
{
EventLogQuery subscriptionQuery = new EventLogQuery(
"System", PathType.LogName, "*[System[Provider[#Name='USER32'] and (EventID=1074)]]");
watcher = new EventLogWatcher(subscriptionQuery);
// Make the watcher listen to the EventRecordWritten
// events. When this event happens, the callback method
// (EventLogEventRead) is called.
watcher.EventRecordWritten +=
new EventHandler<EventRecordWrittenEventArgs>(
EventLogEventRead);
// Activate the subscription
watcher.Enabled = true;
}
catch (EventLogReadingException e)
{
}
}
public void EventLogEventRead(object obj, EventRecordWrittenEventArgs arg)
{
bool restart = false;
try
{
// Make sure there was no error reading the event.
if (arg.EventRecord != null)
{
String[] xPathRefs = new String[1];
xPathRefs[0] = "Event/EventData/Data";
IEnumerable<String> xPathEnum = xPathRefs;
EventLogPropertySelector logPropertyContext = new EventLogPropertySelector(xPathEnum);
IList<object> logEventProps = ((EventLogRecord)arg.EventRecord).GetPropertyValues(logPropertyContext);
string[] eventData = (string[])logEventProps[0];
foreach (string attribute in eventData)
{
if (attribute.Contains("restart")) { restart = true; break; }
}
}
}
catch (Exception e)
{
}
finally
{
if (onShutdownRequsted != null) { onShutdownRequsted(restart); }
}
}
public void Dispose()
{
// Stop listening to events
if (watcher != null)
{
watcher.Enabled = false;
watcher.Dispose();
}
}
}
}
The following is an example of XML which is written to the event log when a PC is restarted:
- <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
- <System>
<Provider Name="USER32" />
<EventID Qualifiers="32768">1074</EventID>
<Level>4</Level>
<Task>0</Task>
<Keywords>0x80000000000000</Keywords>
<TimeCreated SystemTime="2015-12-15T11:10:43.000000000Z" />
<EventRecordID>90416</EventRecordID>
<Channel>System</Channel>
<Computer>WIN7PC</Computer>
<Security UserID="S-1-5-21-1257383181-1549154685-2724014583-1000" />
</System>
- <EventData>
<Data>C:\Windows\system32\winlogon.exe (WIN7PC)</Data>
<Data>WIN7PC</Data>
<Data>No title for this reason could be found</Data>
<Data>0x500ff</Data>
<Data>restart</Data>
<Data />
<Data>WIN7PC\WIN7PCUser</Data>
<Binary>FF00050000000000000000000000000000000000000000000000000000000000</Binary>
</EventData>
</Event>
In OS X my gamepads are recognised correctly in SDL_PollEvent() at application startup. However, when I try hot plugging new gamepads or removing old gamepads, the SDL_PollEvent() does not trigger either SDL_CONTROLLERDEVICEADDED or SDL_CONTROLLERDEVICEREMOVED. The same code works correctly in Windows when I hot plug game controllers.
A more interesting note is that if I resize the window of my application, the hot plugging works. After resize event all the hot plugging events are triggered. It almost seems that the gamepad events are put in a some kind of waiting queue which is purged when the resize event happens. My SDL_PollEvent() code is quite standard as seen below.
case SDL_CONTROLLERDEVICEADDED:
if (SDL_IsGameController(e.cdevice.which))
{
SDL_GameController *pad = SDL_GameControllerOpen(e.cdevice.which);
if (pad)
{
SDL_Joystick *joy = SDL_GameControllerGetJoystick(pad);
int instanceID = SDL_JoystickInstanceID(joy);
if(m_gameControllers.count(instanceID) == 0)
{
m_gameControllers.insert(std::make_pair(instanceID, pad));
}
}
}
break;
case SDL_CONTROLLERDEVICEREMOVED:
{
auto it = m_gameControllers.find(e.cdevice.which);
if (it != m_gameControllers.end())
{
SDL_GameController* pad = m_gameControllers[e.cdevice.which];
SDL_GameControllerClose(pad);
m_gameControllers.erase(it);
}
}
break;
Has anyone else experienced this?
After some struggling I found the solution: Call SDL_PollEvent() from the main thread. Initially I called the gamepad handling method from the CVDisplayLink thread which resulted the described behaviour.
In my case the solution was simply to add dispatch_async call to my gamepad handling function.
dispatch_async(dispatch_get_main_queue(),^ { handleGamePad();});
I am creating an app with a timer in it. I want to display a toast notification(or some sort of notification) if the person leaves the app so they know the timer is over. I used the "PeriodicTask" using ".FromSeconds", but it seemed it didn't fire it at the specified time.
PeriodicTask periodicTask = new PeriodicTask("TaskTest");
periodicTask.Description = "Task";
try
{
IsolatedStorageSettings.ApplicationSettings["TimerForSchedule"] = TimeNum;
ScheduledActionService.Add(periodicTask);
ScheduledActionService.LaunchForTest("TaskTest", TimeSpan.FromSeconds(((TimeNum*60))));
}
On the "OnInvoke" method for the scheduled agent project, I have the following:
protected override void OnInvoke(ScheduledTask task)
{
if (task.Name == "TaskTest")
{
int time = Convert.ToInt32(IsolatedStorageSettings.ApplicationSettings["TimerForSchedule"]);
bool periodic = (bool)(task is PeriodicTask);
ShellToast toast = new ShellToast();
toast.Title = "Done";
toast.Content = "Timer is over";
toast.Show();
}
NotifyComplete();
ScheduledActionService.Remove("TaskTest");
}
Everything seems to fire properly because I DO get a notification, but I do not get it at the expected time.
Any help would be appreciated.
Is your app running in the foreground? If so, the ShellToast won't show up. Your app must be in the background for it to show up. Read more about that and potential workaround at my other # How can I create a shelltoast?
I think that the time you set to run your PeriodicTask is just a hint for the OS, it does not mean that it will fire it exactly at that time.
Windows Phone 7.5 / Silverlight app
If user is playing music / radio on their phone and they try to launch my application, I want to give user an option to stop the currently playing option.
Working fine:
The message popup shows up fine. When I select Cancel, the popup closes, the music keeps playing and my app starts/works as normal.
Issue:
If I select Ok i.e. to stop the currently playing music on phone, the music stops but at the same time my app also exits.
Any ideas what I am doing wrong here?
Here is the code I am using. I call this method on launching:
private void CheckAudio()
{
if (FMRadio.Instance.PowerMode == RadioPowerMode.On)
{
MessageBoxResult Choice;
Choice = MessageBox.Show("For better user experience with this application it is recommended you stop other audio applications. Do you want to stop the radio?", "Radio is currently playing!", MessageBoxButton.OKCancel);
if (Choice == MessageBoxResult.OK)
{
FMRadio.Instance.PowerMode = RadioPowerMode.Off;
}
}
if (MediaPlayer.State == MediaState.Playing)
{
MessageBoxResult Choice;
Choice = MessageBox.Show("For better user experience with this application it is recommended you stop other audio/video applications. Do you want to stop the MediaPlayer?", "MediaPlayer is currently playing!", MessageBoxButton.OKCancel);
if (Choice == MessageBoxResult.OK)
{
MediaPlayer.Stop();
}
}
}
Update:
I posted my solution below. Do let me know if I am doing anything wrong.
I found the following error was being thrown:
FrameworkDispatcher.Update has not been called. Regular
FrameworkDispatcher. Update calls are necessary for fire and forget
sound effects and framework events to function correctly.
So I added this code and now it is working fine. Now upon clicking OK, the music player stops and my app launches fine. I call the SetupTimer method from InitializeComponent in App.xaml.cs
private GameTimer gameTimer;
private void SetupTimer()
{
gameTimer = new GameTimer();
gameTimer.UpdateInterval = TimeSpan.FromMilliseconds(33);
// Call FrameworkDispatcher.Update to update the XNA Framework internals.
gameTimer.Update += new EventHandler<GameTimerEventArgs>(gameTimer_Update); //delegate { try { FrameworkDispatcher.Update(); } catch { } };
// Start the GameTimer running.
gameTimer.Start();
// Prime the pump or we'll get an exception.
FrameworkDispatcher.Update();
}
void gameTimer_Update(object sender, GameTimerEventArgs e)
{
try { FrameworkDispatcher.Update(); }
catch { }
}
If anybody sees any problem/issue with the above please do let me know. Thanks.
I have failed submission (can't even get past upload verification) because my background agent creates two instances, but I can't figure out why.
Here is the method, in app.xaml.cs (I have also tried locating it in the MainPageVM.cs and MainPage.xaml.cs with the same results)
private void RegisterBackgroundTask()
{
var taskName = "xxUpdater";
var oldTask = ScheduledActionService.Find(taskName) as PeriodicTask;
if (oldTask != null)
{
ScheduledActionService.Remove(taskName);
}
PeriodicTask task = new PeriodicTask(taskName);
task.Description = Strings.xxBackgroundTaskDescription;
oldTask = ScheduledActionService.Find(taskName) as PeriodicTask;
if (oldTask == null)
{
ScheduledActionService.Add(task);
}
//ScheduledActionService.LaunchForTest(taskName, TimeSpan.FromSeconds(60));
}
Here is the call, in Application_Launching:
private void Application_Launching(object sender, LaunchingEventArgs e)
{
{ RegisterBackgroundTask(); }
}
The first time the app runs, I back out, look, and there is one task. Then I run again, stepping through, and the remove call turns off the task (instead of removing it). When it gets to the add call, not only does it turn the original task on, but it adds another. From that point on, when I back out and restart the app, the remove turns ONE instance off, and leaves the other, and the Add call turns it back on. Never is oldTask null except after rebuild or uninstall.
The if(oldTask ==old) wasn't initially there, I added it hoping that it would prevent the creation of the second instance. Sometimes, when I uninstall, there is still a background task that is turned off, and I have to turn the phone off and back on to get it to go away.
I have the same problem with the project I'm working on.
For some reason I had the backgroundtask defined twice in my WMAppManifest.xml
Check you WMAppManifest.xml and see if you also have it defined twice.