设计:任务的异步模式(TAP用的await /异步),VS螺纹带信号VS其他线程结构 [英] Design: Task-Asynchronous Pattern (TAP with await / async), vs threads with signalling vs other thread structures

查看:207
本文介绍了设计:任务的异步模式(TAP用的await /异步),VS螺纹带信号VS其他线程结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帮助与下面的C#程序的重新设计想法将不胜AP preciated。我试图实现使用1)TAP多线程之间挑选,2)包含3)使用信号,而不是这些的bool相同的线程时,他们的bool被设置为false终止,或微调粗粒度线程。我将解释下面的程序,使清晰的情况下。

Help with ideas for redesign of the below C# program would be greatly appreciated. I am trying to pick between implementing multithreading using 1) TAP, 2) course-grained threads that contain spinners that terminate when their bools are set to false, or 3) the same threads using signalling instead of these bools. I will explain the program below, to make the case clear.

该计划

该计划是在C#中的游戏自动化应用,我发展作为一个有趣的方式来学习语言和C#(5.0)功能更好。它有需求,同时应用程序运行保持响应的UI。

The program is a game automation application in C# that I am developing as a fun way to learn the language and C# (5.0) features better. It has a UI that needs to remain responsive while the app runs.

在UI中的一个特定的标签被打开时,程序会调用一个名为扫描新的线程,在另一个类的新方法,利用扫描各种存储位置和更新标签与这些迅速变化值的UI该UI的SynchronizationContext的。这正好在一段时间(扫描)循环,只要扫描布尔为真(通常是充分的程序生命持续时间)。

When a particular tab in the UI is opened, the program fires up a new thread called "Scan" that, in a new method in another class, scans various memory locations and updates labels in the UI with these quickly changing values using the UI's SynchronizationContext. This goes on in a while(scanning) loop, for as long as scanning bool is true (usually the full life-duration of the program).

当用户单击该选项卡,程序大火了两个新的线程执行以下操作上的开始按钮:线程运行移动字符围绕以下特定路径。螺纹操作命中特定按键和作为玩家运行路径执行同时动作。如果出现一定的情况下,程序应该停止正在运行的线程和线程的行动暂时运行的方法,当它完成,回到运行和action'ing。

When the user clicks the Start button on this tab, the program fires up two new threads that does the following: Thread "Run" moves the character around following a particular path. Thread "Action" hits particular buttons and performs actions at the same time as the player runs the path. If a certain scenario occurs, the program should stop the running thread and the action thread temporarily, run a method, and when it finishes, go back to the running and action'ing.

当用户单击该选项卡上的停止按钮,自动停止应该和线程终止。

When the user clicks the Stop button on this tab, the automation should halt and threads terminate.

挑战

我已经在每一个需要的各项工作的关心线程使用连续微调循环创造了一个工作版本。纱厂使用一段时间(myBool)运行。对于这三个线程的布尔变量是:扫描,运行和actioning

I have already created a working version using continuous spinner loops in each thread that takes care of the various work. The spinners run using a while(myBool). For the three threads the bools are: scanning, running and actioning.

当我想阻止一个线程在我布尔设置为false,并使用的Thread.join等待线程,然后再继续正常终止。该线程可以,如前所述,可以由用户点击停止按钮,或由程序自动作为其部分功能停止。在后一种情况下一个线程被停止,加入,然后在稍后的阶段重新启动。

When I want to stop a thread I set the bool to false, and use a Thread.Join to wait for the thread to terminate gracefully before proceeding. The threads can, as mentioned, be stopped by the user clicking the Stop button, or automatically by the program as part of its functionality. In the latter case a thread is stopped, Joined, and then at a later stage restarted.

已经做了大量的阅读和研究的线程和在C#5.0新的异步编程工具后,我意识到,我目前做的方式可能是非常笨拙的和不专业。它创造了大量的同步/线程安全问题,而作为这一切的目的是更多地了解C#我想获得我是否应该将其更改为细粒度 - 异步编程方法,而不是你拿,采用TAP与异步和等待为宜。

After having done a lot of reading and research on threading and the new async programming tools in C# 5.0, I have realized that the way I am currently doing it might be very clumsy and unprofessional. It creates lots of synchronization/thread-safety issues, and as the goal of all of this is to learn more about C# I wanted to get your take on whether I should change it to a fine-grained asynchrounous programming approach instead, using TAP with async and await as appropriate.

这听起来像一个情况下与取消标记的任务可能是有用的?线程是毕竟长时间运行的操作,所以我很担心,使用线程池(Task.Run)将导致线程池(超额认购)不良卫生。如果异步编程似乎是一个不好的比赛在这里,怎么样使用线程,因为我已经做了,而是使用信号来启动和停止线程?

Does this sound like a case where Tasks with cancellation tokens could be useful? The threads are after all long-running operations, so I was concerned that using the thread pool (Task.Run) would cause bad hygiene in the thread pool (over-subscription). If async programming seems like a bad match here, what about using threads as I have done, but instead use signalling to start and stop the threads?

有什么想法大大AP preciated。

Any thoughts greatly appreciated.

推荐答案

没有。 TPL被设计为运行更短的任务,其中的新主题所有的时间分配会伤害更流畅。它得到了相当不错的功能,如工作队列和工作窃取(TPL一个线程可以从另一个线程任务)。当然,它可以有更长的运行的任务,但你不会得到从这么多的好处。在contrarary,将强制TPL分配新的线程。

No. TPL was designed to run shorter tasks where the allocation of new threads all time would hurt perfomance. It got quite nice features like job queues and work stealing (a TPL thread can take jobs from another thread). It can of course have longer running task, but you wont get so many benefits from that. On the contrarary, you force TPL to allocate new threads.

然而,问题是,我们需要与贵公司实际执行更多的信息,知道你应该使用什么样的感觉有点一般。为扫描线这是相当明显的,它应该在一个单一的线程中运行。

However, the question is a bit general in the sense that we need more information about your actual implementation to know what you should use. For the Scan thread it's quite obvious that it should run in a single thread.

但对于其他人很难知道。他们做工作,所有的时间或定期?如果他们做的工作的所有时间,你应该让他们在单独的线程。

But for the others it's hard to know. Do they do work all the time or periodically? If they do work all the time you should keep them in seperate threads.

至于线程同步化有另一种选择。你可以使用 ConcurrentQueue 来排队有要绘制的一切。这样,你就不需要任何同步。只是让UI线程检查队列,并在它画任何东西,而生产商可以继续做自己的工作。

As for the thread syncronization there is another alternative. You could use a ConcurrentQueue to queue up everything that has to be drawn. In that way you do not need any synchronization. Just let the UI thread check the queue and draw anything in it, while the producers can continue to do their work.

在事实上,这样你可以将任何不相关的UI绘图到其他线程。这也应提高在应用程序的响应能力。

In fact, in that way you can move anything not related to UI drawing to other threads. That should also improve the responsiveness in your application.

public void ActionRunnerThreadFunc()
{
    _drawQueue.Enqueue(new SpaceShipRenderer(x, y));
}

public void UIThreadFunc()
{
    IItemRender item;
    if (_drawQueue.TryDequeue(out item))
        item.Draw(drawContext);
}

这篇关于设计:任务的异步模式(TAP用的await /异步),VS螺纹带信号VS其他线程结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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