我是 Windows Phone 开发的新手,我一直坚持这一点。我正在尝试运行一个后台资源密集型代理,该代理将在应用程序停用时发出网络请求。这是代码的所有重要部分..
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() 确实在调试行打印“Starting NetworkRequestTaskAgent”时运行。除此之外,我对可能出现的问题知之甚少,我的唯一的假设是,即使我将任务代理命名为 NetworkRequestTaskAgent,它也可能与 StartResourceIntensiveAgent 函数“string resourceIntensiveTaskName =”NetworkRequestTaskAgent“中的部分无关任何形式的见解都会非常有帮助。
最佳答案
我没有注意到快速查看中的错误。
你已经创建了一个实例
resourceIntensiveTask = new ResourceIntensiveTask (resourceIntensiveTaskName);
在“ScheduledActionService”中激活后台代理。添加 ('resourceIntensiveTask');
在'OnInvoke' 上放置一个断点并等待后台代理的启动。调试器允许您单步执行。
您可以在设备模拟器或设置/Batarey 保护程序中看到您的后台代理在 wp 8.1 中工作。
关于c# - WP8后台代理网络请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23682761/