Run only one instance, click the program shortcut to activate the program to the foreground

Source: Internet
Author: User
Tags bool
Program | shortcuts

Involved classes:

1, start the screen class:

public class SplashForm:System.Windows.Forms.Form

{

Private System.Windows.Forms.PictureBox PictureBox1;

Private System.Windows.Forms.Label Label1;

Private System.Windows.Forms.Label lbl_version;

///

The required designer variable.

///

Private System.ComponentModel.Container components = null;


Public Splashform ()

{

//

Required for Windows Forms Designer support

//

InitializeComponent ();

Lbl_version. Text = "version:" + application.productversion;

//

TODO: Add any constructor code after the InitializeComponent call

//

}

The following omitted

2. Application Loading class:

Using System;

Using System.Drawing;

Using System.Collections;

Using System.ComponentModel;

Using System.Windows.Forms;

Using System.Data;

Using System.Runtime.InteropServices;

Using System.Diagnostics;

Using System.Reflection;

Using System.IO;

Namespace Heroic.TempAnalyse.TempGui

{

///

Summary description of the Apploader.

///

public class Apploader

{

private static ApplicationContext context;

private static Splashform sform = new Splashform ();

private static MainWindow mform = null;

0 Invisible but still running, 1 centered, 2 minimized, 3 maximized

Private Const int ws_shownormal = 3;

[STAThread]

static void Main (string[] args)

{

[8/12/2004] used to update the program.

Doupdata ();

[7/19/2004] Change Order to start loading faster

Get a running routine

Process instance = Runninginstance ();

if (instance = null)

{

Sform.show ();

Mform = new MainWindow ();

context = new ApplicationContext ();

Application.idle + = new EventHandler (onappidle);

Application.Run (context);

}

Else

{

Process a discovery Routine

Handlerunninginstance (instance);

MessageBox.Show ("The current program is already running!");

}

}

Online update used, no longer the scope of this article

private static void Doupdata ()

{

System.Diagnostics.Process.Start (application.startuppath+@ "\update.exe", application.startuppath+@ "\ Heroic.TempAnalyse.TempGui.exe 0 ");//

}


private static void Onappidle (object sender, EventArgs e)

{

if (context. MainForm = null)

{

Application.idle-= new EventHandler (onappidle);

Mform.preload ();

Context. MainForm = Mform;

Context. Mainform.show ();

Sform.close ();

Sform = null;

}

}

Two programs are not allowed to start at the same time

public static Process Runninginstance ()

{

Process current = Process.getcurrentprocess ();

Process[] processes = Process.getprocessesbyname (current. ProcessName);

Traversing routines that are running with the same name

foreach (process process in processes)

{

Ignore existing routines

if (process. Id!= Current. ID)

{

Make sure routines run from EXE files

if (assembly.getexecutingassembly (). Location.replace ("/", "\") = =

Current. Mainmodule.filename)

{

Returns another instance of the routine

return process;

}

}

}

No other routines, return null

return null;

}


public static void Handlerunninginstance (Process instance)

{

Make sure the window is not minimized or maximized

Showwindowasync (instance. Mainwindowhandle, Ws_shownormal);


Set the real routine to foreground window

SetForegroundWindow (instance. Mainwindowhandle);

}


[DllImport ("User32.dll")]


private static extern bool Showwindowasync (

INTPTR hWnd, int cmdshow);

[DllImport ("User32.dll")] private static extern bool

SetForegroundWindow (IntPtr hWnd);

}

}

3, loaded after the formal operation of the class:

public void preload ()

{

If the completion is loaded, return the

if (_loaded)

Return

Put the machine-generated code here.

Initcustomcontrol ();

_loaded = true;


}


is the load complete

private bool _loaded = false;


protected override void OnLoad (EventArgs e)

{

Make sure the preload () function has called the

if (!_loaded)

throw new InvalidOperationException ("must call preload before the This function.");


}



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.