asp.net如何在事件中启动线程来打开一个页面
1个回答
展开全部
这个需要用到后台线程执行,否则一运行主线程就卡住了:
public partial class Form1 : Form
{
// 打印放在后台执行
BackgroundWorker m_Worker;
// 用一个标记来看是否请求停止执行
bool m_NeedRun = false;
public Form1()
{
InitializeComponent();
m_Worker = new BackgroundWorker();
m_Worker.DoWork += new DoWorkEventHandler(m_Worker_DoWork);
}
void m_Worker_DoWork(object sender, DoWorkEventArgs e)
{
while( m_NeedRun ) // 后台可以循环打印,不影响主线程的运行
Console.Write( "xxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyy" );
}
private void button1_Click(object sender, EventArgs e)
{
m_NeedRun = true;
m_Worker.RunWorkerAsync();
}
private void button2_Click(object sender, EventArgs e)
{
m_NeedRun = false;
}
}
public partial class Form1 : Form
{
// 打印放在后台执行
BackgroundWorker m_Worker;
// 用一个标记来看是否请求停止执行
bool m_NeedRun = false;
public Form1()
{
InitializeComponent();
m_Worker = new BackgroundWorker();
m_Worker.DoWork += new DoWorkEventHandler(m_Worker_DoWork);
}
void m_Worker_DoWork(object sender, DoWorkEventArgs e)
{
while( m_NeedRun ) // 后台可以循环打印,不影响主线程的运行
Console.Write( "xxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyy" );
}
private void button1_Click(object sender, EventArgs e)
{
m_NeedRun = true;
m_Worker.RunWorkerAsync();
}
private void button2_Click(object sender, EventArgs e)
{
m_NeedRun = false;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询