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

想实现每隔5秒朝另一个程序发送一个按键消息
[DllImport("user32.dll ", EntryPoint = "FindWindow")]
  public static extern IntPtr FindWindow(string 类名, string 程序标题);
  [DllImport("user32.dll ")]
  static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  [DllImport("user32.dll ")]
  static extern int SetForegroundWindow(IntPtr hwnd);



  private void button1_Click(object sender, EventArgs e)
  {
  IntPtr hwnd = FindWindow(null, "C:\\Users\\song\\Desktop\\code\\test.txt - Notepad++"); 
  if (hwnd.ToInt32 () != 0)
  {
  SetForegroundWindow(hwnd);
  ShowWindow(hwnd, 1);
  //MessageBox.Show("计算器句柄_ " + hwnd.ToString());
  //SetForegroundWindow(hwnd);
  DateTime begin = DateTime.MaxValue;
  while (DateTime.Now < begin)
  {
   
  if (state)
  {

  SendKeys.Send("33");
  }
  else
  {
  break;
  }
  }
   
  }
  else { MessageBox.Show("计算器没有运行 "); }
  }

  private void button2_Click(object sender, EventArgs e)
  {
  state = false;

  }

我发现的代码 1.程序不能退出 2.每次运行程序 就在循环,notepad拿不到cpu的资源 每次都是死掉程序
求大侠怎么改进?

------解决方案--------------------
想把系统搞死,有木有?(也不经过大规模测试就)随便胡乱占用系统资源,有木有?