简体   繁体   English

后台代理不执行:Windows Phone 8

[英]Background Agent Not Performing.: Windows Phone 8

NEED A SOLUTION 需要一个解决方案

Background agent is working only once. 后台代理仅工作一次。 After There is no occurrence of a background agent. 之后没有出现后台代理。 It works at the first time and it works perfectly as soon as the page opens. 它在第一次工作,一旦页面打开就会完美运行。 however, after that it takes forever and ever to do that again. 然而,在那之后,它需要永远,永远再次这样做。 sometimes page close and open doesn't work. 有时页面关闭和打开不起作用。 that would probably because of not removing the agenet 这可能是因为没有删除agenet

My background Agent Code: 我的背景代理代码:

#define DEBUG_AGENT
using System;
using System.Windows;
using Microsoft.Phone.Scheduler;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Info;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using System.Threading;
using Microsoft.Xna.Framework.Media;
using System.Windows.Input;
using Microsoft.Devices;
using System.IO;
using System.IO.IsolatedStorage;
using System.Windows.Media.Imaging;
using System.Net.Sockets;
using System.Text;
using System.Net;

namespace ScheduledTaskAgent1
{
    public class ScheduledAgent : ScheduledTaskAgent
    {

        private static volatile bool _classInitialized;
        //private DispatcherTimer s;
        Socket _socket = null;
        ManualResetEvent _clientDone = new ManualResetEvent(false);
        const int TIMEOUT_MILLISECONDS = 5000;
        const int MAX_BUFFER_SIZE = 2048;
        double lat = 7.16126666666667;

        static ScheduledAgent()
        {
            // Subscribe to the managed exception handler
            Deployment.Current.Dispatcher.BeginInvoke(delegate
            {
                Application.Current.UnhandledException += UnhandledException;
            });
        }

        /// Code to execute on Unhandled Exceptions
        private static void UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                // An unhandled exception has occurred; break into the debugger
                System.Diagnostics.Debugger.Break();
            }
        }

        protected override void OnInvoke(ScheduledTask task)
        {
            //TODO: Add code to perform your task in background
            string toastTitle = "";

            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            lat += 0.001;
            string snmea = DD2NMEA(lat, 80.44506);

            string dates = DateTime.UtcNow.ToString("ddMMyy");

            string UTCTime = DateTime.UtcNow.ToString("hhmmss") + ".000";

            string s1 = Checksum("$FRCMD,869444005499999,_SendMessage,,0809.67600,N,8050.70360,E,1.0,1.08,3.0,141013,055642.000,1,Button1=1,Button2=0,Switch1=1,Switch2=0,Analog1=4.00,Analog2=5.00,SosButton=0,BatteryLow=0,Text1=Text1,Text2=Text2*00");

            string s = Send("$FRCMD,869444005499999,_SendMessage,," + snmea + ",1.0,1.08,3.0," + dates + "," + UTCTime + ",1,Button1=1,Button2=0,Switch1=1,Switch2=0,Analog1=4.00,Analog2=5.00,SosButton=0,BatteryLow=0,Text1=Text1,Text2=Text2*00");


                 startToastTask(task, toastTitle);

        }

        private void startToastTask(ScheduledTask task, string toastTitle)
        {              

#if DEBUG_AGENT
            ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(10));
#endif

            // Call NotifyComplete to let the system know the agent is done working.
            NotifyComplete();
        }
 }
}

My Page from app which calls the agent 来自应用程序的我的页面调用代理

PeriodicTask toastPeriodicTask;

 const string toastTaskName = "ToastPeriodicAgent";



 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            toastPeriodicTask = ScheduledActionService.Find(toastTaskName) as PeriodicTask;

 StartPeriodicAgent(toastTaskName);
}

 private void StartPeriodicAgent(string taskName)
        {
            toastPeriodicTask = ScheduledActionService.Find(taskName) as PeriodicTask;

            if (toastPeriodicTask != null)
            {
                RemoveAgent(taskName);
            }
            toastPeriodicTask = new PeriodicTask(taskName);

            toastPeriodicTask.Description = periodicTaskDesc;

            try
            {
                ScheduledActionService.Add(toastPeriodicTask);

#if(DEBUG_AGENT)
                ScheduledActionService.LaunchForTest(taskName, TimeSpan.FromSeconds(2));
#endif
            }
            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.");
                }
                else if (exception.Message.Contains("BNS Error: The maximum number of ScheduledActions of this type have already been added."))
                {
                    MessageBox.Show("BNS Error: The maximum number of ScheduledActions of this type have already been added.");
                }
                else
                {
                    MessageBox.Show("An InvalidOperationException occurred.");
                }
            }
            catch (SchedulerServiceException)
            {

            }
        }

Ensure that your project has DEBUG_AGENT defined. 确保您的项目已定义DEBUG_AGENT。 This is a setting within your project properties. 这是项目属性中的设置。 To set this flag, follow these steps 要设置此标志,请按照下列步骤操作

  1. Right click the project within VS and select Properties 右键单击VS中的项目,然后选择“属性”
  2. Select the Build tab 选择“构建”选项卡
  3. Add DEBUG_AGENT to the "Conditional compilation symbols" field. 将DEBUG_AGENT添加到“条件编译符号”字段中。

If that is set, I've found it's best to give at least 30 seconds in the LaunchForTest. 如果设置了,我发现最好在LaunchForTest中给出至少30秒。 Sometimes it doesn't quite schedule it when you tell it to. 有时,当你告诉它时,它并没有完全安排它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Windows Phone 8-从后台代理激活应用程序 - windows phone 8 - activating the app from the background agent Windows Phone后台代理内存限制 - Windows Phone Background Agent Memory Limit Windows Phone Silverlight 8.1后台代理 - Windows Phone Silverlight 8.1 background agent Windows Phone后台代理Microsoft.Phone.dll提交问题 - Windows Phone background agent Microsoft.Phone.dll submission issue 在Windows Phone 8中在没有UI的情况下在后台执行代码(通过自定义URI) - Performing code in background without UI in windows phone 8 (via custom URI) Windows Phone 8.1后台代理分别调用一个自定义方法 - Windows Phone 8.1 Background Agent invokes a custom method severally Windows Phone后台代理ShellTile无法添加 - Windows phone background agent ShellTile can't be added 如何使用隔离存储将数据传递给windows phone中的后台代理? - How to use isolated storage pass data to background agent in windows phone? 如何将Windows Phone 7中后台任务代理的到期时间设置为永不过期? - How to set the expiration of a background task agent in windows phone 7 to never expire? 将数据传递到后台代理,C#Windows Phone - Pass data to background agent, C# windows phone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM