[英]Waiting for a ThreadState to be “Stopped” in a Button Click Event
I'm having some issues while waiting for a ThreadState
during the Click
event of a button control. 我在按钮控件的
Click
事件期间等待ThreadState
时遇到一些问题。 Whenever I click my button it'll execute the code below. 每当我单击我的按钮时,它将执行以下代码。 The problem with this is that it won't wait until the
ThreadState
is "Stopped", so it never enables btnImportData
or btnExportBellijst
. 问题在于它不会等到
ThreadState
被“停止”,所以它永远不会启用btnImportData
或btnExportBellijst
。
I've tried t.Join()
but that freezes my whole form and I use a RichTextBox
as logger, so that'd result in a logger that freezes for a few seconds and then shows a lot of text at once. 我尝试过
t.Join()
但是它冻结了我的整个表单,并且我使用RichTextBox
作为记录器,因此导致记录器冻结了几秒钟,然后一次显示了很多文本。 The reason I put the ImportData
function on another Thread is to keep the form running so people can see the logs happening realtime. 我将
ImportData
函数放在另一个线程上的原因是保持表单运行,以便人们可以实时查看日志。
What I'd like to have when I click my button: 单击按钮时我想要的东西:
Enabled
of 1 or more buttons. Enabled
。 ImportData
on another thread so my logger can keep logging. ImportData
,这样我的记录器就可以继续记录日志。 ( void ImportData(){}
) void ImportData(){}
) Change Enabled
of 1 or more buttons after my function is done doing something. 我的功能完成某些操作后,将“已
Enabled
的1个或多个按钮更改为“已Enabled
”。
private void btnImportData_Click(object sender, EventArgs e) { //Disable current button btnImportData.Enabled = false; imgBonne.Visible = false; //random image rtConsole.Visible = true; //RichTextBox logger //Create a new thread for the button function var t = new Thread(ImportData); t.Start(); //It does NOT wait until thread stopped while (t.ThreadState == ThreadState.Stopped) { //Never gets executed btnImportData.Enabled = true; btnExportBellijst.Enabled = true; } }
Extra Info: Screenshot from before pressing "Import Data": http://puu.sh/88oD6.png Screenshot from after the application is done importing data: http://puu.sh/88oNT.png 额外信息:按下“导入数据”之前的屏幕截图: http : //puu.sh/88oD6.png应用程序完成数据导入后的屏幕截图: http : //puu.sh/88oNT.png
(edit)Target framework: .NET Framework 4 目标框架:.NET Framework 4
I was originally using the code below, but this instantly enables all the buttons after pressing "Import Data". 我最初使用下面的代码,但是在按下“导入数据”后,这立即启用了所有按钮。
private void btnImportData_Click(object sender, EventArgs e)
{
imgBonne.Visible = false; //random image
rtConsole.Visible = true; //RichTextBox logger
var t = new Thread(ImportData);
t.Start();
while (t.ThreadState == ThreadState.Running)
{
btnImportData.Enabled = false;
}
btnImportData.Enabled = true;
btnExportBellijst.Enabled = true;
}
Edit: I'm sorry if this is in the wrong category, I wanted to put it in c#. 编辑:很抱歉,如果这是错误的类别,我想将其放在c#中。
Using the Task Parallel Library can make it a whole lot easier: 使用Task Parallel Library可以使它变得更加容易:
private void btnImportData_Click(object sender, EventArgs e)
{
imgBonne.Visible = false; //random image
rtConsole.Visible = true; //RichTextBox logger
btnImportData.Enabled = false;
Task.Run(ImportData).ContinueWith((Task task) =>
{
btnImportData.Enabled = true;
btnExportBellijst.Enabled = true;
}, TaskScheduler.FromCurrentSynchronizationContext());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.