Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 后台代理未执行。:Windows Phone 8_C#_Vb.net_Windows Phone 7_Windows Phone 8_Background Agent - Fatal编程技术网

C# 后台代理未执行。:Windows Phone 8

C# 后台代理未执行。:Windows Phone 8,c#,vb.net,windows-phone-7,windows-phone-8,background-agent,C#,Vb.net,Windows Phone 7,Windows Phone 8,Background Agent,需要解决方案 后台代理只工作一次。在没有出现后台代理之后。它在第一次工作时就可以正常工作,并且在页面打开时就可以正常工作。然而,在那之后,再做一次就要花很长时间。有时页面关闭和打开不起作用。这可能是因为没有删除agenet 我的后台代理代码: #define DEBUG_AGENT using System; using System.Windows; using Microsoft.Phone.Scheduler; using Microsoft.Phone.Shell; using Micr

需要解决方案

后台代理只工作一次。在没有出现后台代理之后。它在第一次工作时就可以正常工作,并且在页面打开时就可以正常工作。然而,在那之后,再做一次就要花很长时间。有时页面关闭和打开不起作用。这可能是因为没有删除agenet

我的后台代理代码:

#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();
        }
 }
}
调用代理的应用程序中的我的页面

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

            }
        }

确保项目已定义调试代理。这是项目属性中的设置。要设置此标志,请执行以下步骤

  • 在VS中的项目上单击鼠标右键,然后选择“属性”
  • 选择Build选项卡
  • 将调试代理添加到“条件编译符号”字段 如果设置了,我发现在LaunchForTest中最好至少给出30秒。有时候,当你告诉它的时候,它并没有很好地安排它