杀线程的代码(转自CSDN-cyfboy)

 

private   void  KillProcess( string  processName)
        
{
            System.Diagnostics.Process myproc 
= new System.Diagnostics.Process();
            
//得到所有打开的进程
            try
            
{
                
foreach (Process thisproc in Process.GetProcessesByName(processName))
                
{
                    
if (!thisproc.CloseMainWindow())
                    
{
                        thisproc.Kill();
                    }
                    
                }

            }

            
catch (Exception Exc)
            
{
                MessageBox.Show(Exc.ToString());
            }

        }


        
private   void  Form1_Load( object  sender, EventArgs e)
        
{
            String tempName 
= "";
            
int begpos;
            
int endpos;

            
foreach (Process thisProc in System.Diagnostics.Process.GetProcesses())
            
{
                tempName 
= thisProc.ToString();
                begpos 
= tempName.IndexOf("("+ 1;
                endpos 
= tempName.IndexOf(")");
                tempName 
= tempName.Substring(begpos, endpos - begpos);
                
//this.listBox1.Items.Add(tempName);
                if (tempName == "newfax")
                
{
                    KillProcess(tempName);
                }

            }

        }


你可能感兴趣的:(csdn)