日期:2014-05-18  浏览次数:21016 次

使用线程,总是假死,求指导
刚学习线程编了个程序,但是程序总是假死,求指导~
功能是:界面上就一个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}");

}
}