WIndows phone 8.1中的后台定时器功能
Background timer function in WIndows phone 8.1
我正在开发 windows phone 8.1 应用程序。我有在前台完美运行的计时器功能。当紧急按钮打开时,我需要每 30 秒发送一次报告。
这是我的计时器 Class,
public void Start_timer()
{
DispatcherTimer timer = new DispatcherTimer();
timer.Tick += timer_Tick;
timer.Interval = new TimeSpan(00, 0, 30);
bool enabled = timer.IsEnabled;
timer.Start();
}
async void timer_Tick(object sender, object e)
{
// string which collects data.
URL_log_string = device_id_string + "," + UTCdatetime + "," + lng + "," + lat + "," + speed + "," + heading + "," + alt + "," + "," + battery_level + "," + accuracy + "," + 1 + "," + "," + 0;
//Locally log storage
Log_Class_Object.Save_Data_in_Log("Panic Sent ", URL_log_string);
URL_log_string = "";
}
我的后台Class是执行后台任务
class Task_Class
{
public sealed class ExampleBackgroundTask : IBackgroundTask
{
public async void Run(IBackgroundTaskInstance taskInstance)
{
BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();
//
_deferral.Complete();
}
public static async Task<BackgroundTaskRegistration> RegisterBackgroundTask(String taskEntryPoint, String name, IBackgroundTrigger trigger, IBackgroundCondition condition)
{
foreach (var task in BackgroundTaskRegistration.AllTasks)
{
task.Value.Unregister(true);
}
await BackgroundExecutionManager.RequestAccessAsync();
var builder = new BackgroundTaskBuilder();
builder.Name = name;
builder.TaskEntryPoint = taskEntryPoint;
builder.SetTrigger(new TimeTrigger(15, false));
//var ret = builder.Register();
BackgroundTaskRegistration related_task = builder.Register();
return related_task;
}
}
}
这是我的 xmlmanifest 屏幕截图。
我不知道如何在后台调用此计时器 class。在这方面的合作将受到高度赞赏。
您不能在后台任务中使用 DispatcherTimer
使用 ThreadPoolTimer
TimerElapsedHandler handler=function;
ThreadPoolTimer.CreatePeriodicTimer(handler, TimeSpan.FromSeconds(30));
async private void function(ThreadPoolTimer timer)
{
URL_log_string = device_id_string + "," + UTCdatetime + "," + lng + "," + lat + "," + speed + "," + heading + "," + alt + "," + "," + battery_level + "," + accuracy + "," + 1 + "," + "," + 0;
//Locally log storage
Log_Class_Object.Save_Data_in_Log("Panic Sent ", URL_log_string);
URL_log_string = "";
}
我正在开发 windows phone 8.1 应用程序。我有在前台完美运行的计时器功能。当紧急按钮打开时,我需要每 30 秒发送一次报告。
这是我的计时器 Class,
public void Start_timer()
{
DispatcherTimer timer = new DispatcherTimer();
timer.Tick += timer_Tick;
timer.Interval = new TimeSpan(00, 0, 30);
bool enabled = timer.IsEnabled;
timer.Start();
}
async void timer_Tick(object sender, object e)
{
// string which collects data.
URL_log_string = device_id_string + "," + UTCdatetime + "," + lng + "," + lat + "," + speed + "," + heading + "," + alt + "," + "," + battery_level + "," + accuracy + "," + 1 + "," + "," + 0;
//Locally log storage
Log_Class_Object.Save_Data_in_Log("Panic Sent ", URL_log_string);
URL_log_string = "";
}
我的后台Class是执行后台任务
class Task_Class
{
public sealed class ExampleBackgroundTask : IBackgroundTask
{
public async void Run(IBackgroundTaskInstance taskInstance)
{
BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();
//
_deferral.Complete();
}
public static async Task<BackgroundTaskRegistration> RegisterBackgroundTask(String taskEntryPoint, String name, IBackgroundTrigger trigger, IBackgroundCondition condition)
{
foreach (var task in BackgroundTaskRegistration.AllTasks)
{
task.Value.Unregister(true);
}
await BackgroundExecutionManager.RequestAccessAsync();
var builder = new BackgroundTaskBuilder();
builder.Name = name;
builder.TaskEntryPoint = taskEntryPoint;
builder.SetTrigger(new TimeTrigger(15, false));
//var ret = builder.Register();
BackgroundTaskRegistration related_task = builder.Register();
return related_task;
}
}
}
这是我的 xmlmanifest 屏幕截图。
我不知道如何在后台调用此计时器 class。在这方面的合作将受到高度赞赏。
您不能在后台任务中使用 DispatcherTimer
使用 ThreadPoolTimer
TimerElapsedHandler handler=function;
ThreadPoolTimer.CreatePeriodicTimer(handler, TimeSpan.FromSeconds(30));
async private void function(ThreadPoolTimer timer)
{
URL_log_string = device_id_string + "," + UTCdatetime + "," + lng + "," + lat + "," + speed + "," + heading + "," + alt + "," + "," + battery_level + "," + accuracy + "," + 1 + "," + "," + 0;
//Locally log storage
Log_Class_Object.Save_Data_in_Log("Panic Sent ", URL_log_string);
URL_log_string = "";
}