2009-05-01 54 views
3

我想在C#中使用下面的代碼,但我似乎無法擺脫它。如果用戶按下某個鍵或移動鼠標(又名鼠標),我想終止該應用程序。這是我的代碼(不笑!)。如何阻止我的while循環?

private void frmDots_KeyDown(object sender, KeyEventArgs e) 
{ 
    bgNotClicked = false; 
    Close(); 
} 

private void frmDots_Click(object sender, EventArgs e) 
{ 
    bgNotClicked = false; 
    Close(); 
} 



    while (bgNotClicked) 
    { 

    // Clear the first element in our XY position. This is the reverse of the way I normally create the dots application 
    System.Drawing.Rectangle clearDots = new System.Drawing.Rectangle(Dots.PositionX[iCounter], Dots.PositionY[iCounter], 8, 8); 

    // Create the black color and brush to clear dots 
    Color clearDotsColor = Color.Black; 
    SolidBrush clearDotsBrush = new SolidBrush(clearDotsColor); 

    // Finally clear the dot 
    e.Graphics.FillEllipse(clearDotsBrush, clearDots); 


    GetRandomPosition(iCounter); 

    // Fill the elements to display colors on the displays canvas 
    System.Drawing.Rectangle colorDots = new System.Drawing.Rectangle(Dots.PositionX[iCounter], Dots.PositionY[iCounter], 8, 8); 

    // Create the color and brush to show dots 
    Color colorRandom = GetRandomColor(); 
    SolidBrush colorBrush = new SolidBrush(colorRandom); 

    // Finally show the dot 
    e.Graphics.FillEllipse(colorBrush, colorDots); 

    Thread.Sleep(5); 

    iCounter++; 
    if (iCounter == 399) 
    { 
     iCounter = 0; 
    } 

    } 
} 
+6

我想我要從現在開始稱它爲「齧齒動物」,始終是 – 2009-05-01 17:55:01

+1

哦,我的上帝,我只是意識到齧齒動物的意思是鼠標。棒極了。 – AndreiM 2009-05-01 17:57:54

+0

@Peter:我更新了我的答案以迴應您的修改。問題在於你的UI消息泵被阻塞,所以你的處理程序從不運行。 – 2009-05-01 18:08:26

回答

4

編輯:

看到您的編輯後,這絕對是您的問題。問題是你的while循環阻塞了主UI線程,所以它永遠不會處理觸發你的按鍵/鼠標/等處理程序的Windows消息。

你有幾個選項 - 你可以將其中的一些移動到一個單獨的線程上,按照我的建議操作,或者在你的while循環中添加一個Application.DoEvents的調用。這將允許您的事件處理程序運行,然後再設置bgNotClicked = false;。現在,這絕不會發生,因爲你的UI完全被阻止。


原貼:

如果你在你的UI線程做這個循環中,你將需要重新思考設計了一下。

設置bgNotClicked = false;事件處理程序中的某處可以正常工作,但前提是您的事件處理程序能夠運行。如果你在UI線程中執行上述代碼,它將無限期地阻止你的UI線程,阻止事件處理程序的觸發。

我建議重做這個基於定時器(所以它定期重複運行),而不是鎖定在一個while循環。這將允許您的其他UI事件在運行之間觸發,而不是設置bgNotClicked = false;,您的事件處理程序可以將計時器設置爲未啓用。

0

您可以使用突破聲明退出循環。

編輯:好的,我拿回旗的東西!

+1

爲什麼地球上的國旗必須是全球性的? – Pesto 2009-05-01 17:57:53

+0

讀那個答案傷害了我的感情。 :(全局任何東西(包括單身和靜態類)讓我非常難過 – 2009-05-01 18:10:17

+0

答案更新 – Kirtan 2009-05-01 19:31:51

6

你的「忙等待」策略是糟糕的設計。相反,你應該使用被觸發的事件處理程序:

  • On keypress。
  • 鼠標移動時。

無論哪種情況,您都可以通過終止應用程序進行響應。

+0

當一個人被連續循環卡住時,說起來容易做起來難 – hmcclungiii 2009-05-01 18:14:22

1

break關鍵字將終止一個循環。在這種情況下,當您想要停止循環時,您只需使用break;

3

您的bgNotClicked變量需要設置爲false通過您的按鍵事件處理程序。

如果鼠標移動鼠標,您將需要一個類似的鼠標事件處理程序。

+0

如果一個齧齒動物在你不在辦公桌上時尋找並移動你的齧齒動物? – mquander 2009-05-01 18:13:17

-3
Exit While 

...................

+2

這顯然是C#代碼... VB.NET如何幫助他? – GEOCHET 2009-05-01 17:57:25

0

這並不似乎是正確的方法。 .Net Framework爲您提供了處理KeyPress和MouseMove/Click操作的事件。你爲什麼不使用它們?

0

嘗試將循環移動到BackgroundWorker的DoWork事件處理程序中。然後你的GUI仍然會響應,而不是那個討厭的變量,你可以調用CancelAsync方法來停止循環。

1

如果您正在循環,您需要給應用程序一些時間來處理您希望會導致中斷的事件。這是DoEvents方法的工作。

private bool WeAreDone = false; 

private void DoingIt() 
{ 
    while (true) 
    { 
     Application.DoEvents(); 
     if (WeAreDone) 
     { 
      break; 
     } 
    } 
} 

private void InterruptButton_Click(object sender, EventArgs e) 
{ 
    WeAreDone = true; 
} 
1

我認爲使用Timer適合Windows事件驅動模型比忙等待循環更好。你可以嘗試這樣的事:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private int iCounter = 0; 

    private void Draw() 
    { 
     // .... 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     Draw(); 

     iCounter++; 
     if(iCounter == 399) 
     { 
      iCounter = 0; 
     } 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     timer1.Interval = 5; 
     timer1.Enabled = true; 
    } 

    private void Form1_Click(object sender, EventArgs e) 
    { 
     timer1.Enabled = false; 
     Close(); 
    } 

    private void Form1_KeyDown(object sender, KeyEventArgs e) 
    { 
     timer1.Enabled = false; 
     Close(); 
    } 
} 
0

使用Environment.Exit(2);(或Environment.Exit(1)它確實沒有區別)退出應用程序的出來。