简体   繁体   中英

WP8 Background Agent Network Request

I'm relatively new to windows phone development, and I'm stuck on this. I'm trying to run a background resourse intensive agent that will make a network request when the app is deactivated. Here are all the important parts of the code..

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

        }
    }

and in the 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;
            }
        }
    }

Now I'm not exactly sure if the background task even begins to run, but the function StartResourceIntensiveAgent() does run as the debug line prints "Starting NetworkRequestTaskAgent. Beyond that I have very little knowledge of what could possibly wrong, my only assumption is, is that even though I named the task agent NetworkRequestTaskAgent it may not be associated with the part in the StartResourceIntensiveAgent function "string resourceIntensiveTaskName = "NetworkRequestTaskAgent"; Any sort of insight would be extremely helpful.

I did not notice the error in the quick view.

You have created an instance of the

resourceIntensiveTask = new ResourceIntensiveTask (resourceIntensiveTaskName);

Activated the background agent in 'ScheduledActionService'. Add ('resourceIntensiveTask');

Put a breakpoint on the 'OnInvoke' and wait for the start of the background agent. The debugger allows you to step through.

You can see that your background agent works in wp 8.1 on the device emulator or in the Setting/Batarey saver.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM