1

I have an Universal Windows Platform app and for background checkings I use winrt component (separate project in solution). I register it correctly and system launches my winrt class every 15 minutes. The problem is that my app uses the same files and makes the same checks that background checker does. So I need to know is my app alive (background or foreground) to skip background check to avoid multithread file access. Is there any way to get access to state of my app from winrt component, or query system does my app active?

Tertium
  • 6,049
  • 3
  • 30
  • 51

1 Answers1

1

Since the Anniversary update, it is now possible to run background tasks from within the same process as the main app. Both the background task and the main app share the same memory. See Create and register a single-process background task for information on how to do this.

If you want to keep the background task in a separate process to the main app, then you can use a named EventWaitHandle:

Foreground app code

// Keep this handle alive for the duration of your app
eventHandle = new EventWaitHandle(true, EventResetMode.ManualReset, "MyApp");

Background app code

EventWaitHandle handle;

if (EventWaitHandle.TryOpenExisting("MyApp", out handle)) {
    // Foreground app is running
} else {
    // Foreground app is not running
}

You need to be mindful of possible race conditions here (if that is a problem for your situation). You could probably make better use of the EventWaitHandle to synchronize whatever file task it is both processes need to do.

Decade Moon
  • 32,968
  • 8
  • 81
  • 101
  • thanks! But single-process background task is not available in uwp 8.1. It would solve so many problems if would. 1. so when my app is closed var eventHandle is collected and automatically unregistered system-wide? 2. Not quite understand about race condition - if I see that app runs I do nothing. Or do you mean I can enter "else" when app is offline and continue checks when app goes online? – Tertium Oct 21 '16 at 15:18
  • UWP is Windows 10 only. I assumed you were developing for Windows 10 since it was tagged `uwp`. The EventWaitHandle method should still work for Windows 8.1 though. It will be closed automatically when the process terminates. With the race condition, I just mean there might be a small chance that the background task runs first, sees that the foreground process isn't running, *then* the foreground process starts and tries to access the files too. – Decade Moon Oct 21 '16 at 23:40
  • uwp is not win 10 only, initially it was for 8.1, but now it almost impossible to find docs for uwp 8.1. But it does exist. I have uwp apps online - most code is shared between winphone 8.1 and win 8.1. Another thing that msft will not update some devices to 10 (and some even to 8.1) but I need to support my old users as well. I started to develop for winmob since wince 3.0, but this situation takes place with every major update - msft hates its customers. For developers - I thought the same way, but when I started to develop for iOS/OSX I've found out that there's greater evil than msft :) – Tertium Oct 22 '16 at 08:23
  • UWP is the [Universal Windows Platform](https://en.wikipedia.org/wiki/Universal_Windows_Platform) introduced in Windows 10. Windows 8.1 had the concept of "Universal Apps", but this just meant that code could be shared between desktop and mobile. – Decade Moon Oct 22 '16 at 09:22
  • oh, my bad. but from development perspective code sharing is "universal" enough (comparing to pre-8.1 problems), too similar terms to confuse :) I'd say universal app 8.1 looks like beta for 10 (even most related docs disappeared), but there were no "release" for 8.1, so we have what msft gave us. – Tertium Oct 22 '16 at 09:31