如何在C#中设置线程优先级 [英] how to set thread priority in c#

查看:697
本文介绍了如何在C#中设置线程优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在c#.net 4的backgroundworker_dowork()事件中调用了三种方法.我想同步执行三种方法.也就是说,第二种方法将在第一种方法完全执行后执行,而第三种方法将在第二种方法完全执行后执行.因为只有在第一种方法完全执行后,第二种方法才能正确释放.但是在后台工作器中,这三种方法都是我不希望异步执行的.

如何解决?

在此先感谢.

there are three methods called in a backgroundworker_dowork() event in c# .net 4. I want to execute three methods synchronously. that is, 2nd method will execute after 1st method is fully executed and 3rd method will execute when 2nd method is fully executed. because 2nd method can exetue properly only when 1st method is fully executed. but in the background worker all three methods are executed asynchronously that i don''t want.

How to solve it?

Thanks in advance.

推荐答案

描述得不太好.

但是,您是说所有三个方法都在同时执行?恭喜你!您编写了代码来启动三个不同的BackgroundWorkers或生成了自己的线程来做!

您需要做的就是创建一个BackgroundWorker并执行以下方法:
This isn''t described very well.

But, you''re saying that all three methods are executing at the same time?? Congratulations! You wrote your code to launch three different BackgroundWorkers or spawned your own threads to do it!

All you would have to do is create ONE BackgroundWorker and have it execute a method like this:
Private Sub StartWork()
    Method1()
    Method2()
    Method3()
End Sub


每个方法将按顺序执行,并且每个方法都将等待前一个方法执行完毕,然后再开始下一个方法.


Each method will get executed in order and each will wait for the previous method to finish execution before the next one starts.


除了解决方案1和解决方案3之外.

当然,您始终可以更改线程的优先级:
http://msdn.microsoft.com/en-us/library/system. threading.thread.priority.aspx [ ^ ].

您还可以更改进程的优先级.问题是:在几乎所有情况下,您都不应这样做.您几乎无济于事,但很容易使事情变得糟糕或非常糟糕.

您在错误的方向上进行思考:试图通过时间来影响功能.您应该做相反的事情:功能永远不应该依赖于任何线程中的时间延迟.实际上,测试中有一个方向就是要做到这一点:以系统的方式在线程中植入不同的延迟,并测试它不会影响功能.如果达不到这样的容忍度,则认为这是一个错误(也是该死的坏人),称为竞赛条件.

很多年前,当我学习它时,我以更明确的名称知道它:对执行时间的不正确依赖".请参阅:
http://en.wikipedia.org/wiki/Race_condition [
In addition to Solution 1 and Solution 3.

Of course, you can always change the priority of a thread:
http://msdn.microsoft.com/en-us/library/system.threading.thread.priority.aspx[^].

You can also change the priority of a process. The thing is: in almost all cases, you should not do it. You hardly can help anything but you can easily make things bad or very bad.

You are thinking in wrong direction: trying to affect functionality by the timing. You should do directly opposite: the functionality should never depend on time delays in any threads. In fact, there is a direction in testing which does exactly that: seeds different delays in threads in systematic ways and tests that it does not affect functionality. If such tolerance is not achieved, this is considered to be a bug (and a damn bad one), called race condition.

When I learn it many years ago, I knew it under more defining name: "incorrect dependency on the time of execution". Please see:
http://en.wikipedia.org/wiki/Race_condition[^].

—SA


将它们全部放在同一线程中.
Put them all in the same thread.


这篇关于如何在C#中设置线程优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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