如何从 Windows Phone 上的前台应用程序发出 ScheduledTask 信号? [英] How to signal a ScheduledTask from a foreground app on Windows Phone?

查看:20
本文介绍了如何从 Windows Phone 上的前台应用程序发出 ScheduledTask 信号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,它有一个前台应用程序(当然)以及一个 PeriodicTask 和一个 ResourceIntensiveTask.

I'm creating an app that has a foreground app (of course) and both a PeriodicTask and a ResourceIntensiveTask.

如果用户在运行时启动应用程序,我需要一种关闭计划任务的方法.

I need a way to shutdown the scheduled tasks if the user launches the app itself while they are running.

我看不出有任何方法可以使用系统范围的互斥体来做到这一点.理想情况下,当后台任务开始时,会分出一个线程来监听来自前台应用程序的信号,如果它收到这个信号,它可以干净地关闭后台任务.

I don't see any way of doing this with system-wide Mutexes. Ideally when the background tasks started, a thread would be spun off that listens for a signal from the foreground app, and if it receives this signal it can shutdown the background task cleanly.

除了轮询(比如特定文件的文件夹)之外,是否有人对我的前台应用程序如何在前台应用程序启动时通知后台代理关闭有明确的建议?

Other than polling (say a folder for a specific file), does anyone have a clean suggestion on how my foreground app can signal my background agents to close down if the foreground app is launched?

我正朝着套接字前进,但这似乎有点过分了.

I'm heading towards sockets, but this seems like overkill.

背景:我目前使用 IsolatedStorage 来存储我的数据(不是数据库) - 最终我将转向使用数据库.

Background: I'm currently using IsolatedStorage to store my data (not a database) - eventually I'll move to using a database.

我的前台应用将本地数据与网络服务同步,并在从网络服务添加新项目时更新 UI.我使用一个文件来查找数据项,并在新项同步时附加到该文件中.

My foreground app synchronizes local data with a web service, and updates the UI when new items get added from the web service. I use a file which I seek through to find data items, and which I append to as new items get synced.

我希望后台代理运行以在后台执行此同步,更新隔离存储中的文件,包括索引等.但是一旦我启动了前台应用程序,我需要后台应用程序停止,以便前台和后台不会同时尝试更新相同的文件.

I want background agents to run to perform this synchronization in the background, updating the files in isolated storage, including indexes, etc. But as soon as I start up the foreground app, I need the background app to stop, so that the foreground and background don't both try to update the same files at the same time.

我一次只需要一个进程来更新本地数据,我需要前台进程抢占后台进程.

I need only one process to update the local data at a time, and I need the foreground process to preempt the background process.

推荐答案

无并发更新解决方案:

应用程序(App()):

var m = new Mutex(false, "MyMutex");
m.WaitOne(); //When PeriodicTask runs wait

if(lastSynchronization > 1.Minutes)
{  
  //Update only when your PeriodicTask not runs before 
}

//ApplicationExit      
m.ReleaseMutex();

PeriodicTask(主线程):

var m = new Mutex(false, "MyMutex");
if(m.WaitOne(0)) // Dont wait for the handle
{
  // Synchronization

  m.ReleaseMutex(); 
}
else
{
  //Abort task (You dont want to wait for the main application)
}

应用程序启动时停止PeriodicTask 解决方法:

应用程序(App()):

var m = new Mutex(false, "MyMutex");
m.WaitOne();

// Application    

m.ReleaseMutex();

PeriodicTask(独立线程):

var m = new Mutex(false, "MyMutex");

while (m.WaitOne(0)) // Check for main application
{
   m.ReleaseMutex(); 
}

// Abort task

我认为这是 WP7 中可用成员的最佳解决方案:

I think thats the best solution with the available members in WP7:

http://msdn.microsoft.com/en-us/library/system.threading.mutex(v=VS.96).aspx

这篇关于如何从 Windows Phone 上的前台应用程序发出 ScheduledTask 信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆