Thread.Suspend() [英] Thread.Suspend()

查看:77
本文介绍了Thread.Suspend()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在我的程序中使用第二个线程做一些长的

计算,而没有锁定UI:


worker = new Thread(new ThreadStart(myClass.Run));

worker.Start();


我希望能够暂停计算,所以我正在使用


worker.Suspend()



worker.Resume()


我没有使用任何共享变量或类似资源需要

被保护/互斥,但编译器告诉我我这两个

命令已经过时/折旧,但它们都仍在运行。


我应该采用更新(更好)的方式,或者我可以使用这些来获得
吗?


谢谢


Andrew

Hi,

I''m using a second thread within my program to do some long
calculations, without locking up the UI:

worker = new Thread(new ThreadStart(myClass.Run));
worker.Start();

I want to be able to pause the calculating, so I''m using

worker.Suspend()
and
worker.Resume()

I''m not using any shared variables or such like resources which need to
be protected/mutex but the compiler is telling me that these two
commands are obsolete/depreciated, yet they both still function.

Is there a newer (better) way I should be doing this, or am I OK to be
using these?

Thanks

Andrew

推荐答案

On Sun,2006年2月19日15:08:57 GMT,Andrew Bullock

< an ********** ***********@ANDntlworldTHIS.com>写道:
On Sun, 19 Feb 2006 15:08:57 GMT, Andrew Bullock
<an*********************@ANDntlworldTHIS.com> wrote:
是否有更新(更好)的方式我应该这样做,或者我可以使用这些吗?
Is there a newer (better) way I should be doing this, or am I OK to be
using these?




你不应该使用它们,因为它们很危险,尽管我不会在这一刻回忆起细节。没有更新的替代方式

,你只需要使用手动同步 - 在工作线程上检查一些

信号量,如果是'那就去睡觉'设置。

-
http:// www。 kynosarges.de


嗨安德鲁,

使用挂起线程通常被认为是个坏主意,因为如果

你在执行中期停止一个线程,你可能遇到死锁和竞争

条件。如果一个线程暂停在一个关键区域内,没有什么可以访问该区域等等。一个更好的方法来处理线程

同步是通过WaitHandles。它们就像是线程的障碍

执行,线程无法通过WaitHandle直到另一个线程

打开句柄允许线程通过。有两种基本的

风格,AutoResetEvent会在单个线程通过后关闭句柄(从信号转到

Unsignalled),以及ManualResetEvent

一旦设置将允许多个线程通过,直到手动重置为止。


这是一个允许工作线程进行一些处理的示例

但是必须等待主线程向它发出信号表明它可以完成

其处理的最后部分:


使用System;

使用System.Collections.Generic;

使用System.Text;

使用System.Threading;


namespace ConsoleApplication18

{

class program

{

static void Main(string [] args)

{

AutoResetEvent waitHandle = new AutoResetEvent(false);


//启动其他线程

工人worker = new Worker();

worker.DoSomeWork(waitHandle);

//让主UI线程进行一些处理

Console.WriteLine(主线程正在工作);

Thread.Sleep(5000) ;


Console.WriteLine(主线程完成工作,让其他线程

继续);

waitHandle.Set ();


Console.ReadLine();

}

}


班工人

{

私人WaitHandle waitHandle;


public void DoSomeWork(WaitHandle waitHandle)

{

this.waitHandle = waitHandle;


线程t =新线程(新的ThreadStart(ProcessValues));

t .Start();

}


private void ProcessValues()

{

//可以随时运行

for(int i = 0;我< 5; i ++)

{

Console.WriteLine(" processing:" + i);

}


//确保我可以继续使用

this.waitHandle.WaitOne();


//只能运行一次线程很高兴

for(int i = 0; i< 5; i ++)

{

Console.WriteLine(" last processing :" + i);

}

}

}

}

使用System;

使用System.Collections.Generic;

使用System.Text;

使用System.Threading;


命名空间ConsoleApplication18

{

class program

{

static void Main(string [] args)

{

AutoResetEvent waitHandle = new AutoResetEvent(false);


//启动其他线程

工人工人=新工人();

worker.DoSomeWork(waitHandle);


//让主UI线程进行一些处理

Console.WriteLine(" Main thr ead working");

Thread.Sleep(5000);


Console.WriteLine(&#主线程完成工作,让其他线程

继续);

waitHandle.Set();


Console.ReadLine();

} < br $>
}


班级工人

{

私人WaitHandle waitHandle;

public void DoSomeWork(WaitHandle waitHandle)

{

this.waitHandle = waitHandle;


线程t = new Thread(new ThreadStart(ProcessValues));

t.Start();

}


private void ProcessValues()

{

//可以随时运行

for(int i = 0;我< 5; i ++)

{

Console.WriteLine(" processing:" + i);

}


//确保我可以继续使用

this.waitHandle.WaitOne();


//只能运行一次线程很高兴

for(int i = 0; i< 5; i ++)

{

Console.WriteLine(" last processing :" + i);

}

}

}

}

希望有所帮助

Mark Dawson


-
http://www.markdawson.org

" Andrew Bullock"写道:
Hi Andrew,
using suspend on a thread is generally considered a bad idea because if
you stop a thread in mid execution you can run into deadlock and race
conditions. What if a thread is suspended inside a critical region, nothing
else will be able to access that region etc. A better way to handle thread
syncronization is through WaitHandles. They act like barriers to thread
execution, threads cannot pass through the WaitHandle until another thread
open the handle allowing the thread to pass through. There are two basic
flavours, AutoResetEvent which will close the handle (go from Signalled to
Unsignalled) once a single thread passes through, and the ManualResetEvent
which once set will allow multiple threads to pass until it is manually reset.

Here is an example where a worker thread is allowed to do some processing
but then must wait for the main Thread to signal to it that it can complete
the final part of its processing:

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace ConsoleApplication18
{
class Program
{
static void Main(string[] args)
{
AutoResetEvent waitHandle = new AutoResetEvent(false);

//start other thread
Worker worker = new Worker();
worker.DoSomeWork(waitHandle);

//let main UI thread so some processing
Console.WriteLine("Main thread working");
Thread.Sleep(5000);

Console.WriteLine("Main thread finished work, let other thread
continue");
waitHandle.Set();

Console.ReadLine();
}
}

class Worker
{
private WaitHandle waitHandle;

public void DoSomeWork(WaitHandle waitHandle)
{
this.waitHandle = waitHandle;

Thread t = new Thread(new ThreadStart(ProcessValues));
t.Start();
}

private void ProcessValues()
{
//Can run this anytime
for (int i = 0; i < 5; i++)
{
Console.WriteLine("processing:" + i);
}

//make sure I am allowed to keep going
this.waitHandle.WaitOne();

//Can only run this once main thread is happy
for (int i = 0; i < 5; i++)
{
Console.WriteLine("last processing:" + i);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace ConsoleApplication18
{
class Program
{
static void Main(string[] args)
{
AutoResetEvent waitHandle = new AutoResetEvent(false);

//start other thread
Worker worker = new Worker();
worker.DoSomeWork(waitHandle);

//let main UI thread so some processing
Console.WriteLine("Main thread working");
Thread.Sleep(5000);

Console.WriteLine("Main thread finished work, let other thread
continue");
waitHandle.Set();

Console.ReadLine();
}
}

class Worker
{
private WaitHandle waitHandle;

public void DoSomeWork(WaitHandle waitHandle)
{
this.waitHandle = waitHandle;

Thread t = new Thread(new ThreadStart(ProcessValues));
t.Start();
}

private void ProcessValues()
{
//Can run this anytime
for (int i = 0; i < 5; i++)
{
Console.WriteLine("processing:" + i);
}

//make sure I am allowed to keep going
this.waitHandle.WaitOne();

//Can only run this once main thread is happy
for (int i = 0; i < 5; i++)
{
Console.WriteLine("last processing:" + i);
}
}
}
}

Hope that helps
Mark Dawson

--
http://www.markdawson.org
"Andrew Bullock" wrote:


我正在我的程序中使用第二个线程进行一些长时间的计算,而不会锁定UI :

worker = new Thread(新的ThreadStart(myClass.Run));
worker.Start();

我希望能够暂停计算所以我正在使用

worker.Suspend()

worker.Resume()

我没有使用任何共享变量或类似资源需要受保护/互斥,但编译器告诉我这两个命令已经过时/折旧,但它们都仍在运行。

有更新(更好)的方式我应该这样做,或者我可以使用这些吗?

谢谢

Andrew
Hi,

I''m using a second thread within my program to do some long
calculations, without locking up the UI:

worker = new Thread(new ThreadStart(myClass.Run));
worker.Start();

I want to be able to pause the calculating, so I''m using

worker.Suspend()
and
worker.Resume()

I''m not using any shared variables or such like resources which need to
be protected/mutex but the compiler is telling me that these two
commands are obsolete/depreciated, yet they both still function.

Is there a newer (better) way I should be doing this, or am I OK to be
using these?

Thanks

Andrew



总是一个好主意,试着知道你为什么不想做什么,而不仅仅是接受一揽子陈述,否则怎么做

你知道什么时候不去做到这一点。不调查这些事情将导致

评论如投掷例外是昂贵的,不要扔掉它们或者

永远不要连接字符串,总是使用字符串生成器 :-)


Mark。

-
http://www.markdawson.org

" Christoph Nahr"写道:
Its always a good idea to try to know why you are not suppose to do
something, rather than just accepting a blanket statement, otherwise how do
you know when not to do it. Not looking into these things will lead to
comments like "Throwing Exceptions are expensive, don''t ever throw them" or
"Never concatenate strings, always use a string builder" :-)

Mark.
--
http://www.markdawson.org
"Christoph Nahr" wrote:
On Sun,2006年2月19日15:08:57 GMT,Andrew Bullock
< an ************** *******@ANDntlworldTHIS.com>写道:
On Sun, 19 Feb 2006 15:08:57 GMT, Andrew Bullock
<an*********************@ANDntlworldTHIS.com> wrote:
是否有更新(更好)的方式我应该这样做,或者我可以使用这些吗?
Is there a newer (better) way I should be doing this, or am I OK to be
using these?


<你不应该使用它们因为它们很危险,尽管我现在还没有回忆起细节。也没有更新的替代方式
,你只需要使用手动同步 - 在工作线程上检查一些
信号量,如果已设置则进入休眠状态。
-
http://www.kynosarges.de



这篇关于Thread.Suspend()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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