暂停/恢复另一个线程。 [英] Pausing/resuming another thread.

查看:108
本文介绍了暂停/恢复另一个线程。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,



我得到了以下情况:



1.线程T1正在运行一些工作。

2.线程T2检查某些值是否为真/假。如果它是真的 - T2应该暂停T1执行并在值再次为假之后恢复它。



这是一个微不足道的问题,但是......我想暂停的线程(T1) - 是从另一个进程创建的,我不能改变它的代码。虽然我可以得到它的参考或ID。



有什么想法可能是最好的出路吗?



谢谢!



我的尝试:



是考虑AutoResetEvent,但不知道如何使用它,当我不应该改变我想要停止的线程代码...

解决方案

你好,



如需深入了解: .NET Framework中的并行处理和并发 [ ^ ]



快速参考:同步基元概述 [ ^ ]

看看这个 [ ^ ]。



虽然有一个警告。如果您尝试暂停的线程没有写入暂停,那么您可能会在其他进程中导致各种并发问题,而您无法做任何事情。


这是一个解决问题的错误方法。永远不要这样做。



你所做的就是建立一个开关,让你立即停车。不管它的位置或情况如何,请注意你不知道汽车在哪里。

想象一下,汽车在高速公路的左侧线路上或穿过铁路,会发生什么?



陈述问题,可能还有另一种解决方案。


Hey,

I got the following case:

1. Thread T1 is running with some work.
2. Thread T2 is checking if some value is true/false. if it's 'true' - T2 should pause T1 execution and resume it after the value is 'false' again.

This is a trivial question, but ... the thread that I want to pause (T1) - was created from another process and I can not alter it's code. Though I can get it's reference or the ID.

Any thoughts on what could be the best way out?

Thanks!

What I have tried:

Was thinking about AutoResetEvent, but not sure how to use it, when I am not supposed to alter the code of thread I want to stop...

解决方案

Hi,

For in-depth knowledge : Parallel Processing and Concurrency in the .NET Framework[^]

For Quick reference : Overview of Synchronization Primitives[^]


Take a look at this[^].

One warning though. If the thread you're trying to pause isn't written with pausing it in mind you could cause all kinds of concurrency problems in the other process that you're not going to be able to do anything about.


This is a bad solution to your problem. Never do it.

What you do is building a switch that allow you to stop a car on instant. whatever is its position or situation, note that you don't know where is the car.
Imagine the car is on left line on freeway or crossing a railroad, what will happen ?

State the problem, may be there is another solution.


这篇关于暂停/恢复另一个线程。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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