-1
這裏是我的代碼:Visual Studio的C#while循環冷凍形式應用
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace _8BB_2._0
{
public partial class Form1 : Form
{
public static class globalVars
{
public static bool spacerunning = false;
}
public Form1()
{
InitializeComponent();
globalVars.spacerunning = false;
}
private void button1_Click(object sender, EventArgs e)
{
if (!globalVars.spacerunning)
{
globalVars.spacerunning = true;
while (globalVars.spacerunning)
{
Thread.Sleep(1000);
SendKeys.Send(" ");
}
}
else if (globalVars.spacerunning)
{
globalVars.spacerunning = false;
}
}
}
}
當我點擊按鈕1這是開始打空間每一秒它應該像但當我嘗試再次單擊它關閉它的應用程序凍結並保持緊迫的空間。我嘗試了多種其他方式,但似乎無法弄清楚我是如何一次執行兩件事的,因爲我被鎖定在while循環中。
謝謝!用戶界面現在可以工作,但當我點擊按鈕時,它似乎並沒有停止?這是與異步或我的代碼錯誤? – Prince
代碼正常,但發送空間可以按下按鈕(如果選中)。 –