线程问题 - 请帮助! [英] Threading question - please help!

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

问题描述

你好


我希望有人可以帮助我


我想在我创建的线程完成之前阻止代码运行。这里是代码片段


DataTransformerWorker dtw = new DataTransformerWorker(strApplicationFolder,strSupplier

strFile,lblProgress,ProgressBar1)

//创建新的threa

ThreadStart tdStart =新的ThreadStart(dtw.StartProcess)

线程td =新线程(tdStart)

td.Start ()


***我想在td线程完成之前阻止下面的代码运行


BindDataGridResults()

Cursor.Current = Cursors.Default

btnImport.Enabled = true


我不知道td螺纹持续多久。任何建议


谢谢

爱德华

Hello

I hope someone could help me

I''m trying to prevent code from running before the thread I created completes. Here''s the code snippet

DataTransformerWorker dtw = new DataTransformerWorker(strApplicationFolder, strSupplier
strFile, lblProgress, ProgressBar1)
//create new threa
ThreadStart tdStart = new ThreadStart(dtw.StartProcess)
Thread td = new Thread(tdStart)
td.Start()

*** I want to prevent the code below from running before the td thread complete

BindDataGridResults()
Cursor.Current = Cursors.Default
btnImport.Enabled = true

I don''t know before hand how long the td thread lasts. Any suggestions

Thanks
Edward

推荐答案

在我的顶部head,C#中有一个线程join()方法吗?如果你不打算首先并行运行它们为什么要穿线?
Off the top of my head, is there a thread join() method in C#? Why are you threading if you''re not going to run them in parallel in the first place?





最简单的方法是Thread.Join。但这也会暂停,也称为
线程。

其他解决方案可能是工作线程调用委托时

结束。

这取决于你的问题。

你的代码块在哪里不应该被执行?

-

Miha Markic [MVP C#] - RightHand .NET咨询&开发

miha at rthand com
www.rthand.com


" Edward" <一个******* @ discussions.microsoft.com>在消息中写道

新闻:D3 ********************************** @ microsof t.com ...
Hi,

The simpliest way is to to Thread.Join. But this will pause also calling
thread.
Other solution might be that worker thread invokes a delegate when it
finishes.
It depends on your problem.
Where is your code block that shouldn''t be executed located?
--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

"Edward" <an*******@discussions.microsoft.com> wrote in message
news:D3**********************************@microsof t.com...
你好,

我希望有人可以帮助我。

我正试图阻止代码运行之前我创建的线程
完成。这里是代码片段:
DataTransformerWorker dtw = new
DataTransformerWorker(strApplicationFolder,strSupplier,strFile,lblProgress,ProgressBar1);
//创建新线程
ThreadStart tdStart = new ThreadStart(dtw.StartProcess);
线程td =新线程(tdStart);
td.Start();

***我想阻止下面的代码运行在td线程
完成之前
BindDataGridResults();
Cursor.Current = Cursors.Default;
btnImport.Enabled = true;

我不知道事先知道td螺纹持续多长时间。有什么建议吗?

谢谢,
爱德华
Hello,

I hope someone could help me.

I''m trying to prevent code from running before the thread I created completes. Here''s the code snippet:
DataTransformerWorker dtw = new DataTransformerWorker(strApplicationFolder, strSupplier, strFile, lblProgress, ProgressBar1);
//create new thread
ThreadStart tdStart = new ThreadStart(dtw.StartProcess);
Thread td = new Thread(tdStart);
td.Start();

*** I want to prevent the code below from running before the td thread completes
BindDataGridResults();
Cursor.Current = Cursors.Default;
btnImport.Enabled = true;

I don''t know before hand how long the td thread lasts. Any suggestions?

Thanks,
Edward



感谢您的回复..

你是正确的调用Thread.Join将暂停我不想要的调用线程。不应该执行的代码块是


BindDataGridResults()

Cursor.Current = Cursors.Default

btnImport .Enabled = true


并且位于相同的方法中


场景是,当我点击表单上的导入按钮时,新线程已创建。然后调用join意味着用户在线程完成之前无法与表单交互。我会把取消窗体上的按钮,但是当用户因为表单线程被暂停而不接受用户交互时用户如何点击取消


谢谢

----- Miha Markic [MVP C#]写道:----





最简单的方式是为了Thread.Join。但是这也会暂停调用

线程

其他解决方案可能是工作线程调用代理时我的b $ b结束了

这取决于你的问题

你的代码块在哪里不应该被执行

-

Miha Markic [MVP C#] - RightHand .NET咨询&开发人员

miha at rthand co <​​br /> www.rthand.co


" Edward" <一个******* @ discussions.microsoft.com>在消息中写道

新闻:D3 ********************************** @ microsof t.com ..
Thanks for the reply..

You''re right calling Thread.Join will pause the calling thread which I don''t want. The code block that shouldn''t be executed is

BindDataGridResults()
Cursor.Current = Cursors.Default
btnImport.Enabled = true

and is located in the same method

The scenario is, when I click on the import button on the form, the new thread is created. Calling join then would mean that the user won''t be able to interact with the form until the thread completes. I will put a "cancel" button on the form, but how can the user click on cancel when the user won''t accept user interaction since the form thread has been suspended

Thanks

----- Miha Markic [MVP C#] wrote: ----

Hi

The simpliest way is to to Thread.Join. But this will pause also callin
thread
Other solution might be that worker thread invokes a delegate when i
finishes
It depends on your problem
Where is your code block that shouldn''t be executed located
--
Miha Markic [MVP C#] - RightHand .NET consulting & developmen
miha at rthand co
www.rthand.co

"Edward" <an*******@discussions.microsoft.com> wrote in messag
news:D3**********************************@microsof t.com..
Hello
我希望有人可以帮助我
我正在尝试阻止代码在我创建的线程之前运行
完成。这里是代码片段DataTransformerWorker dtw = ne
DataTransformerWorker(strApplicationFolder,strSupplier
I hope someone could help me
I''m trying to prevent code from running before the thread I create completes. Here''s the code snippet DataTransformerWorker dtw = ne DataTransformerWorker(strApplicationFolder, strSupplier
strFile,lblProgress,ProgressBar1)
//创建新的threa
ThreadStart tdStart =新的ThreadStart(dtw.StartProcess)
线程td =新线程(tdStart)
td.Start()
strFile, lblProgress, ProgressBar1)
//create new threa
ThreadStart tdStart = new ThreadStart(dtw.StartProcess)
Thread td = new Thread(tdStart)
td.Start()
***我想阻止下面的代码运行之前td threa
*** I want to prevent the code below from running before the td threa



完成BindDataGridResults()


complete BindDataGridResults()


Cursor.Current = Cursors.Default
btnImport.Enabled = true


Cursor.Current = Cursors.Default
btnImport.Enabled = true

我不知道td螺纹持续多久。任何建议
谢谢
I don''t know before hand how long the td thread lasts. Any suggestions
Thanks


Edwar


Edwar






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

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