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

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

问题描述

我正在创建一个具有前台应用程序(当然)并且同时包含PeriodicTask和ResourceIntensiveTask的应用程序.

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

我看不到在系统范围内的互斥对象上执行此操作的任何方法.理想情况下,当启动后台任务时,会弹出一个线程,侦听来自前台应用程序的信号,如果接收到该信号,则可以干净地关闭后台任务.

除了轮询(例如特定文件的文件夹)之外,没有人对前台应用程序启动时如何发出信号通知我的前台应用程序发出干净的建议吗?

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

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

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

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

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

解决方案

无并发更新解决方案:

应用程序(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中可用成员的最佳解决方案:

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

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.

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

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.

解决方案

No concurrent update solution:

Application (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 (Main thread) :

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)
}

Stop PeriodicTask when Application starts solution:

Application (App()) :

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

// Application    

m.ReleaseMutex();

PeriodicTask (Separate thread) :

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

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

// Abort task

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天全站免登陆