Only one form is started. If it is started again, the form is activated.
Source: Internet
Author: User
1 Using System;
2 Using System. Collections. Generic;
3 Using System. Windows. forms;
4 Using System. diagnostics;
5 Using System. runtime. interopservices;
6
7 Namespace Windowsapplication2
8 {
9 Static Class Program
10 {
11 /// <Summary>
12///The main entry point of the application.
13/// </Summary>
14
15 [Dllimport ( " User32.dll " )]
16 Private Static Extern Bool Showwindowasync (intptr hwnd, Int Cmdshow );
17 [Dllimport ( " User32.dll " )]
18 Private Static Extern Bool Setforegroundwindow (intptr hwnd );
19 Private Const Int Ws_shownormal = 1 ;
20
21 [Stathread]
22 Static Void Main ()
23 {
24 // Get the running routine
25 Process instance = Runninginstance ();
26 If (Instance = Null )
27 {
28 // If there are no other routines, create a new form.
29 Application. enablevisualstyles ();
30 Application. setcompatibletextrenderingdefault ( False );
31 Application. Run ( New Form1 ());
32 }
33 Else
34 {
35//Process the detected routine
36Handlerunninginstance (instance );
37}
38 }
39 /// <Summary>
40///Get the running process
41/// </Summary>
42/// <Returns> </returns>
43 Public Static Process runninginstance ()
44 {
45 Process current = Process. getcurrentprocess ();
46 Process [] Processes = Process. getprocessesbyname (current. processname );
47
48 // Traversing a process with the same name running
49 Foreach (Process In Processes)
50 {
51 // Ignore existing processes
52 If (Process. ID ! = Current. ID)
53 {
54 // Make sure the process runs from the EXE file
55 If (Process. mainmodule. filename = Current. mainmodule. filename)
56 {< br> 57 // return to another process instance
58 return process;
59 }
60 }
61 }
62 // If no other process exists, null is returned.
63 Return Null ;
64 }
65 /// <Summary>
66 /// Processes a running process, that is, activating it.
67 /// </Summary>
68 /// <Param name = "instance"> Process to be processed </Param>
69 Public Static Void Handlerunninginstance (process instance)
70 {
71 // Ensure that the window is not minimized or maximized
72 Showwindowasync (instance. main1_whandle, ws_shownormal );
73 // Set the actual process to foreground window
74 Setforegroundwindow (instance. main1_whandle );
75 }
76 }
77 }
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