Windows phone 7 如何使用WP7计划任务处理异步网络I/O?
使用Mango,可以创建一个计划任务来更新ShellTiles数据 完成后,由任务调用Windows phone 7 如何使用WP7计划任务处理异步网络I/O?,windows-phone-7,Windows Phone 7,使用Mango,可以创建一个计划任务来更新ShellTiles数据 完成后,由任务调用NotifyComplete() 鉴于手机上的I/O应该是异步的,在调用NotifyComplete()之前,如何确保I/O已完成 通过灵长类动物?或者在任务通知手机操作系统已完成后,是否允许I/O完成 同步是显而易见的答案,但在电话中,阻塞并不是一个好的选择。计划的任务不是同步执行的。它们被启动,然后在强制终止之前有15秒的时间调用NotifyComplete(或abort) 为了直接回答您的问题,您可以使用
NotifyComplete()
鉴于手机上的I/O应该是异步的,在调用NotifyComplete()
之前,如何确保I/O已完成
通过灵长类动物?或者在任务通知手机操作系统已完成后,是否允许I/O完成
同步是显而易见的答案,但在电话中,阻塞并不是一个好的选择。计划的任务不是同步执行的。它们被启动,然后在强制终止之前有15秒的时间调用
NotifyComplete
(或abort)
为了直接回答您的问题,您可以使用异步IO方法,然后从complete事件或回调调用NotifyComplete
这里有一个例子。我已经使用了Microsoft.Phone.Reactive的功能,但是如果您愿意,可以用传统的方式使用Begin/EndGetResponse
public class SampleTask : ScheduledTaskAgent
{
protected override void OnInvoke(ScheduledTask task)
{
HttpWebRequest request = WebRequest.CreateHttp("http://stackoverflow.com");
Observable.FromAsyncPattern<WebResponse>(
request.BeginEndResponse,
request.EndGetResponse
)()
.Subscribe(response =>
{
// Process the response
NotifyComplete();
}, ex =>
{
// Process the error
Abort(); // Unschedules the task (if the exception indicates
// the task cannot run successfully again)
});
// Synchronous control flow will continue and exit the OnInvoke method
}
}
public类SampleTask:ScheduledTaskAgent
{
受保护的覆盖无效OnInvoke(ScheduledTask任务)
{
HttpWebRequest请求=WebRequest.CreateHttp(“http://stackoverflow.com");
可观察的模式(
请求。开始响应,
request.EndGetResponse
)()
.订阅(响应=>
{
//处理响应
NotifyComplete();
},ex=>
{
//处理错误
Abort();//取消计划任务(如果异常指示
//任务无法再次成功运行)
});
//同步控制流将继续并退出OnInvoke方法
}
}
计划的任务不会同步执行。它们被启动,然后在强制终止之前有15秒的时间调用NotifyComplete
(或abort)
为了直接回答您的问题,您可以使用异步IO方法,然后从complete事件或回调调用NotifyComplete
这里有一个例子。我已经使用了Microsoft.Phone.Reactive的功能,但是如果您愿意,可以用传统的方式使用Begin/EndGetResponse
public class SampleTask : ScheduledTaskAgent
{
protected override void OnInvoke(ScheduledTask task)
{
HttpWebRequest request = WebRequest.CreateHttp("http://stackoverflow.com");
Observable.FromAsyncPattern<WebResponse>(
request.BeginEndResponse,
request.EndGetResponse
)()
.Subscribe(response =>
{
// Process the response
NotifyComplete();
}, ex =>
{
// Process the error
Abort(); // Unschedules the task (if the exception indicates
// the task cannot run successfully again)
});
// Synchronous control flow will continue and exit the OnInvoke method
}
}
public类SampleTask:ScheduledTaskAgent
{
受保护的覆盖无效OnInvoke(ScheduledTask任务)
{
HttpWebRequest请求=WebRequest.CreateHttp(“http://stackoverflow.com");
可观察的模式(
请求。开始响应,
request.EndGetResponse
)()
.订阅(响应=>
{
//处理响应
NotifyComplete();
},ex=>
{
//处理错误
Abort();//取消计划任务(如果异常指示
//任务无法再次成功运行)
});
//同步控制流将继续并退出OnInvoke方法
}
}
我不会总是在失败时调用Abort(),它会在应用程序再次运行之前取消任务计划。您不想仅仅因为一次更新中的网络连接不完整就这样做。@Chris-同意,我只是想说明这些选项。我添加了一条注释来澄清这一点。我不会总是在失败时调用Abort(),它会在应用程序再次运行之前取消任务计划。您不想仅仅因为一次更新中的网络连接不完整就这样做。@Chris-同意,我只是想说明这些选项。我添加了一条评论来澄清这一点。