Results 1 to 5 of 5

Thread: Pause a Loop

  1. #1
    shibity is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Jan 2009
    Posts
    1
    Reputation
    0

    Pause a Loop

    Hey guys, im new here but so far it looks like a good place. Right now i need help with my program. I have it looping and im trying to pause the loop for several seconds. Heres my code
    Code:
    For x = 1 To Text3.Text
    If Check1.Value = 1 Then
    SendKeys Text1.Text
    SendKeys "{Enter}"
    End If
    Sleep (200)
    If Check2.Value = 1 Then
    SendKeys Text2.Text
    SendKeys "{Enter}"
    End If
    Now sleep workd, But what it did was pause the whole program, instead of pausing between each loop. Any ideas,
    Thanks,
    Dave

  2. #2
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,036
    Reputation
    1722
    Quote Originally Posted by shibity View Post
    Now sleep workd, But what it did was pause the whole program, instead of pausing between each loop. Any ideas,
    Thanks,
    Dave
    That's because your program only has one thread, that thread is running the loop code, and sleep slepps the thread therefore sleeping the loop code but also the whole program

    You need to alter your thinking to be event based rather than loop based. You seem to initiate an action only when a checkbox is checked, so attach an event handler to its checkedchanged event!

    Also if you must do something, then wait several seconds, then do something else, start a timer and after X seconds have elapsed its Tick event will fire, and in the Tick event handler you can do your subsequent processing


    -

    You can also use a BackgroundWorker, which has a separate thread. You can sleep that without upsetting the main program, but then you open the Multithreading can of worms.. Additionally, I tend to recommend AVOIDING use of thread.sleep for situations other than simulating heavy load

  3. #3
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,611
    Reputation
    2738
    Quote Originally Posted by cjard View Post
    Additionally, I tend to recommend AVOIDING use of thread.sleep for situations other than simulating heavy load
    Why? You often need to in polling loops that that don't need to go CPU berserk and ask for info every nanosecond, but perhaps could benefit of sleeping 500ms each loop, you'd still have fast reliable response at least twice a second. You may not want to let any bit of this polling work to take place in UI thread as with a Timer. Yes, you could use a Threading.Timer that callback on a new Threadpool thread each time, but in this case using a single long running thread is better thread management, it could also avoid sync problems for multiple threads in case poll gave result.

  4. #4
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,036
    Reputation
    1722

  5. #5
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,611
    Reputation
    2738
    That post mostly talks about sleeping UI thread is bad (or COM STA threads), I agree, but that is not the scenario here. The argumentation about create/destroy time is actually an argument for using Sleep for long running threads, and avoid creating other threads. With "A WinForm thread must be STA" he means UI thread. "STA" actually only has a meaning when using COM and only if a COM call is made is the thread initialized for STA. So a thread may also be requested for STA, but may not enter that apartment. New threads defaults to MTA by the way, and is the case discussed here. Message pumps also have to be created, a thread does not start one by itself. A winforms application is configured to start one by default, the application startup thread is also marked STA for measures. If you run UI elements in other threads they communicate via main message loop, either in MTA or in STA or across MTA/STA border. Only if a thread creates windows is a message queue created for the thread. I don't see any arguments against using Sleep in a polling scenario as described, nor any better solution proposed. Sleep is not used for timing. A WaitHandle timeout will use a threadpool wait thread, why call another idle thread or have a new thread created multiple times a second when you only need one thread? Sleep is not a never-never as I see it.

Bookmarks

Posting Permissions

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