Results 1 to 8 of 8

Thread: System.Threading.Thread.Sleep [Resolved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2003
    Posts
    127

    System.Threading.Thread.Sleep [Resolved]

    After some trial and error I found some articles about using the below code to delay an action from occuring.

    Code:
    System.Threading.Thread.Sleep(1000)
    Sure it works to delay but it from what I gather only works on Win2k and XP and it puts the system to sleep for the specified time. "Meaning that nothing else will run" while it's sleeping.

    Is there a better way to get a delay between two actions taking place and allow other actions to take place too?

    I'm simply trying to show one picture box, hide it and then show another picture box beside it, hide it and then show another picture box beside it.
    Last edited by teamdad; Jun 22nd, 2004 at 09:31 AM.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by teamdad

    Code:
    System.Threading.Thread.Sleep(1000)
    Sure it works to delay but it from what I gather only works on Win2k and XP...
    Who said that ??
    This is from MSDN Help :

    Thread.Sleep Method (Int32)

    Platforms Supported: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2003
    Posts
    127
    Point taken.....

    My info must have been from a bad source. It still kills anything else from happening though from my experience. For example setting picture box 2 and picture box 3 properties to Visible/False and running the below code does not show the picture boxes going from visible to hidden, visible to hidden as it should.

    Code:
    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
    
                PictureBox2.Visible = True
                System.Threading.Thread.Sleep(1000)
                PictureBox2.Visible = False
                System.Threading.Thread.Sleep(1000)
                PictureBox3.Visible = True
                System.Threading.Thread.Sleep(1000)
                PictureBox3.Visible = False
    
    End Sub
    I have toyed around with the timer as you had suggested in your other reply to my picture box timer thread but didn't have any luck with it either. Any suggestions using the System.Threading.Thread.Sleep method?

  4. #4
    Member
    Join Date
    May 2001
    Location
    North East Germany
    Posts
    46
    Hi,

    i'm using this method for my thread variables.

    Example:
    In my mp3 Player is a 'fadein' Function.

    VB Code:
    1. Private thFader as System.Threading.Thread();
    2.  
    3. Private Sub StartFadeIN()
    4.   thFader = New System.Threading.Thread(AdressOf (FadeIn))
    5.   thFader.Start();
    6. End Sub
    7.  
    8. Private Sub FadeIn()
    9.  do while mp3player.volume < 100
    10.   mp3player.volume += 1
    11.   thFader.Sleep(100);
    12.  end while
    13. End Sub

    (This Code is not Copy pasted, only from brain memory, it's bad )


    There is no Problem, the System runs perfectly, while the thread is fading the volume.

    Maybee you need another thread to start and use 'join' or something else to let the main thread wait for the other thread.
    http://www.fin-sn.de

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by teamdad
    Point taken.....

    My info must have been from a bad source. It still kills anything else from happening though from my experience. For example setting picture box 2 and picture box 3 properties to Visible/False and running the below code does not show the picture boxes going from visible to hidden, visible to hidden as it should.

    Code:
    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
    
                PictureBox2.Visible = True
                System.Threading.Thread.Sleep(1000)
                PictureBox2.Visible = False
                System.Threading.Thread.Sleep(1000)
                PictureBox3.Visible = True
                System.Threading.Thread.Sleep(1000)
                PictureBox3.Visible = False
    
    End Sub
    I have toyed around with the timer as you had suggested in your other reply to my picture box timer thread but didn't have any luck with it either. Any suggestions using the System.Threading.Thread.Sleep method?
    I find that when using Thread.Sleep as a program delay mechanism, you have to update the relevent object before using Sleep.

    PictureBox2.Visible = True
    PictureBox2.Update
    System.Threading.Thread.Sleep(1000)
    PictureBox2.Visible = False
    PictureBox2.Update
    System.Threading.Thread.Sleep(1000)
    PictureBox3.Visible = True
    PictureBox3.Update
    System.Threading.Thread.Sleep(1000)
    PictureBox3.Visible = False

    You can also use the DateTime functions:

    VB Code:
    1. PictureBox2.Visible = True
    2.             Delay5(5)                      
    3.             PictureBox2.Visible = False
    4.             Delay5(6)
    5.             PictureBox3.Visible = True
    6.             Delay5(7)
    7.             PictureBox3.Visible = False
    8.  
    9.  
    10. Public Sub Delay5(iDelay As Integer)
    11.         Dim dt1 As DateTime=now
    12.         Do
    13.             If Now > dt1.AddSeconds(iDelay) Then Exit Do
    14.         Loop
    15. End Sub
    Last edited by taxes; Jun 22nd, 2004 at 01:19 PM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Dec 2003
    Posts
    127
    I am so thankfull that there are a bunch of people all over the world that can get along and help eachother code in peace.

  7. #7
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by teamdad
    I am so thankfull that there are a bunch of people all over the world that can get along and help eachother code in peace.
    Watch the threads!! Gets a bit heated sometimes but it never comes to blows (and we usually end up realising we are all human )
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  8. #8
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi teamdad,

    I've just noticed my suggested code for using DateTime did not reset the variable. I have edited that post accordingly and it now allow you to choose a variable delay period.

    By the way, when you find some suggested code works (or does not work) it helps if you make a reference to the posting which contained that code. Otherwise we are not sure of the code to which you refer.
    Last edited by taxes; Jun 22nd, 2004 at 01:22 PM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

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