简体   繁体   English

多线程

[英]Multi-threading

The application I'm currently working on performs some I/O or CPU intensive actions (file compression, file transfers, communicating with third party APIs, etc.) that occur when a user presses a 'Send' button. 我当前正在使用的应用程序执行一些I / O或CPU密集型操作(文件压缩,文件传输,与第三方API通信等),这些操作在用户按下“发送”按钮时发生。

I'm currently trying to persuade my employers that we should push these actions out to separate threads inside the main application (we'd need a maximum of two worker threads active at any given time), but my colleague has claimed that: 我目前正在努力说服我的雇主,我们应该将这些操作推送到主应用程序内部的单独线程中(在任何给定时间,我们最多需要两个工作线程处于活动状态),但是我的同事声称:

Any extra processing executed on a low priority thread could affect the usability of the GUI. 在低优先级线程上执行的任何其他处理都可能影响GUI的可用性。

My view was that pushing I/O or CPU intensive activity to worker threads, updating the UI with Invoke calls during progress reporting, is pretty standard practise for handling intensive activity. 我的观点是,将I / O或CPU密集型活动推送到工作线程,在进度报告过程中使用Invoke调用更新UI,是处理密集型活动的相当标准的做法。

Am I incorrect? 我不正确吗? If so, could someone provide an explanation? 如果是这样,有人可以提供解释吗?

EDIT: 编辑:

Thank you for the answers so far. 到目前为止,谢谢您的回答。

I should clarify: the colleague's solution to non-blocking is to spawn a child process containing a timer loop that scans a folder and processes the file compression/transfer activities. 我应该澄清一下:同事的非阻塞解决方案是生成一个包含计时器循环的子进程,该计时器循环扫描文件夹并处理文件压缩/传输活动。 (Note that this doesn't cover the calls to third party APIs - I have no idea what his solution there would be. (请注意,这不包括对第三方API的调用-我不知道他的解决方案会有什么。

The main issue with this approach is that the main application loses all scope on the state of whatever activity., leading to, IMHO, further complexity (his solution to progress reporting is to expose the Windows message pump in both processes and send custom messages between the two processes). 这种方法的主要问题是,主应用程序失去了任何活动状态的所有范围。这导致恕我直言,进一步复杂(他的进度报告解决方案是在两个进程中公开Windows消息泵,并在两个进程之间发送自定义消息这两个过程)。

You are correct. 你是对的。 Background threads are the very essence of keeping the UI active, precisely as you have described, via Invoke operations. 就像您所描述的那样,后台线程是通过Invoke操作保持UI处于活动状态的本质。 Keeping everything on the GUI thread will, eventually clog up the plubming and make the GUI unresponsive. 将所有内容保留在GUI线程上,最终将阻塞运行并使GUI无响应。

The definitive answer would be to implement it as a proof-of-concept and then profile the app to see what sort of potential performance hit may or may not exist. 最终的答案是将其实现为概念验证,然后对应用进行概要分析,以查看可能存在或不存在什么样的潜在性能影响。 Maybe on a 也许在

Having said that - it sounds like rubbish to me. 话虽如此-对我来说听起来像垃圾。 In fact, it's quite the opposite - using additional threads are often the best way to keep the UI responsive. 实际上,情况恰恰相反-使用额外的线程通常是保持UI响应能力的最佳方法。

Especially with things like the Task Parallel Library, it's just not very difficult to basic multi-threading. 尤其是对于任务并行库之类的东西,基本的多线程并不是很困难。

Yes, you are correct. 是的,你是对的。 The general principle is that the thread that's responsible for responding to the user and keeping the user interface up to date (usually referred to as the UI thread) should never be used to perform any lengthy operation. 一般原则是,永远不要使用负责响应用户并保持用户界面最新的线程(通常称为UI线程)执行任何冗长的操作。
As a rule of thumb, anything that could take longer than about 30ms is a candidate for removal from the UI thread. 根据经验,任何可能花费超过30ms的时间都可以从UI线程中删除。 This is a little aggressive — 30ms is about the shortest interval that most people will perceive as being anything other than instantaneous and it's actually slightly less than the interval between successive frames shown on a movie screen. 这有点激进-30ms是大多数人认为不是瞬时的任何东西的最短间隔,实际上比电影屏幕上显示的连续帧之间的间隔稍短。

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

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