C # software Startup design

Source: Internet
Author: User
Design the purpose of this article: according to the recent development of C # software, for software launch design to talk about my experience.

The following code is the class that I designed to launch the software, and the application portal is inside this generic class.

Using System;
Using System.Threading;
Using System.Windows.Forms;

Namespace MainClass
{
public class Mainapp
{
private static Mutex Mymutex;
private static bool Requestinitialownership = TRUE;
private static bool mutexwascreated;

/*

The above section is the mutex that declares the process

*/

[STAThread]
static void Main ()
{
Try
{
Mymutex = new Mutex (requestinitialownership, "Test", out mutexwascreated);
if (!) ( Requestinitialownership && mutexwascreated))
Mymutex.waitone ();
Else
New Mainapp ();

/*

Here is the implementation of the process mutex. I've seen some people write about mutual exclusion, and they're using it to see if there's a process to start in the current process, and see if it's the same directory as the process you want to run.

In fact, this does not seem to solve the problem completely, for example, if I renamed the program, soft after the directory so that can be run, and the time complexity is too high.

And the above code:

Mymutex = new Mutex (requestinitialownership, "Test", out mutexwascreated);


Here is the request for a named mutex, and returns whether there is already an application with the same name.

if (!) ( Requestinitialownership && mutexwascreated))
Mymutex.waitone ();


If the mutex has already been applied, block the program to run.

*/
}
}
catch (Exception ed)
{
MessageBox.Show (ed. ToString (), "wrong convention", Messageboxbuttons.ok,messageboxicon.error);
System.Environment.Exit (0);
}
}

Public Mainapp ()
{

/*

The following are the startup classes

*/
Try
{

This window is the initialization window, or the software wrapper.
MainClass.InitializeForm.InitializeForm initializeform = new MainClass.InitializeForm.InitializeForm ();


Add a validation program to the initialization window, which is typically used to initialize the database
string result;
if (result = Initializeform.starttest ())!= "")
throw new Exception (result);
Else
Initializeform.close ();

End Initialization window, finally enter main window

Application.Run (New Mainform.mainform ());
}
catch (Exception ed)
{
MessageBox.Show (ed. ToString (), "#error #", Messageboxbuttons.ok,messageboxicon.error);
Environment.exit (1);
}
}

/*

The above code can achieve all the software startup function, if you want to fill in the landing window, you can put in the initialization window, you can also put in the main window class to implement.

*/
}
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.