Where is the main function in VB.Net
I have taken over support of a VB.Net WinForms application. I am actually a c# developer and am more familiar with the setup of visual studio projects in c# projects. Now I am trying to determine why my application is crashing on a specific XP installation, and I read the suggestion here
http://social.msdn.microsoft.com/forums/en-US/winformssetup/thread/53c2de93-ab33-41d0-b5dd-7ca5fbfa5c24/
to add a try catch block in the main function. This is suggested in about the 5th post from the bottom. (I will quote it below) However, if I look in the VB.Net visual studio project, I do not find a Main() procedure. What I do find is a grey folder called "My project" with a "Application.myapp" file inside it. This file has an associated designer file, but if I click on it I see the following xml:
<?xml version="1.0" encod开发者_Go百科ing="utf-8"?>
<MyApplicationData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MySubMain>true</MySubMain>
<MainForm>MDIMain</MainForm>
<SingleInstance>false</SingleInstance>
<ShutdownMode>0</ShutdownMode>
<EnableVisualStyles>true</EnableVisualStyles>
<AuthenticationMode>0</AuthenticationMode>
<SaveMySettingsOnExit>true</SaveMySettingsOnExit>
</MyApplicationData>
So can anyone enlighten me to where the actual main procedure call is for this VB.Net project so that I can try catch the exception that is occurring. If, as I suspect, there isn't actually a Main procedure in my VB.Net project, can someone maybe let me know how I can go about doing the following in my project:
[STAThread]
static void Main()
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
catch (System.IO.FileNotFoundException ex)
{
MessageBox.Show(ex.Message + " \n\n\n" + ex.StackTrace);
}
}
VB has a special mode called “Application Framework” (which can be found under the main options).
If this mode is enabled, the compiler auto-generates a Main
method and some fluff around it. You can disable this option; however, this may cause problems in the project since the application framework functionality might actually be used by the project.
Alternatively, you can register an event handler for uncaught exceptions (UnhandledExceptions
) using this same application framework.
The more VB way to do this is to open the Application properties and click on the ViewApplicationEvents button. This will open the Application.xaml.vb file where you can add custom event handlers for the application. Select Application Events from the left drop-down and you can easily access a bunch of events including DispatcherUnhandledException, Activated, Navigating, Startup, Exit, etc. You can also add the Main method here by selecting Applciation from the left drop-down and selecting Main from the right drop down.
In the case of WindowsForms applications, the process is similar. However when you select the Applciation Events button, the file that is shown is the ApplicationEvents.vb file. In here, to add a global error handler, select the left drop-down and select MyApplication Events. Then in the right drop-down, add the UnhandledException handler. You can also create your Main method here as well.
It is generated automatically by the compiler when it can't find one, but you can create one yourself.
http://msdn.microsoft.com/en-us/library/ms235406%28v=VS.100%29.aspx
http://msdn.microsoft.com/en-us/library/y4bwckbb.aspx
I came to this page today in search of answers, and I found some good ones, both here and in The Code Project.
By the time I satisfied myself that I knew what to do, I also had in hand a simplified approach that leaves the project properties virtually untouched. (You must turn off the Application Framework, or the VB runtime won't run your Main routine!) In a nutshell, if you define your Main
routine in the class module that defines your startup form, the Visual Basic runtime engine will find and execute it.
As is stated above, your routine must be defined as Shared. You can see my example, along with a few other notes, at How to Run a Particular Form in VB.NET
Caveat
When you disable the Application Framework, you lose the Single Instance check box. I just finished updating the cited example to include the code that I developed and tested to enforce single instance.
You can create that method anywhere, as long as it's Shared
. To wire it up, you have to go into the project settings and set the entry point to be your Main method.
精彩评论