Run an example of an instance

xiaoxiao2021-03-06  41

Using system; using system.windows; using system.diagnostics; using system.reflection;

public class OneInstnace {[STAThread] public static void Main () {// Get the running instance Process instance = RunningInstance ();.. if (instance == null) {// There is not another instance, show our form Application .Run (new Form ());} else {// There is another instance of this process HandleRunningInstance (instance);.}} public static Process runningInstance () {Process current = Process.GetCurrentProcess (); Process [] processes = Process.getProcessesbyName (current.ProcessName);

// Loop through the running processes in with the same name foreach (Process process in processes) {// Ignore the current process if (process.Id! = Current.Id) {// Make sure that the process is running from the exe File. IF (Assembly.GetexecutingAssembly (). Location.Replace ("/", "//") == current.mainmodule.filename) {// Return The Other process instance. Return process;}}}

// no Other instance. 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 intance 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); private const int WS_SHOWNORMAL = 1;} static void Main () {// Analyzing whether the previously opened the same process // new Bestzone.MIS.Report.F1BookContainer (); bool alreadyExist = false; try {System.Diagnostics.Process curP = System.Diagnostics.Process.GetCurrentProcess (); System.Diagnostics.Process [] ps = system.diagnostics.Process.getProcesses (); foreach (system.diagnostics.process p in ps) {if (p.ProcessName.Equals (curp.processname) && p.id! = curp.id) {alreadyexist = true;}}} catch (system.platformnotsupportedException ex) {EX.TOSTRING ();} catch (system.invalidoperty);} // If already existing, give up this process if (alreadyexist) {return;}

Application.run (new mainform ());

转载请注明原文地址:https://www.9cbs.com/read-55682.html

New Post(0)