pro_vb_dotnet thread: RE: Passing control from one windows form to another - a new angle
Hi, this API call should stop the 100% consumation of CPU resources.
Declare Function WaitMessage Lib "user32" () As Long
Just call WaitMessage in either the line before or the line after the call
to DoEvents. This should force the application to only consume CPU
resources when an event is raised.
Hope this helps (dont know if the API call will be in your compact
framework though)
Regards,
Phillip
> Hi again,
By the way, I just tried Phillip's suggestion, albeit modified a little. =
Basically:
1. I changed the app to use a 'sub main' procedure as a startup, instead =
of a form
2. I altered my form definition so that when the user wants to exit the =
whole application, it throws a custom event called ExitApplicationEvent=20
3. In my main() sub, I add an event handler to consume this event
4. To start the app i create my first form and then wait for the =
ExitApplication event to get raised.
5. When it does, main() runs to completion and the app terminates
Code is (basically):=20
Public WithEvents frmNewForm As frmBaseForm
Private _ExitFlag As Boolean
Sub main()
frmNewForm =3D New frmRootMenu()
frmNewForm.Show()
_ExitFlag =3D False
AddHandler frmNewForm.ExitApplicationEvent, AddressOf =
ExitApplicationHandler
Do While _ExitFlag =3D False
System.Windows.Forms.Application.DoEvents()
Loop
End Sub
Public Sub ExitApplicationHandler(ByRef Cancel As Boolean)
_ExitFlag =3D True
End Sub
When I run this, it works BUT the major drawback is that the 'Do While' =
loop consumes 100% of the CPU while it waits for the user to exit the =
app and raise the event its waiting on.=20
Also, I explored the system.threading namespace and found the 'sleep' =
method. I thought I might be able to use code similar to the above =
except that instead of the Do While loop, I would call Sleep to make =
main() sleep indefinitely. Then, I would change my exception handler so =
that when ExitApplicationEvent fired, it would wake up main() again by =
calling the thread 'interupt' method, causing main() to run to =
completion and terminate the application as per normal. That didn't work =
either, as since I was putting the main thread to sleep, no forms events =
got processed so the form never got initialised and so was un-useable.
I will keep looking.
Brett