简体   繁体   English

如何在 UWP 后台应用程序中同时运行 2 个任务?

[英]How to make 2 tasks run simultaneously in a UWP background app?

I need help running my UWP background app deployed on my Raspberry Pi (hosting Windows 10 IOT Core).我需要帮助来运行部署在我的 Raspberry Pi(托管 Windows 10 IOT Core)上的 UWP 后台应用程序。

It's almost working but I got a tricky issue... Let me explain:它几乎可以正常工作,但我遇到了一个棘手的问题......让我解释一下:

The app has 2 functionalities:该应用程序有2个功能:

  • A Web Server that receives http requests and do some processing according to parameters.一个接收http请求并根据参数做一些处理的Web Server。 (based on this link ) (基于此链接
  • A repeating task that is executed every X minutes (X varies from 10 to 60).每 X 分钟执行一次重复任务(X 从 10 到 60)。

Both functionalities work well if executed seperately but I need them to run simultaneously.如果单独执行,这两个功能都可以很好地工作,但我需要它们同时运行。 I'm rather new to UWP & Raspberry so maybe there's something I'm missing...我对 UWP 和 Raspberry 比较陌生,所以也许我遗漏了一些东西......

  • Is it a matter of thread conflict?是线程冲突的问题吗?
  • Is a background task on a raspberry limited to a single thread (so the first/last process executed wins)?树莓上的后台任务是否仅限于单个线程(因此执行的第一个/最后一个进程获胜)?
  • Do all classes need to be "sealed" (microsofts's doc says so but not the example they provide)?是否所有类都需要“密封”(microsofts 的文档是这样说的,但不是他们提供的示例)?

I tought about splitting the project into two independant deployable tasks and I believe it would work but I need the webserver task to control (START/PAUSE/STOP) the repeating task.我想将项目分成两个独立的可部署任务,我相信它会起作用,但我需要网络服务器任务来控制(开始/暂停/停止)重复任务。

I think communication between them could be feasable (using this way ) but I'm looking for the simplest/fastest solution here.我认为他们之间的沟通可能是可行的(使用这种方式),但我正在寻找最简单/最快的解决方案。

Here's the stripped down code (the 3 classes are in separate files):这是精简后的代码(这 3 个类位于单独的文件中):

App class:应用类:

public sealed class StartupTask : IBackgroundTask
{
    private static BackgroundTaskDeferral _Deferral = null;

    public async void Run(IBackgroundTaskInstance taskInstance)
    {
        _Deferral = taskInstance.GetDeferral();

        var webserver = new WebServer();
        await ThreadPool.RunAsync(workItem => { webserver.Start(); });

        StartRepeatingTask();
    }
}

Web server class:网络服务器类:

internal class WebServer
{
    private const uint BUFFER_SIZE = 8192;

    public async void Start()
    {
        var listener = new StreamSocketListener();
        await listener.BindServiceNameAsync("1537");

        listener.ConnectionReceived += async (sender, args) =>
        {
            // Do some stuff
            ...
            Processing.StopProcess();
        }
    }
}

Processing class加工类

public class Processing 
{
    private static Task myTask = null;
    private static CancellationTokenSource taskCancellation = new CancellationTokenSource();

    public static void StartRepeatingTask()
    {
        taskCancellation = new CancellationTokenSource();
        myTask = Task.Run(() => AutoProcess(), taskCancellation.Token);
    }

    public static void AutoProcess()
    {
        Process();

        myTask = Task.Delay(GetDelayToUse(DELAY_SET_LIST))
            .ContinueWith(t => AutoProcess(), taskCancellation.Token);
    }

    public static void Process()
    {
        // Do some other stuff
    }

    public static void StopProcess()
    {
        taskCancellation.Cancel();
    }
}

The two task can run in a background app simultaneously.这两个任务可以同时在后台应用程序中运行。

Is it a matter of thread conflict?是线程冲突的问题吗?

If there are no commitications or shared data between the two task, there will be no confilct.如果两个任务之间没有提交或共享数据,则不会有冲突。 The important thing is that you need to pay attention to the synchronization of data.重要的是你需要注意数据的同步。 If you use a list or queue to share the data between the two threads, you should use the concurrency object.如果使用列表或队列在两个线程之间共享数据,则应使用并发对象。 Please refer to ConcurrentQueue example, ConcurrentQueue represents a thread-safe.请参考ConcurrentQueue示例,ConcurrentQueue 代表线程安全。

Is a background task on a raspberry limited to a single thread (so the first/last process executed wins)?树莓上的后台任务是否仅限于单个线程(因此执行的第一个/最后一个进程获胜)?

Once a background applications are deployed and configured, these applications launch at machine startup and run continuously without any process lifetime management resource use limitations.一旦部署和配置了后台应用程序,这些应用程序将在机器启动时启动并持续运行,而没有任何进程生命周期管理资源使用限制。 There are some limitations about background task, but it is not limited to a single thread.后台任务有一些限制,但不限于单线程。 More information here .更多信息在这里 Here you need to differentiate the background application on Windows IoT Core and task.这里需要区分 Windows IoT Core 上的后台应用程序和任务。

Do all classes need to be "sealed" (microsofts's doc says so but not the example they provide)?是否所有类都需要“密封”(microsofts 的文档是这样说的,但不是他们提供的示例)?

Yes, the background task class itself—and all other classes in the background task project—need to be public classes that are sealed (or final).是的,后台任务类本身——以及后台任务项目中的所有其他类——需要是密封(或最终)的公共类。 It applies to the background tasks which inherited from interface IBackgroundTask , but not the classes are using for normal thread.它适用于从接口IBackgroundTask继承的后台任务,但不适用于普通线程使用的类。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM