tonofsteel Posted July 23, 2003 Posted July 23, 2003 The program I am creating needs to have a delay in opening the main form. The program starts and tries to read default settings from a file, if these settings are not found an error message comes up, and the setup window shows up to enter the values in manually. This all needs to be done before the main form pops up. I put the code for detecting these conditions in the Main forms load event. I know there was stuff in VB6 such as SetWaitableTimer and such. But in that function it used DoEvents, where did that go in VB .net? What is there to use now to stop the program temporarily until a certain condition is met??? Quote
Hamburger1984 Posted July 23, 2003 Posted July 23, 2003 two possibilities: 1 - I guess you open a Dialog to set these values you need when they aren't set. you could stop the prog by showing this Dialog with MySettingsDialog.ShowDialog(). this would cause the app to wait until the Dialog is closed again. 2. - the "dirty" possibility would be writing a loop which runs until the settings are set - with a Threading.Thread.Sleep(500) call at the end which sends the current app to sleep for half a second before the loop is entered again, checking if the values are set now and exit the loop if they if not run the loop etc.... Hope this helps! Andreas Quote
*Experts* Volte Posted July 23, 2003 *Experts* Posted July 23, 2003 DoEvents in .NET is System.Windows.Forms.Application.DoEvents(), but most of the time you can just use Application.DoEvents(). Quote
tonofsteel Posted July 23, 2003 Author Posted July 23, 2003 Thanks for the help so far. I am thinking of headin in the route of the dialog box approach. But before i do all that, there is not equivalence in VB.net with commands like SetWaitableTimer or anything like it that will pause the program until a condition has passed. I have hunted for code for VB.net with no results. Quote
Hamburger1984 Posted July 23, 2003 Posted July 23, 2003 you can write something like this - but as far as I know there's no vb6 SetWaitableTimer-Function in vb.NET... an example to fake this behavior: Dim ExitLoop as Boolean = False While Not ExitLoop Application.DoEvents() Threading.Thread.Sleep(500) End While ..but you have to make sure that at some point the loop will be exited (ExitLoop = True)!! So the best way would be declaring ExitLoop in the Form (not in the Function..) and set it to true from the outside at some point. Quote
tonofsteel Posted July 23, 2003 Author Posted July 23, 2003 Thank you alot for the help with that bit of code. It was simple but I am so used to the old ways of VB. Anyways i found that when i used the code you suggested there were some *small* delays when typing or selecting files in dialog boxes. So if you remove the line ----> Threading.Thread.Sleep(500) <---- it all runs smoothly. The ExitLoop = true is in the close event in the form i load Dim ExitLoop as Boolean = False While Not ExitLoop Application.DoEvents() End While Quote
Kurt Posted July 29, 2003 Posted July 29, 2003 Yes, but your processor will be used for the full 100% (running through the waiting loop without any pauses) while the user gives in the setup data. I would advice you to work with the showdialogue methode, or figure out if it is possible to run the form in which you give in the setup data on a thread, seperate from the thread on which the Main form runs... Quote qrt
tonofsteel Posted July 29, 2003 Author Posted July 29, 2003 If you put an empty loop, it locks up the program because it is using 100% of the processor and never leaves. With the original code you would go to click but the program would be sleeping and whenever you would want to do something with the form there would be endless little pauses and freezes. By getting rid of that it does not freeze pause or even slow the computer down. If the program was using 100% of the processor you would not be able to click buttons without any delay or type text without there being a pause between what you type and what shows up. I was thinking that too, but try it, and see what happens, you may be surprized. It almost seems like that statement is saying, while ExitLoop is false pause the program, and after that does not loop, just waits for the exit loop to change to true Quote
*Gurus* Derek Stone Posted July 30, 2003 *Gurus* Posted July 30, 2003 Do things the correct way and use Sub Main(). Private Sub Main() If bShowSettings Then 'Show the settings dialog modally End If mainForm.Show() End Sub 'Main Quote Posting Guidelines
JoeNuvo Posted July 30, 2003 Posted July 30, 2003 Don't sure it good or not but I just try loop with DoEvents Sleep 1 seem it work. a little bit delay compare run without "sleep" but CPU never peak. (without sleep my process end in 11227 TickCount with sleep my process end in 12786 TickCount) But what I actually want (I think tonofstell also) is some code which can act as "Timer" control. Timer control never eat CPU and come back to do the work at adjustable period. Just want something like that in Code. Quote
Nazdor Posted August 28, 2003 Posted August 28, 2003 There should never be any need to sit and wait for something to happen. Instead, start that something and exit - and the end of the 'something' run the next step, eg (pseudo): Startup Form = frmSetup frmSetup_Unload (handles MyBase.Closed) Load frmMain or use Derek Stone's idea Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.