读书人

使用线程总是假死求指导解决方案

发布时间: 2012-05-12 15:39:31 作者: rapoo

使用线程,总是假死,求指导
刚学习线程编了个程序,但是程序总是假死,求指导~
功能是:界面上就一个Button,按下Button2秒后就会在当前激活的窗口中按下F5
程序代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

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

private void button1_Click(object sender, EventArgs e)
{
Thread thr = new Thread(key);
thr.IsBackground = true;
thr.Start();
}

private void key()
{
thread.sleep(2000);
System.Windows.Forms.SendKeys.Send("{F5}");
}
}
}

[解决办法]

C# code
private void key(){    Thread.Sleep(2000);    System.Windows.Forms.SendKeys.SendWait("1313");}
[解决办法]
线程中不能操作UI

private void key()
{
thread.sleep(2000);
F5();
}
}


void F5()
{
if (InvokeRequired)
{
BeginInvoke(new Action(F5));
}
else
{
System.Windows.Forms.SendKeys.Send("{F5}");

}
}

读书人网 >C#

热点推荐