2016-03-08 98 views
1

我想在C#中做一個非常簡單的自動刷新,只是爲了讓它更好。打破while循環和睡眠

我有兩個數字文本框和兩個按鈕的窗體。第一個文本框指定每次點擊之間應該有多少時間(以毫秒爲單位),第二個文本框指定迭代次數。

我擁有的第一個按鈕是button1,它基本上只是啓動程序。我有第二個按鈕叫button2,這將停止button1_Click功能。

這是我有:

private void button1_Click(object sender, EventArgs e) 
{ 
    //While a is less than number of specified iterations 
    for (int a = 0; a < Convert.ToInt32(numericUpDown2.Value); a++) 
    { 
     //Sleep for desired time 
     System.Threading.Thread.Sleep(Convert.ToInt32(numericUpDown1.Value)); 

     //Get x/y coordinates of mouse 
     int X = Cursor.Position.X; 
     int Y = Cursor.Position.Y; 

     //Click mouse at x/y coordinates 
     mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0); 
    } 
} 

public void Form_KeyDown(object sender, KeyEventArgs e) 
{ 
    if (e.KeyCode == Keys.Escape) 
    { 
     this.Close(); 
    } 
} 

我的問題是我的程序是停留在for循環,而且我也沒有辦法打破button1_Click功能。

我想要它,所以如果我按F11或我的button2按鈕,button1_Click函數將立即停止,但窗體本身仍將打開。現在我只是爲了簡單而使用ESC鍵。

+10

你睡覺的UI線程,這樣的應用程序*將*變得反應遲鈍。你需要看看使用適當的計時器。 – ChrisF

+0

在WPF/C#中,您可以使用RepeatButton,詳情如下:https://msdn.microsoft.com/en-us/library/ms750603(v=vs.85).aspx –

+0

您可以在按鈕1中做一個while循環那是否按鈕2沒有被激活? – Jacobr365

回答

1

添加「定時器」對象添加到您的形式,其設置的時間間隔,以毫秒爲「休眠」(不是真的)的數量。將它的Enabled屬性設置爲false。

處理它的Tick事件並將所有內部循環代碼放在那裏(mouse_event調用)。

在button1_Click(buttonStart將是一個更好的名字)中,將timer Enabled設置爲true,並在button2_Click(buttonStop)中將timer Enabled設置爲false。

0

像這樣的事情

public Timer myTimer { get; set; } 

public Form() 
{ 
    myTimer = new Timer(); 
    myTimer.Tick += new EventHandler(TimerEventProcessor); 
} 

private void button1_Click(object sender, EventArgs e) 
{  
    myTimer.Interval = Convert.ToInt32(numericUpDown1.Value); 
    myTimer.Start(); 
} 

public void Form_KeyDown(object sender, KeyEventArgs e) 
{ 
    if (e.KeyCode == Keys.Escape) 
    { 
     myTimer.Stop(); 
     this.Close(); 
    } 
} 

private static void TimerEventProcessor(Object myObject, EventArgs myEventArgs) 
{ 
    //Get x/y coordinates of mouse 
    int X = Cursor.Position.X; 
    int Y = Cursor.Position.Y; 
    //Click mouse at x/y coordinates 
    mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0); 
} 
-1
Timer t1; 
int ticks = 0; 
bool timerInitialized = false 
private void button1_Click(object sender, EventArgs e) 
{ 
    if (!timerInitialized) 
    { 
    t1 = new Timer(); 
    t1.Tick += Timer_Tick; 
    timerInitialized = true; 
    } 
    button1.Enabled = false; 
    t1.Interval = Convert.ToInt32(numericUpDown1.Value); 
    ticks = 0; 
    t1.Start(); 
} 

private void Timer_Tick(object sender, EventArgs e) 
{ 
    if (ticks < Convert.ToInt32(numericUpDown2.Value)) 
    { 
    ticks++; 

    //Get x/y coordinates of mouse 
    int X = Cursor.Position.X; 
    int Y = Cursor.Position.Y; 
    //Click mouse at x/y coordinates 
    mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0); 
    } 
    else 
    { 
    t1.Stop(); //You could Call Stop from every where e.g. from another Button 
    button1.Enabled = true; 
    } 
} 

編輯: 分配「嘀」事件只有一次

+1

您正在委託每個Button1單擊的Timer_Tick方法。不是很好。 –

+0

@JamesDev這是真的我已經糾正了片段。 – Pazi01