Nathan_

I'm working on an application that works on a plugin type architecture.

The main app has an mdi form that contains a navigation form (host) and nothing else, at start up I create an instance of a singleton that handles the communication between the host and the plug-ins (loading, disposing etc).

I would like to be able to make the singleton the startup object to place all logic in relation to what is displayed etc in one place and to make it easier for extension in the future when the requirement comes to be able to have a form that doesn't use the host.

Unfortunately I'm having a problem finding out how to make the singleton the startup object.

Anybody got any pointers

Cheers


Re: Visual C# General Singleton as startup

IanG

Typing "C# singleton" into Google provides quite a lot of information on how to implement singletons in C#.

Presumably you already tried that though. Could you be specific about how the information you found on the web didn't meet your requirements





Re: Visual C# General Singleton as startup

Nathan_

The singleton is not the problem.

The problem is making it the startup object of the application rather than a windows form.

So in a normal Winforms app in program.cs you'll see

Code Snippet

Application.Run(new Form1());


Well obviously I can't use that with a class, unless I inherit form which I don't really want to do, so I'm asking if there is a way that I can make the singleton class the startup object.

I have googled on this but haven't found any articles that actually mention doing this.




Re: Visual C# General Singleton as startup

IanG

Are you aware that you can just call Application.Run(); with no parameters at all

The application will then run for as long as you want - until you call Application.Exit(). The idea of there being a 'main form' is convenient for single-window applications, but if that pattern doesn't suit you, you're not obliged to use it. And it sounds like you want to do something different from the normal pattern.

You're allowed to modify the code in program.cs. Why not just modify that code to ensure that your singleton is created, and create whatever forms you want in there

The singleton pattern is something you just go ahead and implement - you don't need to fit into some Windows Forms-specific way of doing it. Just implement it as normal. You take control of Windows Forms - you don't need to let it dictate terms to you.





Re: Visual C# General Singleton as startup

Nathan_

Ian

Thanks for the reply.

I had no idea you could call run with no parameters.

That sound exactly like what I want to do, I'll give it a go and see how I get on.

Thanks again




Re: Visual C# General Singleton as startup

czaval

Hi,

After 'Application.Run(new Form1());' was called a constructor of 'Form1' will be executed. If I understand you right, you want to apply 'Singleton' pattern on 'Form1' . You should change 'Application.Run(new Form1());' to 'Application.Run(Form1.GetInstance());' after you added 'GetInstance' function:

private static Singleton instance;
private static int numOfReference;

public static Form1 GetInstance()
{
if(instance == null)
{
instance = new Form1();
}


numOfReference++;
return instance;
}

and change toe constructor of 'Form1' to private for controlling the single object creation:

private Form1 ()
{
numOfReference = 0;
}

It must work.