如何确保工作线程在从主线程运行时完成其工作。

编程入门 行业动态 更新时间:2024-10-27 16:25:17
本文介绍了如何确保工作线程在从主线程运行时完成其工作。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 任何人都可以告诉我如何确保从主线程运行的工作线程或后台线程完成其任务。我有一个线程,我通过它轮询某些设备,我想更新UI线程,并将数据保存到数据库。我想确保所有任务都由工作线程完成,然后连续运行UI线程。就像运行用于轮询的UI线程一样,等待工作线程完成其任务并继续轮询。 提前谢谢。

解决方案

您可以等待它或中止它(然后等待)。请参阅: 等待: msdn.microsoft/en-us/library/95hbf2ta.aspx [ ^ ], msdn.microsoft/en-us/library/6b1kkss0.aspx [ ^ ], msdn.microsoft/en-us/library/23f7b1ct.aspx [ ^ ]。 中止: msdn.microsoft/en-us/library/ty8d3wta.aspx [ ^ ], msdn.microsoft/en-us/library/5b50fdsz.aspx [ ^ ]。 许多人认为堕胎是危险的,因此是不可接受的,但这是不正确,只有你很清楚自己在做什么。 Abort与ungraceful TerminateThread 无关,而且异议通常基于它,而不了解它是如何工作的。这是非常重要的机制。它试图在我过去的答案中解释它: 正确关闭dll中的线程 [ ^ ], 从专用线程创建流程的问题 [ ^ ]。
- SA

Hi all, Can anybody tell me how to ensure that the worker thread or the background thread which is running from the main thread completes its task or not. I have a thread through which i am polling certain devices along with this i want to update the UI thread & also save the data to database. I want to ensure that all the tasks are completed by the worker thread & then run UI thread continuously. Like run UI thread for polling wait for worker thread to finish its task & continue to poll. Thanks in advance.

解决方案

You can either wait for it or abort it (and then wait anyway). Please see: Waiting for: msdn.microsoft/en-us/library/95hbf2ta.aspx[^], msdn.microsoft/en-us/library/6b1kkss0.aspx[^], msdn.microsoft/en-us/library/23f7b1ct.aspx[^]. Aborting: msdn.microsoft/en-us/library/ty8d3wta.aspx[^], msdn.microsoft/en-us/library/5b50fdsz.aspx[^]. Many think that abortion is dangerous and hence unacceptable, but this is not true, only if you know very well what you are doing. Abort has nothing to do with ungraceful TerminateThread, and the objections are often based on it, without understanding how it works. This is very non-trivial mechanism. It tried to explain it in my past answers: Close correcly the thread inside a dll[^], Problem with creating Process from dedicated thread[^].
—SA

更多推荐

如何确保工作线程在从主线程运行时完成其工作。

本文发布于:2023-11-26 22:16:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635419.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工作   线程   主线

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!