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...
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:
Private Sub Form_Load....
Dim thrWork As New Threading.Thread(AddressOf WorkSub)
thrWork.Start
End Sub
Private Sub WorkSub
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.
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.
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.
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
Re: how to wait in 2005 Express
Answers come quickly around here.
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.