Results 1 to 7 of 7

Thread: how to wait in 2005 Express

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    2

    how to wait in 2005 Express

    Hey guys I'm brand new to VB.NET and I have a question about a program I'm creating. Basically my program gets an xml file from the internet and then saves certain parts of it when the user clicks a start button. I want to have the routine wait for approx 5 mins, during which time I want the user to be able to enter information and click other buttons on the form, and then I want the program to loop back to the beginning of the sub to check for updated info.

    My problem is I can't figure out how to make the routine wait and still allow full functionality of my form. I have tried Threading.Thread.Sleep but it basically freezes the rest of the form until the specified sleep time is over.

    Is there any way to make the sub wait while other subs and such can be executed?

    Thanks for the help

    -NDScrackbaby

    Edit: Sorry If this gets posted like 3x times, New Thread wouldn't go through till just now. Sorry Mods...
    Last edited by NDScrackbaby; Feb 26th, 2007 at 08:16 PM.

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: how to wait in 2005 Express

    Im off to bed so I'll just throw in some quick help:

    The sleep function you used will put you main thread to sleep, if its called on the main thread. You will need to put your "heavy" code in its own thread. That way you can put it to sleep without disturbing the user interface. This is how to start a subroutine in a new thread:

    VB Code:
    1. Private Sub Form_Load....
    2.    Dim thrWork As New Threading.Thread(AddressOf WorkSub)
    3.    thrWork.Start
    4. End Sub
    5.  
    6. Private Sub WorkSub
    7.  
    8. End Sub

    Search these forums for Threading for more info. You cant access controls from another thread than the one thread they where created on, so you will also need to search this forum for "Invoke" or "Invoking" to find the answer.

    Also, a simpler method is to use the BackgroundWorker component. But I personally just dont like it

    If anything is unclear try searching MSDN, time for me to sleep.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    Junior Member
    Join Date
    Jul 2006
    Posts
    29

    Re: how to wait in 2005 Express

    It sounds like you need to have more than one thread. Put the code that you want to wait in it's own thread and pause that. This will allow your main thread (containing your form) to still accept input from the user.

    Without knowing the full story, I think you may be going about this the wrong way though. Why do you want to wait some arbitrary amount of time for user input? Why not use one of the events from the control the user is editing? The events Leave and LostFocus come to mind for a text box. Put the code you want to execute in the event handler for that control.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: how to wait in 2005 Express

    It doesn't sound multithreaded to me, it sounds like a normal program:

    You have a form with a bunch of controls and what not. One of those controls is a button that you click. When you click the button, it calls a sub that goes and gets an XML file, and does some funk with it. Then you want to wait. Well....guess what...you ARE waiting. So why not just have the button click event call a sub that does the work of getting the XML, then start a timer which counts for five minutes, and on the timer Tick event, call the sub again to check for updates.

    This is a much simpler solution than actually making the thread wait. You want updates every five minutes? Then just have a timer and call the routine to check for them every five minutes.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    2

    Re: how to wait in 2005 Express

    wow I cant believe all the help I received in such a short amount of time. Thanks a bunch guys. Shaggy: that seems to make the most sense, its kinda sad I didn't think about that when I was originally messing around with timers.
    Thanks again for all the help, Ill try out the timer first, and then the others if for some reason I cant get that working.

    -NDScrackbaby

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: how to wait in 2005 Express

    Answers come quickly around here.
    My usual boring signature: Nothing

  7. #7
    Lively Member
    Join Date
    Jun 2006
    Location
    City of Angles. Right Angles.
    Posts
    110

    Re: how to wait in 2005 Express

    And just a little extra info, the BackgroundWorker object is much easier to work with than creating your own threads using Threading.Thread.

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