In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how C# ends the process through the designated process ID". In daily operation, I believe many people have doubts about how C# ends the process through the designated process ID. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubt of "how C# ends the process through the designated process ID". Next, please follow the editor to study!
ProcessCmdUtils code demonstration:
Using System;using System.Diagnostics;namespace HelloWorld.utils {class ProcessCmdUtils {/ / start a process and execute the incoming CMD DOS command public static Process ExecCmd () {/ / cmd = cmd.Trim (). TrimEnd ('&') + "& exit"; Process p = null; try {p = new Process () P.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; / / whether to use the operating system shell to start p.StartInfo.RedirectStandardInput = true; / / accept the input information from the caller p.StartInfo.RedirectStandardOutput = true; / / get the output information p.StartInfo.RedirectStandardError = true by the caller / / redirect standard error output p.StartInfo.CreateNoWindow = true; / / do not show program window} catch (Exception e) {throw;} return p } / / Force termination of the specified process public bool KillProcExec (int procId) {string cmd = string.Format ("taskkill / f / t / im {0}", procId) based on the passed process ID; / / Force termination of the specified process Process ps = null; try {ps = ExecCmd () Ps.Start (); ps.StandardInput.WriteLine (cmd + "& exit"); return true;} catch {throw;} finally {ps.Close ();} return false }}}
Perform a code demonstration (only demonstrate compliance with this tutorial code):
Static int psTaskID =-1; / / detect whether there is a last executed task, and execute it if the value is-1, otherwise kill drops the last unfinished task private void button1_Click (object sender, EventArgs e) {Console.WriteLine ("method ininer" + psTaskID) / / check whether psTaskID is-1, if not, execute the KillProcExec () method if (psTaskID! =-1) {ProcessCmdUtils procKill = new ProcessCmdUtils (); bool killStatus=procKill.KillProcExec (psTaskID); Console.WriteLine ("try external" + psTaskID) If (killStatus) {psTaskID =-1;}} try {string address = textBox1.Text; string cmd = "TRACERT.exe" + address; ps = ProcessCmdUtils.ExecCmd (); ps.Start () PsTaskID = ps.Id;// get the running process ID assigned to paTaskID Console.WriteLine ("try ininer" + psTaskID); / / other code other code, do not demonstrate} catch {throw for now } finally {ps.Close ();}} at this point, the study on "how C # ends the process through the specified process ID" is over. I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.