.NET线程问题 [英] .NET Threading Question

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

问题描述

此代码有什么问题。所有我想测试的是3个进度条和

一个按钮。在buttonclick上我创建3个线程,每个线程调用一个方法

,然后更新进度条并且它可以工作。我想知道是否可以使用这个



谢谢

private void button1_Click(object sender,System.EventArgs e)


{


ThreadStart job = new ThreadStart(onemethod);


Thread thread = new Thread(job );


thread.Start();


ThreadStart job1 =新的ThreadStart(secondmethod);


线程thread1 =新线程(job1);


thread1.Start();


ThreadStart job2 =新的ThreadStart(thirdmethod) ;


线程thread2 =新线程(job2);


thread2.Start();


}


public void onemethod()

{


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


{


progressBar1.Value = i;


Thread.Sleep(100);


}


}


public void secondmethod( )


{


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


{


progressBar2.Value = i;


Thread.Sleep(100);


}


}


public void thirdmethod()


{


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


{


progressBar3.Value = i;


Thread.Sleep(100);


}


}

What is wrong with this code. All i am trying to test is 3 progressbar and
one button. On buttonclick i create 3 threads and each thread calls a method
which in turn updates the progressbar and it works. I would to know if this
can be used.
Thanks
private void button1_Click(object sender, System.EventArgs e)

{

ThreadStart job = new ThreadStart(onemethod);

Thread thread = new Thread(job);

thread.Start();

ThreadStart job1 = new ThreadStart(secondmethod);

Thread thread1 = new Thread(job1);

thread1.Start();

ThreadStart job2 = new ThreadStart(thirdmethod);

Thread thread2 = new Thread(job2);

thread2.Start();

}

public void onemethod()

{

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

{

progressBar1.Value = i;

Thread.Sleep(100);

}

}

public void secondmethod()

{

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

{

progressBar2.Value = i;

Thread.Sleep(100);

}

}

public void thirdmethod()

{

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

{

progressBar3.Value = i;

Thread.Sleep(100);

}

}

推荐答案

嗨Arun,


代码应该可以正常工作,尽管它违反了最佳实践通过

从主要

线程以外的线程直接与UI进行交互。除此之外,它*看起来*它应该运行得很好。


~d


Arun Kumar写道:
Hi Arun,

The code should work fine, although it violates best practices by
interacting directly with the UI from threads other than the main
thread. Other than that though, it *looks* like it should run just fine.

~d

Arun Kumar wrote:
此代码有什么问题。所有我想测试的是3个进度条和一个按钮。在buttonclick上我创建3个线程,每个线程调用一个方法
,然后更新进度条并且它可以工作。我想知道是否可以使用。
谢谢
private void button1_Click(object sender,System.EventArgs e)



/> ThreadStart job = new ThreadStart(onemethod);

线程thread = new Thread(job);

thread.Start();

ThreadStart job1 = new ThreadStart(secondmethod);

线程thread1 =新线程(job1);

thread1.Start();

ThreadStart job2 = new ThreadStart(thirdmethod);

线程thread2 =新线程(job2);

thread2.Start();

}

public void onemethod()

对于(int i = 0; i< 100; i ++)

{

progressBar1.Value = i;

Thread.Sleep(100);

}

}

public void secondmethod()

对于(int i = 0; i< 100; i ++)

{

progressBar2.Value = i;

Thread.Sleep(100);

}

}

public void thirdmethod()


对于(int i = 0; i< 100; i ++)



progressBar3.Value = i;

Thread.Sleep(100);

}

}
What is wrong with this code. All i am trying to test is 3 progressbar and
one button. On buttonclick i create 3 threads and each thread calls a method
which in turn updates the progressbar and it works. I would to know if this
can be used.
Thanks
private void button1_Click(object sender, System.EventArgs e)

{

ThreadStart job = new ThreadStart(onemethod);

Thread thread = new Thread(job);

thread.Start();

ThreadStart job1 = new ThreadStart(secondmethod);

Thread thread1 = new Thread(job1);

thread1.Start();

ThreadStart job2 = new ThreadStart(thirdmethod);

Thread thread2 = new Thread(job2);

thread2.Start();

}

public void onemethod()

{

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

{

progressBar1.Value = i;

Thread.Sleep(100);

}

}

public void secondmethod()

{

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

{

progressBar2.Value = i;

Thread.Sleep(100);

}

}

public void thirdmethod()

{

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

{

progressBar3.Value = i;

Thread.Sleep(100);

}

}


< br>

DotNet Coder,


我同意,这不是最好的做法,但我的观点是,如果你使用线程和

请小心使用它它应该不是问题。

查看此链接
http://www.yoda.arachsys.com/csharp/...winforms.shtml


让我告诉你评论。


Arun

" DotNet Coder" < D0 ********* @ yahoo.dot.com>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP12.phx.gbl ...
DotNet Coder,

I agree, its not the best practices but my point is, if you use threads and
use it carefully it shouldn''t be a problem.
Check this link
http://www.yoda.arachsys.com/csharp/...winforms.shtml

let me know you comments.

Arun
"DotNet Coder" <d0*********@yahoo.dot.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
嗨Arun ,

代码应该可以正常工作,虽然它违反了最好的做法,直接与主线程以外的线程进行UI交互。
除此之外,它*看起来*喜欢它应该运行得很好。

~Ar / /
Arun Kumar写道:
Hi Arun,

The code should work fine, although it violates best practices by
interacting directly with the UI from threads other than the main thread.
Other than that though, it *looks* like it should run just fine.

~d

Arun Kumar wrote:
这段代码有什么问题。所有我想测试的是3个进度条和一个按钮。在buttonclick上我创建3个线程,每个线程调用一个
方法,然后更新进度条并且它可以工作。我想知道是否可以使用它。
谢谢
private void button1_Click(对象发送者,System.EventArgs e)

{

ThreadStart job = new ThreadStart(onemethod);

线程thread = new Thread(job);

thread.Start();

ThreadStart job1 = new ThreadStart(secondmethod);

线程thread1 =新线程(job1);

thread1.Start();

ThreadStart job2 = new ThreadStart(thirdmethod);

线程thread2 =新线程(job2);

thread2.Start();

}

public void onemethod()

对于(int i = 0; i< 100; i ++)

{

progressBar1.Value = i;

Thread.Sleep(100);

}

}

public void secondmethod()

对于(int i = 0; i< 100; i ++)

{

progressBar2.Value = i;

Thread.Sleep(100);

}

}

public void thirdmethod()


对于(int i = 0; i< 100; i ++)



progressBar3.Value = i;

Thread.Sleep(100);

}

}
What is wrong with this code. All i am trying to test is 3 progressbar
and one button. On buttonclick i create 3 threads and each thread calls a
method which in turn updates the progressbar and it works. I would to
know if this can be used.
Thanks
private void button1_Click(object sender, System.EventArgs e)

{

ThreadStart job = new ThreadStart(onemethod);

Thread thread = new Thread(job);

thread.Start();

ThreadStart job1 = new ThreadStart(secondmethod);

Thread thread1 = new Thread(job1);

thread1.Start();

ThreadStart job2 = new ThreadStart(thirdmethod);

Thread thread2 = new Thread(job2);

thread2.Start();

}

public void onemethod()

{

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

{

progressBar1.Value = i;

Thread.Sleep(100);

}

}

public void secondmethod()

{

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

{

progressBar2.Value = i;

Thread.Sleep(100);

}

}

public void thirdmethod()

{

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

{

progressBar3.Value = i;

Thread.Sleep(100);

}

}






完全错误...如果你使用线程并仔细使用它,它将会是一个问题。正如那篇文章所述,永远不要在除Invoke之外的另一个线程上创建的控件上调用任何方法或

属性,

BeginInvoke,EndInvoke或CreateGraphics,以及InvokeRequired。 ;。


这不仅仅是一个黄金法则,它是winforms编程的事实。

当你开始搞乱其他线程中创建的对象时,你开始

遇到各种各样的问题,比如竞争条件,死锁等等。


另外,正如作者所述,你可以侥幸逃脱一段时间,但最终,你的应用程序将开始显示一些非常奇怪的行为,调试可能会成为一个可怕的过程,特别是如果

它是杀死应用程序的线程错误。


HTH,

~d


Arun Kumar写道:
Completely wrong... if you use threads and use it carefully, it will
still be a problem. As that article states, "Never invoke any method or
property on a control created on another thread other than Invoke,
BeginInvoke, EndInvoke or CreateGraphics, and InvokeRequired.".

This is not just a golden rule, it is a fact of winforms programming.
When you start messing with objects created in other threads, you start
running into all sorts of problems, like race conditions, deadlocks, etc.

Also, as the author stated, you may get away with it for awhile, but
eventually, your application will start displaying some pretty strange
behaviors and debugging could become a horrible process, especially if
it is threading errors that are killing the app.

HTH,
~d

Arun Kumar wrote:
DotNet Coder,

我同意,这不是最好的做法,但我的观点是,如果你使用线程和
使用我小心它不应该是一个问题。
检查此链接
http://www.yoda.arachsys.com/csharp/...winforms.shtml

告诉我你的意见。

Arun

" DotNet Coder" < D0 ********* @ yahoo.dot.com>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP12.phx.gbl ...
DotNet Coder,

I agree, its not the best practices but my point is, if you use threads and
use it carefully it shouldn''t be a problem.
Check this link
http://www.yoda.arachsys.com/csharp/...winforms.shtml

let me know you comments.

Arun
"DotNet Coder" <d0*********@yahoo.dot.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
嗨Arun ,

代码应该可以正常工作,虽然它违反了最好的做法,直接与主线程以外的线程进行UI交互。
除此之外,它*看起来*喜欢它应该运行得很好。

~Ar / /
Arun Kumar写道:
Hi Arun,

The code should work fine, although it violates best practices by
interacting directly with the UI from threads other than the main thread.
Other than that though, it *looks* like it should run just fine.

~d

Arun Kumar wrote:
这段代码有什么问题。所有我想测试的是3个进度条和一个按钮。在buttonclick上我创建3个线程,每个线程调用一个
方法,然后更新进度条并且它可以工作。我想知道是否可以使用它。
谢谢
private void button1_Click(对象发送者,System.EventArgs e)

{

ThreadStart job = new ThreadStart(onemethod);

线程thread = new Thread(job);

thread.Start();

ThreadStart job1 = new ThreadStart(secondmethod);

线程thread1 =新线程(job1);

thread1.Start();

ThreadStart job2 = new ThreadStart(thirdmethod);

线程thread2 =新线程(job2);

thread2.Start();

}

public void onemethod()

对于(int i = 0; i< 100; i ++)

{

progressBar1.Value = i;

Thread.Sleep(100);

}

}

public void secondmethod()

对于(int i = 0; i< 100; i ++)

{

progressBar2.Value = i;

Thread.Sleep(100);

}

}
< (int i =。)public void thirdmethod()


0; i< 100; i ++)



progressBar3.Value = i;

Thread.Sleep(100);

}

}
What is wrong with this code. All i am trying to test is 3 progressbar
and one button. On buttonclick i create 3 threads and each thread calls a
method which in turn updates the progressbar and it works. I would to
know if this can be used.
Thanks
private void button1_Click(object sender, System.EventArgs e)

{

ThreadStart job = new ThreadStart(onemethod);

Thread thread = new Thread(job);

thread.Start();

ThreadStart job1 = new ThreadStart(secondmethod);

Thread thread1 = new Thread(job1);

thread1.Start();

ThreadStart job2 = new ThreadStart(thirdmethod);

Thread thread2 = new Thread(job2);

thread2.Start();

}

public void onemethod()

{

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

{

progressBar1.Value = i;

Thread.Sleep(100);

}

}

public void secondmethod()

{

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

{

progressBar2.Value = i;

Thread.Sleep(100);

}

}

public void thirdmethod()

{

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

{

progressBar3.Value = i;

Thread.Sleep(100);

}

}




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

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