Xamarin.Forms背景线程 [英] Xamarin.Forms Background Thread

查看:122
本文介绍了Xamarin.Forms背景线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行一个非常简单的应用程序,该应用程序正在运行Google任务列表. 我有一个SQLite本地数据库,我想同步/尝试与Google服务器定期同步.

I'm trying to get a really simple application running listing google Task. I have a SQLite local db I'd like to sync / try syncing at regular interval with Google's server.

我当时想只运行一个简单的后台线程,以便在所有设备上都能轻松工作.

I was thinking of simply running a simple background thread so it'd work easily on all devices.

问题归结为在PCL项目(​​Xamarin.Forms)中创建该后台线程 我似乎找不到合适的制作方法.

Problem comes down creating that background thread in a PCL project (Xamarin.Forms) I can't seem to find a proper way of making one.

目前,我只有第二个线程正在运行:

Currently I just have a second thread running as such:

    private bool Sync() { //Does stuffs }

    private void SynchronizeData()
    {
        // Repeats sync process every 35s
        Device.StartTimer (new TimeSpan (0, 0, 35), Sync);
    }

    // Is called pretty much when app starts.
    private void StartSynchronizationThread()
    {
        Task.Factory.StartNew(SynchronizeData, 
            CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
    }

尽管考虑到操作计数确实很低(尽管这是测试的最坏情况,它少于一秒的阻塞),但是无论如何这不是什么大问题,但是我在第一次运行线程时遇到了应用程序的大量冻结. (计时器第一次到期) 冻结持续了好一分钟...

While it should anyway not be much of a problem considering operation count is really low (worst case for testing purpose it'd be less than a sec blocking) i experience a massive freeze of my application around when thread should be ran first. (Timer expiring for the first time) The freeze last for a good minute...

一旦停顿,我几乎可以永远留在该应用程序上,而不会遇到任何问题.我注意到以下内容很晚才出现:

Once it hapenned, i can stay on the app mostly forever without a single issue. I noticed the following appears quite later though:

[艺术] 线程[2,tid = 2532,WaitingInMainSignalCatcherLoop,Thread * = 0xee170800,peer = 0x12c00080,"Signal 麦田守望者》 [Catcher]:对信号3做出反应[art]将堆栈跟踪写入 '/data/anr/traces.txt'[art]将堆栈跟踪写入 '/data/anr/traces.txt'[艺术] 线程[2,tid = 2532,WaitingInMainSignalCatcherLoop,Thread * = 0xee170800,peer = 0x12c00080,"Signal 麦田守望者》 [Catcher]:对信号3做出反应[art]将堆栈跟踪写入 '/data/anr/traces.txt'

[art] Thread[2,tid=2532,WaitingInMainSignalCatcherLoop,Thread*=0xee170800,peer=0x12c00080,"Signal Catcher"]: reacting to signal 3 [art] Wrote stack traces to '/data/anr/traces.txt' [art] Wrote stack traces to '/data/anr/traces.txt' [art] Thread[2,tid=2532,WaitingInMainSignalCatcherLoop,Thread*=0xee170800,peer=0x12c00080,"Signal Catcher"]: reacting to signal 3 [art] Wrote stack traces to '/data/anr/traces.txt'

尽管踪迹并没有真正给我更多信息... 但这可能与它有关.

Traces aren't really giving me much more informations though... But it probably have to do with it.

有人暗示这件事吗?包,代码示例或其他内容? 希望我错过了一些大的东西;

As anyone a hint regarding this matter ? Package, code example or anything ? Hopefully I missed out on something big e;

谢谢.

推荐答案

在实际设备上像超级按钮一样工作,该问题与仿真器有关. (虽然还没有找到解决办法)

Working like a charm on an actual device, the issue was linked to the emulator. (Haven't found a fix for it though)

这篇关于Xamarin.Forms背景线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆