开发者

What is the C# equivalence of startup module we had in vb6.0?

开发者 https://www.devze.com 2022-12-15 13:24 出处:网络
What is the win-based application developing wi开发者_开发百科th C# equivalence of startup module we had in vb6.0? a static class ? or what?In the properties page under \"Application\" there is a sett

What is the win-based application developing wi开发者_开发百科th C# equivalence of startup module we had in vb6.0? a static class ? or what?


In the properties page under "Application" there is a setting called "Startup object" which can be used to set the object with a static method named Main that will be called. This is a simple UI on the C# compiler's /main switch.


Yes, a static class named Program with a static Main() function. It is hard to miss when you look at the code auto-generated by a project template. Open Program.cs


it is static void Main() {}, which can be found in different places depending on what type of app it is e.g.:

Winforms - it is usually found in Program.cs
WPF - it is usually found in App.cs


too long for a comment

@odiseh

You almost certainly are talking about a Windows Forms project : and most of the comments here did /are going to/ assume you are using Visual Studio, as I will. If you are using another development environment, then please let us know : that may be relevant (?).

So, create a sample WinForms project in Visual Studio : look at the Program.cs file and its structure : that's the default, as other answers here have pointed out. My guess is that : if you are "coming from" VB6, you already know what the [STAThread] attribute is about, and what "single apartment threading" is.

The default startup format embodied in "Program.cs" is not, however, the only game in town : check out ApplicationContext: MSDN .NET 3.5 ApplicationContext : note that the usages I've seen of this are pretty rare, and often exotic : if I remember correctly, Chris Sells' book on Windows Forms had an interesting ApplicationContext example.

There are some circumstances in which you may wish to modify what's happening in the default Program.cs static class : for example you can show multiple Forms, and use Application.Run() which will mean that when all the Forms you've shown are closed, your Application will be still running, but with no UI (a problem you can handle with writing code for the FormClosing events and "doing the right thing").

Even SO's very own (highly esteemed) Marc Gravell once published, on another site, an example of an interesting use of NotifyIcon with a modified Main method : some choice "early vintage" Gravell

And do please ignore the horrible "smell" coming from the bizarre example I post below of what "you can get away with" : if there were any justice in the world, that example should result in a "blue screen of death," at least.

comments :

@Martinho : you are correct when you imply there is no "static" class absolutely required for a WinForms project startup. But I must respectfully disagree with the statement there's no "class" requirement. The static method Main, imho, must be inside a Class !

A brief demonstration of what a "monstrosity" you can actually "get away with" in .NET 4.0 : this will actually compile, and run, and put up an instance of Form1 :

using System;
using System.Windows.Forms;

// your mother told you to never do this !

namespace WindowsFormsApplication7
{
    public struct Class1
    {
        public static int Main(string[] someWastedBunchOfStrings)
        {
            (new Form1()).ShowDialog();
            return 1;
        }
    }
}

Kids : please don't try the above at home : they'll put you on Ritalin or Prozac :)


static void Main() within any static class, but you can change it, as you could change entry point in VC6.

0

精彩评论

暂无评论...
验证码 换一张
取 消