C # Close other program windows or share Process Code

Source: Internet
Author: User

C # Close other program windows or share Process Code

This article mainly introduces how to close other program windows or process code sharing in C #. This article provides two methods and sample code respectively. For more information, see

During winform development, other programs may need to be closed or the process should be closed. I have written an article about this. Some colleagues asked me today, so I will share it with you on the next page.

The following two methods I know should be helpful to you. If you have any other methods, thank you for sharing them.

Method 1

Name of the process to be disabled by ProcName

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

Private bool closeProc (string ProcName)

{

Bool result = false;

System. Collections. ArrayList procList = new System. Collections. ArrayList ();

String tempName = "";

 

Foreach (System. Diagnostics. Process thisProc in System. Diagnostics. Process. GetProcesses ())

{

TempName = thisProc. ProcessName;

ProcList. Add (tempName );

If (tempName = ProcName)

{

If (! ThisProc. CloseMainWindow ())

ThisProc. Kill (); // forcibly terminate the process when the command for sending and closing the window is invalid.

Result = true;

}

}

Return result;

}

The above program defines an ArrayList. If you do not know the specific name of the process to be closed, you can put the value of ArrayList in a listbox or other controls to select the process to end.

Method 2

Declare at the top of the class body:

?

1

2

3

4

5

6

7

8

[DllImport ("user32.dll", CharSet = CharSet. Auto)]

Public static extern int SendMessage (int hWnd, int msg, int wParam, int lparam );

 

// SendMessage (hwnd1, WM_CLOSE, 0, 0 );

// Hwnd1 is the handle value returned by the findwindow function.

// Wm_close is defined in winuser. h.

// 0x0010 is the value of WM_CLOSE

SendMessage (hwnd1, 0x0010,0, 0 );

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.