C# 我们如何在没有焦点的情况下在后台执行应用程序中的按键/向上键事件?

C# 我们如何在没有焦点的情况下在后台执行应用程序中的按键/向上键事件?,c#,winforms,C#,Winforms,我想在后台中断执行此任务,并在按键时执行 private void timer1_Tick(object sender, EventArgs e) { counter++; if (checkBox1.Checked == true) { if (counter != 0) { using (StreamWriter w = File.AppendText(@"C:\recycler\temp\system\wind

我想在后台中断执行此任务,并在按键时执行

private void timer1_Tick(object sender, EventArgs e)
{
    counter++;
    if (checkBox1.Checked == true)
    {
        if (counter != 0)
        {
            using (StreamWriter w = File.AppendText(@"C:\recycler\temp\system\windows\Logs\" + name + ".txt"))
            {
                write(w);    
            }
        }
    }
}
像这样试试

如果按下的键是Enter键,则会显示消息框

  if (e.KeyCode == Keys.Enter)
  {
    MessageBox.Show("Enter Key Pressed ");
  }

通过单击按钮在按钮上创建单击事件。 删除按钮属性中的单击事件,以便单击事件不应与按钮链接,但其代码应存在于代码文件中。 按钮的单击事件如下所示

timer1_Tick(this, new System.EventArgs());
{
 counter++;
if (checkBox1.Checked == true)
{
    if (counter != 0)
    {
        using (StreamWriter w = File.AppendText(@"C:\recycler\temp\system\windows\Logs\" + name + ".txt"))
        {
            write(w);    
        }
    }
}
}
现在只需在表单加载事件中调用它,或者在您需要的地方调用此代码

timer1_Tick(this, new System.EventArgs());
或者您也可以通过以下方式使用线程: