Results 1 to 5 of 5

Thread: Thread.Sleep unresponsive

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2009
    Posts
    10

    Thread.Sleep unresponsive

    Okay, before I am going to post my problem, I am going to explain the project. The coding that I have a problem with might make people think that I am creating some malware or something stupid.
    So the project I am doing is associated with iTunes. iTunes allows people to have one library. There is another way, but it is very confusing to some people, and very unreliable. So my project is that I will allow people to have multiple iTunes libraries on one computer.

    So the problem is this. In order to have an option to choose your library, I gotta make sure that iTunes is not running. So this is the script:

    Code:
        Private Sub CheckAndCloseiTunes()
    retry:
            'Custom class that includes the function to safely close iTunes
            Dim Processes As Process() = Process.GetProcessesByName("iTunes")
            If Processes.Length > 0 Then
                Dim SafeProcessQuit As New VitalMod.VitalOptions
                SafeProcessQuit.CloseApplication("iTunes")
    
                'After every two seconds, makes sure iTunes has exited.
                Threading.Thread.Sleep(2000)
    
                GoTo retry
            Else
                Exit Sub
            End If
    
        End Sub
    So when I use the Threading.Thread.Sleep(2000) code, the form becomes unresponsive. How do I fix the problem?

  2. #2

    Re: Thread.Sleep unresponsive

    Run CheckAndCloseiTunes in a separate thread.

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2009
    Posts
    10

    Re: Thread.Sleep unresponsive

    Quote Originally Posted by formlesstree4 View Post
    Run CheckAndCloseiTunes in a separate thread.
    I just tried that. It would definitely work, but I stumbled upon another problem. Here is the code:

    Code:
        Private Sub Main_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
            'IGNORE THIS PART!!!!
            If iTunesInstalled() = True Then
    
                'HERE IS THE PROBLEM
                Dim iTunesCheck As New SeparateThread
                Dim New_Thread As New Thread(AddressOf iTunesCheck.CheckAndCloseiTunes)
                New_Thread.IsBackground = False
                New_Thread.Start()
    
                'This should only pop up when iTunes is closed.
                MsgBox("iTunes Check Completed")
                'PROBLEM ENDS HERE
            Else
                lstlog.Items.Add("Error: iTunes could not be located!")
                TimerFormSizeChange.Start()
            End If
        End Sub
    Then a new class with the function.
    Code:
    Public Class SeparateThread
    
        Public Sub CheckAndCloseiTunes()
    retry:
            Dim Processes As Process() = Process.GetProcessesByName("iTunes")
            If Processes.Length > 0 Then
                MsgBox("iTunes running. Recheck in 10 seconds.")
                Dim SafeProcessQuit As New VitalMod.VitalOptions
                SafeProcessQuit.CloseApplication("iTunes")
    
                'After every two seconds, makes sure iTunes has exited.
                Threading.Thread.Sleep(10000)
    
                GoTo retry
            Else
                MsgBox("iTunes closed...")
                Exit Sub
            End If
    
        End Sub
    End Class
    So now the problem is this. I open iTunes and run the script. Here is what it should do:

    1) It calls the subroutine CheckAndCloseiTunes()
    2) CheckAndCloseiTunes noticed that iTunes is running, notifies the user that iTunes is open. Attempts to close iTunes(it does).
    3) Waits 10 seconds for another recheck.
    4) Rechecks again, sees that iTunes is closed, sends a message that says iTunes is closed.
    5) Then the Main_Show script continues where it shows the message "iTunes Check Completed"

    But my results were not what I had in mind. It shows the messages in these order:
    1) "iTunes running. Recheck in 10 seconds." and "iTunes Check Completed" at the same time.
    2) 10 seconds later, it shows me this: "iTunes closed..."

  4. #4

    Re: Thread.Sleep unresponsive

    Keep the thing in a do loop instead of using GoTo's.

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2009
    Posts
    10

    Re: Thread.Sleep unresponsive

    Quote Originally Posted by formlesstree4 View Post
    Keep the thing in a do loop instead of using GoTo's.
    Yes, but you cannot stall the script for one second or 10 before reloading. Do Loop just reloads the script again and again. That could cause the same problem I am experiencing with unresponsive form.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width