none
exit loop when Escape key is pressed RRS feed

  • Question

  • in need exit the While When a Key is pressed By User. (For Example Esc)
    how to exit while when a key is pressed?
    Thursday, October 16, 2008 5:07 PM

Answers

  • Here is a sample to show how to exit the loop by using the way you proposed

    Public Class Form1  
        Private cancelled As Boolean = False 
     
        Public Sub New()  
     
            ' This call is required by the Windows Form Designer.  
            InitializeComponent()  
     
            ' Add any initialization after the InitializeComponent() call.  
            Me.KeyPreview = True 
            ' if you assigned this statement when you press escape key will be  
            ' ignored in the keyDown event  
            Me.CancelButton = Me.btnCancel  
     
            ' both properties above mentioned can be assigned in the properties window  
        End Sub 
     
        Private Sub btnStart_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles btnStart.Click  
            Dim i As Integer = 0  
            While True 
                Application.DoEvents()  
                i = i + 1  
                Me.Text = "Loop No. " + i.ToString()  
                If Me.cancelled = True Then 
                    Exit While 
                End If 
     
            End While 
            Me.Text = "The operation is canclled" 
        End Sub 
     
        Private Sub btnCancel_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles btnCancel.Click  
            cancelled = True 
        End Sub 
     
        Private Sub Form1_KeyDown(ByVal sender As System.ObjectByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown  
            ' This code will be useless if you assigned the CancelButton property  
            If e.KeyCode = Keys.Escape Then 
                cancelled = True 
            End If 
     
        End Sub 
    End Class 


    But i would like to use BackgroundWorker for this case please have a look to the sample i had proposed in this thread
    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/de9b1a91-1ef6-47ea-bb39-5237e07129fb


    Mahmoud Zaben
    • Marked as answer by Edisoon Thursday, October 16, 2008 10:22 PM
    Thursday, October 16, 2008 8:38 PM

All replies

  • post the loop code.
    Thursday, October 16, 2008 5:40 PM
  • This would be one way to do it:

    Public Class Form1  
     
    Dim m_KeyCode As Windows.Forms.Keys  
    Dim m_th As Threading.Thread  
     
    Private Sub WhileLoop()  
        While True 
            If m_KeyCode = Keys.Escape Then Exit While 
            Threading.Thread.Sleep(10)  
        End While 
     
        MsgBox("Loop exited!")  
    End Sub 
     
    Private Sub Button1_Click(ByVal sender As System.Object, _  
        ByVal e As System.EventArgs) _  
        Handles Button1.Click  
     
        m_th = New Threading.Thread(AddressOf WhileLoop)  
        m_th.Start()  
    End Sub 
     
    Private Sub Form1_KeyDown(ByVal sender As Object, _  
        ByVal e As System.Windows.Forms.KeyEventArgs) _  
        Handles Me.KeyDown  
     
        m_KeyCode = e.KeyCode  
    End Sub 
     
    Private Sub Form1_KeyUp(ByVal sender As Object, _  
        ByVal e As System.Windows.Forms.KeyEventArgs) _  
        Handles Me.KeyUp  
     
        m_KeyCode = Keys.None  
    End Sub 
     
    Private Sub Form1_Load(ByVal sender As System.Object, _  
        ByVal e As System.EventArgs) _  
        Handles MyBase.Load  
     
        Me.KeyPreview = True 
    End Sub 
     
    End Class 
    • Proposed as answer by Corey Furman Thursday, October 16, 2008 5:54 PM
    • Unproposed as answer by Edisoon Thursday, October 16, 2008 6:28 PM
    Thursday, October 16, 2008 5:44 PM
  • My Loop Code :

    if use Threading, i recive any exception

       Private Sub BtnProgress_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPardazesh.Click 
     
            ProgressBar.Visible = True 
            ProgressBar.Minimum = 0 
            ProgressBar.Value = 0 
            ProgressBar.Maximum = FrmMain.DGV.RowCount 
     
            Dim x As Integer = 0 
            Do While x < Val(FrmMain.ObjDt.Rows.Count) 
                LblPID.Text = FrmMain.ObjDt.Rows(x).Item("Pid") 
                LblFullName.Text = FrmMain.ObjDt.Rows(x).Item("Fname") 
                ViewDetail() 
                ViewAcc() 
                Application.DoEvents() 
                SaveAll(False) 
                ProgressBarProgressBar.Value = ProgressBar.Value + 1 
                x = x + 1 
            Loop 
    end sub 

    • Edited by Edisoon Thursday, October 16, 2008 6:27 PM
    Thursday, October 16, 2008 5:56 PM

  •     Dim escPrsd As Boolean = False 
        'before the Do While x < Val(FrmMain.ObjDt.Rows.Count) add 
        'Me.KeyPreview = True 'set this so the form gets the keystrokes 
        'after the Loop 
        'Me.KeyPreview = False  
        'If escPrsd Then Exit Do 'add this inside the Do Loop after the Application.DoEvents 
        ' 
        Private Sub Form1_KeyUp(ByVal sender As ObjectByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp 
            If e.KeyCode = Keys.Escape Then 
                escPrsd = True 
                e.Handled = True 
            Else 
                escPrsd = False 
            End If 
        End Sub 
     

    • Edited by dbasnett Thursday, October 16, 2008 6:33 PM
    • Proposed as answer by dbasnett Thursday, October 16, 2008 9:16 PM
    • Unproposed as answer by Edisoon Thursday, October 16, 2008 10:21 PM
    Thursday, October 16, 2008 6:29 PM
  • I guess it can be done buy using a global or modular boolean variable.  You can set the variable to true by running the excusion button and reset it to false when you run the stop button. During the condition run you can check for the variable value change.

    Here is an example using your code:

    Private Sub BtnProgress_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPardazesh.Click 
     
            ProgressBar.Visible = True 
            ProgressBar.Minimum = 0 
            ProgressBar.Value = 0 
            ProgressBar.Maximum = FrmMain.DGV.RowCount 
     
            Dim x As Integer = 0 
            Do While x < Val(FrmMain.ObjDt.Rows.Count)  and g_blnRun is true
                LblPID.Text = FrmMain.ObjDt.Rows(x).Item("Pid") 
                LblFullName.Text = FrmMain.ObjDt.Rows(x).Item("Fname") 
                ViewDetail() 
                ViewAcc() 
                Application.DoEvents() 
                SaveAll(False) 
                ProgressBar ProgressBar.Value = ProgressBar.Value + 1 
                x = x + 1 
            Loop 
    end sub 


    Analyst
    Thursday, October 16, 2008 8:18 PM
  • tfbasta said:

    I guess it can be done buy using a global or modular boolean variable.  You can set the variable to true by running the excusion button and reset it to false when you run the stop button. During the condition run you can check for the variable value change.

    Here is an example using your code:

    Private Sub BtnProgress_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPardazesh.Click 
     
            ProgressBar.Visible = True 
            ProgressBar.Minimum = 0 
            ProgressBar.Value = 0 
            ProgressBar.Maximum = FrmMain.DGV.RowCount 
            g_blnRun = true 'global variable that in order to stop the loop you can set it up to false via the btnstop_click
            Dim x As Integer = 0 
            Do While x < Val(FrmMain.ObjDt.Rows.Count)  and g_blnRun is true
                LblPID.Text = FrmMain.ObjDt.Rows(x).Item("Pid") 
                LblFullName.Text = FrmMain.ObjDt.Rows(x).Item("Fname") 
                ViewDetail() 
                ViewAcc() 
                Application.DoEvents() 
                SaveAll(False) 
                ProgressBar ProgressBar.Value = ProgressBar.Value + 1 
                x = x + 1 
            Loop 
    end sub 


    Analyst




    Analyst
    • Edited by tfbasta Thursday, October 16, 2008 8:20 PM
    Thursday, October 16, 2008 8:19 PM
  • Here is a sample to show how to exit the loop by using the way you proposed

    Public Class Form1  
        Private cancelled As Boolean = False 
     
        Public Sub New()  
     
            ' This call is required by the Windows Form Designer.  
            InitializeComponent()  
     
            ' Add any initialization after the InitializeComponent() call.  
            Me.KeyPreview = True 
            ' if you assigned this statement when you press escape key will be  
            ' ignored in the keyDown event  
            Me.CancelButton = Me.btnCancel  
     
            ' both properties above mentioned can be assigned in the properties window  
        End Sub 
     
        Private Sub btnStart_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles btnStart.Click  
            Dim i As Integer = 0  
            While True 
                Application.DoEvents()  
                i = i + 1  
                Me.Text = "Loop No. " + i.ToString()  
                If Me.cancelled = True Then 
                    Exit While 
                End If 
     
            End While 
            Me.Text = "The operation is canclled" 
        End Sub 
     
        Private Sub btnCancel_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles btnCancel.Click  
            cancelled = True 
        End Sub 
     
        Private Sub Form1_KeyDown(ByVal sender As System.ObjectByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown  
            ' This code will be useless if you assigned the CancelButton property  
            If e.KeyCode = Keys.Escape Then 
                cancelled = True 
            End If 
     
        End Sub 
    End Class 


    But i would like to use BackgroundWorker for this case please have a look to the sample i had proposed in this thread
    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/de9b1a91-1ef6-47ea-bb39-5237e07129fb


    Mahmoud Zaben
    • Marked as answer by Edisoon Thursday, October 16, 2008 10:22 PM
    Thursday, October 16, 2008 8:38 PM