1

我有一个后台代理,我想在 Mango 中执行它以更新动态磁贴。问题是它永远不会被执行。这是我使用的代码:

//start background agent 
PeriodicTask periodicTask = new PeriodicTask("BruceWpAgent");

periodicTask.Description = "BruceWp periodic live task";
periodicTask.ExpirationTime = System.DateTime.Now.AddDays(10);

// If the agent is already registered with the system,
if (ScheduledActionService.Find(periodicTask.Name) != null)
{
     ScheduledActionService.Remove("BruceWpAgent");
}

ScheduledActionService.Add(periodicTask);

我在使用后台作业但从未调用该任务的应用程序之间找到了我的应用程序名称。我究竟做错了什么?

4

2 回答 2

2

这段代码可以帮助你..

string periodicTaskName = "PeriodicAgent";      
    public bool agentsAreEnabled = true;
    private void StartBackgroundAgent()
    {
        // Variable for tracking enabled status of background agents for this app.
        agentsAreEnabled = true;
        // Obtain a reference to the period task, if one exists
        periodicTask = ScheduledActionService.Find(periodicTaskName) as PeriodicTask;

        // 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 (periodicTask != null)
        {
            RemoveAgent(periodicTaskName);
        }

        periodicTask = new PeriodicTask(periodicTaskName);

        // 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.
        periodicTask.Description = "Task to update the Economic times tile.";

        // Place the call to Add in a try block in case the user has disabled agents
        try
        {
            ScheduledActionService.Add(periodicTask);
            // If debugging is enabled, use LaunchForTest to launch the agent in one minute.

            ScheduledActionService.LaunchForTest(periodicTaskName, TimeSpan.FromMinutes(2));

        }
        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.");
                agentsAreEnabled = false;
            }
        }
    }
于 2011-12-13T04:31:19.033 回答
1

查看此动手实验室,了解在 Windows Phone 7.5 中向您的应用程序添加多任务处理,它应该涵盖它。

于 2011-12-01T11:56:32.140 回答