首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WP8后台代理网络请求

WP8后台代理网络请求
EN

Stack Overflow用户
提问于 2014-05-15 23:35:09
回答 1查看 181关注 0票数 3

对于windows phone开发,我还是个新手,对此我束手无策。我正在尝试运行一个后台资源密集型代理,该代理将在应用程序停用时发出网络请求。下面是代码的所有重要部分。

代码语言:javascript
复制
 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中

代码语言:javascript
复制
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“中的部分没有关联;任何类型的洞察都将非常有帮助。

EN

回答 1

Stack Overflow用户

发布于 2015-03-24 12:51:48

我没有注意到快速视图中的错误。

您已经创建了

代码语言:javascript
复制
resourceIntensiveTask = new ResourceIntensiveTask (resourceIntensiveTaskName);

已激活'ScheduledActionService‘中的后台代理。Add ('resourceIntensiveTask');

在'OnInvoke‘上设置一个断点,等待后台代理的启动。调试器允许您单步执行。

您可以在设备仿真器或设置/Batarey保存程序中看到您的后台代理在wp8.1中工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23682761

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档