Application.DoEvents

b3jsd73jfx

Active member
Joined
Mar 6, 2006
Messages
28
Programming Experience
1-3
Alright, I have a situation that I need help with (obviously). I have a program that starts from Sub Main in Module1.vb. Here's what that subroutine looks like:
VB.NET:
[SIZE=2][COLOR=#0000ff]Public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Main()
[/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff] Dim[/COLOR][/SIZE][SIZE=2] reader [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] IO.StreamReader = IO.File.OpenText("v.dat")
    checkinterval = reader.ReadLine
    autoupdate = reader.ReadLine
    Browser = reader.ReadLine
    AutoCheck = reader.ReadLine
    MinStart = reader.ReadLine
    reader.Close()
[/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Writer [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] IO.StreamWriter = IO.File.CreateText("v.dat")
    checkinterval = 900000
    autoupdate = "no"
    Browser = "c:\program files\internet explorer\iexplore.exe"
    AutoCheck = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2] MinStart = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2] Writer.Close()
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Form1 [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Form1
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] MinStart = [/SIZE][SIZE=2][COLOR=#0000ff]True [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2] Form1.WindowState = FormWindowState.Minimized
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][COLOR=#000000][SIZE=2]Form1.Show()
[/SIZE][SIZE=2][COLOR=#0000ff]Do
[/COLOR][/SIZE][SIZE=2] Application.DoEvents()
[/SIZE][SIZE=2][COLOR=#0000ff]Loop [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE][SIZE=2] Form1.Visible = [/SIZE][SIZE=2][COLOR=#0000ff]True [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Or[/COLOR][/SIZE][SIZE=2] Form1.tskIcon.Visible = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][/COLOR][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
Now the code works fine as is except when I run it, it takes up 100% of my CPU resources. It's the application.doevents that's causing the problem. I've tried taking that out, but then my program just ends after it shows that first form. Is there a way to pass control over to Form1 so that when Sub Main ends my program will continue running?
 
Replace Form1.Show() with Form1.ShowDialog() and remove the Do..DoEvenets..Loop and you should be fine.
 
No, I can't use form1.showdialog. This is a program that when not being used, sits in the system tray. If I use showdialog then it doesn't work properly.
 
i apologise but it's early, but i'm struggling with what yuor trying to do. You want you program to run in the system tray, so does the application.doevents actually do?
Also in your sub main you should specify this with an attribute to indicate that this operates in a single apartment thread. Also rather than catching the exception if the sfile doesn't exist, first check if it does with

VB.NET:
if system.io.fileexixsts...

It'll save all that overhead with throwing an exception
 
Back
Top