locked
How to avoid a Hang while executing a Program RRS feed

  • Question

  • my program(windows from) has a while loop.to complete a loop it takes may be one day or two days.in that execution time when i tried to minimize and opened another program or word document my windows from HANGS.How to avoid the Hang.  
    PS.Shakeer Hussain Hyderabad
    Wednesday, March 17, 2010 4:51 PM

Answers

  • The UI related refreshing is handled by Main Thread. In your case, since the Main Thread is blocked in executing the while loop. It can't handle other events/UI refreshing. To avoid this, try executing the while loop in another thread.

    For example check this piece of code.

    Imports System.Threading
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim methodThread As New ThreadStart(AddressOf MethodToExecute)
            Dim thread As New Thread(methodThread)
            thread.Start()
        End Sub
    
        Private Sub MethodToExecute()
            While True
                If DateTime.Now.Day = 1 And DateTime.Now.Hour = 11 Then
                    MessageBox.Show("1st Day of Month")
                End If
                Thread.Sleep(New TimeSpan(1, 0, 0, 0))
            End While
        End Sub
    End Class
    


    Thanks,
    A.m.a.L
    Dot Net Goodies
    Don't hate the hacker, hate the code
    • Proposed as answer by Brad Lane Wednesday, March 17, 2010 10:54 PM
    • Marked as answer by Helen Zhou Tuesday, March 23, 2010 3:15 AM
    Wednesday, March 17, 2010 5:04 PM