您的位置:首页 > 编程语言 > C#

c#如何实现一个线程暂停,等待用户输入文本后继续运行?

2017-01-26 10:14 706 查看
using System;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Thread th = new Thread(YourThread);
th.Start();
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text.Length >= 4)
{
detailCollectedEvent.Set();                      // 当textBox1的文本超过4位,发一个通知
}
}

AutoResetEvent detailCollectedEvent = new AutoResetEvent(false);
void YourThread()
{
MessageBox.Show("input you bank account details into the textbox");
detailCollectedEvent.WaitOne();                      // 等候通知
MessageBox.Show("we will keep the secret.");
}
}
}

 

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private delegate void setText(string msg);
private setText settext;

private void button1_Click(object sender, EventArgs e)
{
Thread objThread = new Thread(new ThreadStart(delegate
{
YourThread();
}));
objThread.Start();

settext = new setText(setTextbox);
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text.Length >= 4)
{
detailCollectedEvent.Set();                      // 当textBox1的文本超过4位,发一个通知
}
}
public void setTextbox(string msg)
{
textBox1.Text = msg;
}
AutoResetEvent detailCollectedEvent = new AutoResetEvent(false);
void YourThread()
{
for (int i = 0; i < 3; i++)
{
this.BeginInvoke(settext, "开始");
detailCollectedEvent.WaitOne();                      // 等候通知
this.BeginInvoke(settext, "输入正确" + i.ToString());
}
}

}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐