我对Windows手机开发相对较新,而且我坚持这一点。我正在尝试运行后台资源密集型代理,该代理会在应用程序停用时发出网络请求。以下是代码的所有重要部分..
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
StartResourceIntensiveAgent();
Debug.WriteLine("App deactivating.");
}
private void StartResourceIntensiveAgent()
{
// Variable for tracking enabled status of background agents for this app.
Debug.WriteLine("Starting NetworkRequestTaskAgent.");
string resourceIntensiveTaskName = "NetworkRequestTaskAgent";
ResourceIntensiveTask resourceIntensiveTask = ScheduledActionService.Find(resourceIntensiveTaskName) as ResourceIntensiveTask;
// If the task already exists and background agents are enabled for the
// application, you must remove the task and then add it again to update
// the schedule.
if (resourceIntensiveTask != null)
{
RemoveAgent(resourceIntensiveTaskName);
}
resourceIntensiveTask = new ResourceIntensiveTask(resourceIntensiveTaskName);
// The description is required for periodic agents. This is the string that the user
// will see in the background services Settings page on the device.
resourceIntensiveTask.Description = "This demonstrates a resource-intensive task.";
// Place the call to Add in a try block in case the user has disabled agents.
try
{
ScheduledActionService.Add(resourceIntensiveTask);
// If debugging is enabled, use LaunchForTest to launch the agent in one minute.
}
catch (InvalidOperationException exception)
{
if (exception.Message.Contains("BNS Error: The action is disabled"))
{
MessageBox.Show("Background agents for this application have been disabled by the user.");
}
}
catch (SchedulerServiceException)
{
}
}
和ScheduledAgent.cs
protected override void OnInvoke(ScheduledTask task)
{
//TODO: Add code to perform your task in background
if (task is ResourceIntensiveTask)
{
string ToastMessage = "ResourceIntensiveTask is running";
ShellToast Toast = new ShellToast();
Toast.Title = "Background agent sample";
Toast.Content = ToastMessage;
Toast.Show();
string url = "http://someserver.net:8080/MultipleDeviceServ/javaQuery?request=sendId&title=deactivatedtest&id=deactivatedtest";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.BeginGetResponse(httpComplete, request);
}
NotifyComplete();
}
private static void httpComplete(IAsyncResult asyncResult)
{
Debug.WriteLine("Trying web request..");
HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
if (request != null)
{
try
{
Debug.WriteLine("Web request has gone through.");
}
catch (WebException e)
{
return;
}
}
}
现在我不确定后台任务是否开始运行,但是函数StartResourceIntensiveAgent()会在调试行打印"启动NetworkRequestTaskAgent时运行。除此之外,我对可能出现的错误知之甚少,我唯一的假设是,即使我将任务代理命名为NetworkRequestTaskAgent,它也可能与StartResourceIntensiveAgent函数中的部分无关" string resourceIntensiveTaskName =&#34 ; NetworkRequestTaskAgent&#34 ;;任何形式的见解都会非常有用。
答案 0 :(得分:0)
我没有注意到快速查看中的错误。
您已创建
的实例resourceIntensiveTask = new ResourceIntensiveTask (resourceIntensiveTaskName);
在'ScheduledActionService'中激活后台代理。添加('resourceIntensiveTask');
在'OnInvoke'上放置一个断点并等待后台代理的启动。调试器允许您逐步完成。
您可以在设备模拟器或设置/ Batarey保护程序中看到后台代理在wp 8.1中工作。