C# 方法等待winform事件
在我一直在开发的一个程序中,需要一个方法来等待,直到在特定的文本框中单击ENTER(通常称为winform事件)。我知道我应该用线程来做这件事,但我不知道如何制作一个方法来做到这一点。更具体地说,我不知道如何在线程上调用event方法,也不能在Main上调用,因为在调用这个方法之前它是被阻塞的 停止主线程的方法是:C# 方法等待winform事件,c#,.net,winforms,multithreading,wait,C#,.net,Winforms,Multithreading,Wait,在我一直在开发的一个程序中,需要一个方法来等待,直到在特定的文本框中单击ENTER(通常称为winform事件)。我知道我应该用线程来做这件事,但我不知道如何制作一个方法来做到这一点。更具体地说,我不知道如何在线程上调用event方法,也不能在Main上调用,因为在调用这个方法之前它是被阻塞的 停止主线程的方法是: void WaitForInput() { while (!gotInput) { System.Threading.Thread.Slee
void WaitForInput()
{
while (!gotInput)
{
System.Threading.Thread.Sleep(1);
}
}
感谢您的帮助。只需订阅(或)您文本框中的事件:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// do your stuff
}
}
首先,可以使用以下任务将WaitForInput方法更改为线程化:
private void WaitForInput()
{
Task.Factory.StartNew(() =>
{
while (!gotInput)
{
System.Threading.Thread.Sleep(1);
}
MessageBox.Show("Test");
});
}
private void KeyDown(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
gotInput = true;
}
CancellationTokenSource tokenSource; // member variable in your Form
// Initialize and wait for input on Form.Load.
async void Form_Load(object sender, EventArgs e)
{
tokenSource = new CancellationTokenSource();
await WaitForInput(tokenSource.Token);
// ENTER was pressed!
}
// Our TextBox has input, cancel the wait if ENTER was pressed.
void TextBox_KeyDown(object sender, KeyEventArgs e)
{
// Wait for ENTER to be pressed.
if(e.KeyCode != Keys.Enter) return;
if(tokenSource != null)
tokenSource.Cancel();
}
// This method will wait for input asynchronously.
static async Task WaitForInput(CancellationToken token)
{
await Task.Delay(-1, token); // wait indefinitely
}
然后捕获文本框的按键事件,并将布尔输入的状态更改为true,如下所示:
private void WaitForInput()
{
Task.Factory.StartNew(() =>
{
while (!gotInput)
{
System.Threading.Thread.Sleep(1);
}
MessageBox.Show("Test");
});
}
private void KeyDown(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
gotInput = true;
}
CancellationTokenSource tokenSource; // member variable in your Form
// Initialize and wait for input on Form.Load.
async void Form_Load(object sender, EventArgs e)
{
tokenSource = new CancellationTokenSource();
await WaitForInput(tokenSource.Token);
// ENTER was pressed!
}
// Our TextBox has input, cancel the wait if ENTER was pressed.
void TextBox_KeyDown(object sender, KeyEventArgs e)
{
// Wait for ENTER to be pressed.
if(e.KeyCode != Keys.Enter) return;
if(tokenSource != null)
tokenSource.Cancel();
}
// This method will wait for input asynchronously.
static async Task WaitForInput(CancellationToken token)
{
await Task.Delay(-1, token); // wait indefinitely
}
祝你好运使用.NET 4.5中的
async/await
关键字。你可以这样做:
private void WaitForInput()
{
Task.Factory.StartNew(() =>
{
while (!gotInput)
{
System.Threading.Thread.Sleep(1);
}
MessageBox.Show("Test");
});
}
private void KeyDown(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
gotInput = true;
}
CancellationTokenSource tokenSource; // member variable in your Form
// Initialize and wait for input on Form.Load.
async void Form_Load(object sender, EventArgs e)
{
tokenSource = new CancellationTokenSource();
await WaitForInput(tokenSource.Token);
// ENTER was pressed!
}
// Our TextBox has input, cancel the wait if ENTER was pressed.
void TextBox_KeyDown(object sender, KeyEventArgs e)
{
// Wait for ENTER to be pressed.
if(e.KeyCode != Keys.Enter) return;
if(tokenSource != null)
tokenSource.Cancel();
}
// This method will wait for input asynchronously.
static async Task WaitForInput(CancellationToken token)
{
await Task.Delay(-1, token); // wait indefinitely
}
目前我的电脑是恐龙电脑,上面有XP(.NET2008,要到4月左右才能升级)。我最终遵循了注释中的解决方案,让主线程等待并在线程上运行条目。
谢谢 你为什么不调用TextBox的按键事件的方法呢?看看这个stackoverflow帖子..看起来像是类似的问题,我已经这样做了,但没有帮助。我需要停止主线程,但仍然能够调用textBoxInput_KeyDown()。您的解决方案不会停止线程。@user1461837您永远不应该停止主线程。它将挂起你的应用程序。我开始考虑让线程等待输入,并使每个事件都在自己的线程上运行。@ USE1461837,你能解释一下你想要达到的目标吗?为什么要停止线程?请描述应用程序的工作流by KeyDown(),您指的是textbox KayDown事件?它表示任务不存在。