Sign in to follow this  
Seroja

[.net] VB.Net Sub Main problem [Solved]

Recommended Posts

Seroja    280
I have tried to start my program from Sub Main. Everything went well, and about the following code worked: MainForm = New frmMain MainForm.Show Do While MainForm.IsDisposed = False Application.DoEvents Loop It still gave some trouble sometimes on the show call, saying no such object (e.g. sometimes it stuck on the show code until the form terminated), but overally it was fine. After several changes to my program, including moving some code into a separate class library, this ceased to work. It now gets stuck on the Do..Loop, and when it is supposed to execute the rest of the program during DoEvents, it doesn't :( I have tried several solution I found on the net, like using Application.Run instead of Show, adding Exit Sub in the end, spawning a thread and waiting for it to terminate, rmoving the loop etc. All of those, either got stuck in some loop instead of executing the program, or exited immediately (in VB.Net, the application terminates when Sub Main ends, unlike VB6, regardless if you have any forms running). This problems also gives me an ide why I couldn't run the program using Mono and Portable.Net. It seems now weird that it worked at all. By the way, I'm using VB.Net 2005 Beta 1. Haven't tried this on VB.Net 2003, but I think it won't work there as well. Is there any neat solution to this? A workaround would be creating a loader form that would launch Sub Main, load my form from there, and then terminate the loader (compiling with option of ending program when all forms close). But I would like to load from Sub Main, it looks nicer to me, and it also allows me to create a global form variable, so I can access all forms from all modules. In the end I migh even avoid using WinForms at all (after all, I just need them for my context). Thanks in advance :) [Edited by - Seroja on May 27, 2005 8:39:12 AM]

Share this post


Link to post
Share on other sites
DaWanderer    538
First off, why don't you use Application.Run to start the Windows application? Finally, if you must do it that way, use the IsCreated property of the main form instead of IsDisposed.

Share this post


Link to post
Share on other sites
Seroja    280
I tried Application.Run as well, and it didn't matter much. Don't know what IsCreated is, but I want to loop until the form is disposed. And the problem was not with the loop not working, but with DoEvents not doing any events.....

Just came upon an idea while writing the last sentence... DoEvents did a few events at the beginning, but it got stuck when a command has been called, which is in the same module... Now, I wonder if splitting into several modules will help... Anyway, why doesn't same-module multithreading work?

Share this post


Link to post
Share on other sites
jods    367
Note: I don't know VB.net well, because I use C#.

You do it the wrong way. A few reminders:

Application.DoEvents make the thread process any events (windows messages) pending, and returns. Usually, you would call it in a long processing method, to keep your application responsive.

Here, you are calling it in a loop, you will make the CPU usage 100%, because there is no blocking call. Using 100% CPU to poll if there are events or if a propriety is False is obviously not something you'd want.

When you close a form, Windows.Forms doesn't Dispose it. It just hide it reuse the Win objects if you show it again. That's why the IsDisposed exit condition doesn't work.

The right way to do it is as follow (sorry if the syntax is a bit wrong):

MainForm = New frmMain
Application.Run MainForm




You said Application.Run didn't work for you, but it should. I suppose it's because you put it in the loop. DO NOT. Application.Run is a blocking call that will return when the form is closed. So you don't need the loop anyway (and the exit condition still wouldn't work, see above).

Share this post


Link to post
Share on other sites
Seroja    280
No, it wasn't the problem. IsDisposed, DoEvents and the rest - all should have worked (and are working now :)

The problem was about moving some of the code to an external class library. When adding it as a reference, I didn't check copy local. VB.Net didn't tell me it couldn't find it (I wonder what does copy local really do). But when running, any attempt of calling a sub/function in a module that uses that class library resulted either in DoEvents halt, or Application.Run thinking the form unloaded. That was really hard to track down, I was lucky to get FileNotFound exception when trying to run the program using threads.

Anyway, solved now. Thanks to everyone who helped :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this