如何从进程(任务管理器)中删除应用程序? [英] how to remove application from processes(task manager) ?

查看:193
本文介绍了如何从进程(任务管理器)中删除应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好...
当我关闭应用程序时,它始终停留在活动进程中,每次将其从任务管理器中删除时,我该怎么办?

这是类listen

HI everyone...
when I close my application, it always stays with the active processes, each time I remove it from task manager, what do I do?

this is class listen

<pre lang="cs">class Listener<br />
   {<br />
       <br />
       public delegate void lbconnection(string lbconnection);<br />
       public lbconnection lbc;<br />
       Thread thdsHandler;<br />
       public int lport;<br />
       public string lpath;<br />
       <br />
       //Le constructeur de la class Listener<br />
       public Listener(int port,string path,lbconnection lbcc)<br />
       {<br />
           lbc=lbcc;<br />
           lport = port;<br />
           lpath = path;<br />
       }<br />
       public TcpClient handlerSocket;<br />
       public TcpListener tcpListener;<br />
       public void listenerThread()<br />
       {<br />
           //la port d''coute initialisé a 0   <br />
           //int port = 0;<br />
           //la creation de la class tcplistener <br />
           tcpListener = new TcpListener(new IPEndPoint(IPAddress.Any, lport)); <br />
           //le debut de l''ecoute on utilisant la methode start<br />
           tcpListener.Start();<br />
           //boucle infini while<br />
           while (true)<br />
           {<br />
               //la method acceptsocket accept chaque client arrivé <br />
               handlerSocket = tcpListener.AcceptTcpClient();<br />
               if (handlerSocket.Connected)<br />
               {<br />
                   lbc("Connected To Localhost :" + lport + " connected.");<br />
                   //lbConnections.Items.Add("Connected To Localhost :" + lport + " connected.");<br />
                   //lock pour ivite le blocage des clients<br />
                   lock (this)<br />
                   {<br />
                       //alSockets.Add(handlerSocket);<br />
                       Request rqs = new Request(lpath, handlerSocket);<br />
                       //creation d''une thread appeler thdstHandler<br />
                       thdsHandler = new Thread(new ThreadStart(rqs.handlerThread));<br />
                       //thdHandler = new Thread(thdsHandler);<br />
                       //le debut de travail de thread thdstHandler,on utilisant la methode start<br />
                       thdsHandler.Start();<br />
                   }<br />
               }<br />
           }<br />
       }<br />
       //procedure killthread<br />
       public void killthread()<br />
       {<br />
        thdsHandler.Abort();<br />
       }<br />
   }</pre><br />



和请求类



and the request class

 <pre lang="cs">class Request<br />
    {<br />
        //public delegate void Desplaylist(string lv0, string lv1, string lv2, string lv3, string lv4);<br />
        //Desplaylist ds;<br />
        //int tcn = 0; int cc = 0; int sr = 0; int fr = 0; int mc = 0; int rs = 0;<br />
        //public Thread thdHandler;<br />
        //Listener lst;<br />
        public string tbpath;<br />
        public TcpClient tcpclient;<br />
        public Request(string tbpath, TcpClient tcpclient)<br />
        {<br />
           this.tbpath = tbpath;<br />
           this.tcpclient = tcpclient;  <br />
        }<br />
        <br />
<br />
        public struct serverinfo<br />
        {<br />
            public string httpversion;<br />
            public string Date;<br />
            public string sinfo;<br />
            public string AcceptRanges;<br />
            public string ContentLength;<br />
            public string Connection;<br />
            public string ContentType;<br />
        };<br />
<br />
        // la procedure handleThread fait le traitement de requette arrivé de client <br />
        public void handlerThread()<br />
        {<br />
            //tcn++;<br />
            //lbConnections.Items.Add("Server Started");<br />
            byte[] clientData = new byte[1024];<br />
            //initialisation d''une socket appele handlesocket <br />
            //Socket handlerSocket = (Socket)alSockets[alSockets.Count - 1];<br />
            String filename = "";<br />
            String[] verbs;<br />
            NetworkStream ns = tcpclient.GetStream();<br />
            ns.Read(clientData, 0, clientData.Length);<br />
            //handleSocket.Receive(clientData);<br />
            //maitre la requette recus dans un fichier text<br />
            string stringdata = Encoding.ASCII.GetString(clientData);<br />
            File.WriteAllText(tbpath + "\\Request.txt", stringdata);<br />
            verbs = stringdata.Split(" ".ToCharArray());<br />
            // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<br />
            filename = verbs[1].Replace("/", "\\");<br />
            if (filename.IndexOf("?") != -1)<br />
            {<br />
                // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<br />
                filename = filename.Substring(0, filename.IndexOf("?"));<br />
            }<br />
            if (filename.EndsWith("\\"))<br />
            {<br />
                // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<br />
                filename += "index.html";<br />
            }<br />
            filename = tbpath + filename;<br />
            if (!File.Exists(filename))<br />
            {<br />
                //fr++;<br />
                filename = tbpath + "\\Page Not Found.html";<br />
            }<br />
            //if (filename != "\\Page Not Found.html")<br />
            //{<br />
            //    sr++;<br />
            //}<br />
            //ds(Convert.ToString(tcn), Convert.ToString(cc), Convert.ToString(sr), Convert.ToString(fr), Convert.ToString(mc));<br />
            //creation d''une nouvelle class filestream appele fs ouvrir ou cree le fichier ou lien filename<br />
            FileStream fs = new FileStream(filename, FileMode.Open);<br />
            //pointe sur le depart de string <br />
            fs.Seek(0, SeekOrigin.Begin);<br />
            byte[] fileContents = new byte[fs.Length];<br />
            //Maitre le contenu de (les bites) dans la variable filecontents<br />
            fs.Read(fileContents, 0, (int)fs.Length);<br />
            fs.Close();<br />
            //envoy la reponce ou client on utilisons la methode Send <br />
            ns.Write(fileContents, 0, fileContents.Length);<br />
            //handleSocket.Send(fileContents);<br />
            serverinfo sinfo = new serverinfo();<br />
            sinfo.httpversion = "HTTP/1.1 200 OK \r\n";<br />
            sinfo.Date = "Dtae: ";<br />
            sinfo.sinfo = "Server: HTTPServer/1 (Windows) HTTP/1.1 \r\n";<br />
            sinfo.AcceptRanges = "Accept-Ranges: bytes \r\n";<br />
            sinfo.ContentLength = "Content-Length: 1564 \r\n";<br />
            sinfo.Connection = "Connection: Close \r\n";<br />
            sinfo.ContentType = "Content-Type: text/html \r\n";<br />
            string servinfo = sinfo.httpversion + sinfo.Date + DateTime.Now.Date + "," + DateTime.Now.Month + " " + DateTime.Now.Year + "  " + DateTime.Now.Hour + " " + DateTime.Now.Minute + " "<br />
                + DateTime.Now.Second + "\r\n" + sinfo.sinfo + sinfo.AcceptRanges + sinfo.ContentLength + sinfo.Connection + sinfo.ContentType + "\r\n" + Encoding.ASCII.GetString(fileContents);<br />
            File.WriteAllText("C:\\www\\Reponse.txt", servinfo);<br />
            //lbConnections.Items.Add(filename);<br />
            //arreté la socket<br />
            ns.Close();<br />
            //handleSocket.Close();<br />
            //procedute killthread qui tue le Thread<br />
            //lst.killthread();<br />
        }<br />
    }</pre>



然后点击bouton



and the click bouton

<pre lang="cs">private void btnStart_Click(object sender, EventArgs e)<br />
        {<br />
<br />
            tbPort.ReadOnly = true;<br />
            tbPath.ReadOnly = true;<br />
            //la creation d''une list des socket<br />
            int port = 0;<br />
            port = Convert.ToInt16(tbPort.Text);<br />
            string path = tbPath.Text;<br />
            //alSockets = new ArrayList();<br />
            Listener lst = new Listener(port, path, this.lbconnect);<br />
            //creation d''une thread appeler thdlistener<br />
            thdListener = new Thread(new ThreadStart(lst.listenerThread));<br />
            //le debut de travail de thread...on utilisant la methode start<br />
            thdListener.Start();<br />
<br />
            ////////////////////////////////<br />
            btnStart.Enabled = false;<br />
            Resume.Enabled = false;<br />
            Suspend.Enabled = true;<br />
            Stop.Enabled = true;<br />
        }</pre><br />



这是我的工作,我需要向我解释错误的问题.



this is my work and i need to explain me the wrong ...

推荐答案

您是否还有辅助线程仍然存在? (如果它们是前台线程,则它们都需要在进程终止之前终止)

[更新]
-----------

只是一个更新.从OP的评论来看,这确实是问题所在.他必须将线程标记为后台线程,这才解决了他的问题.
Do you have secondary threads that may still be alive? (if they are foreground threads, all of them need to terminate before the process terminates)

[Update]
-----------

Just an update. From the comments OP made, this was indeed the issue. He had to mark the threads as background threads and that fixed his problem.


您做的不好:在循环中创建线程.对于网络,您需要一个线程(在客户端)或两个在服务器端的线程:一个正在接受新的连接(将远程线程的信息添加到某个容器中),另一个线程执行数据交换协议:从/向网络读/写溪流.另外,不要使用lock-套接字本身就是线程同步原语.您需要从头开始创建线程,然后在关闭应用程序时中止线程.

另外,如果使用Thread.Abort,则必须处理ThreadAbortException.

线程数应该是固定的.

请参阅我对类似问题的回答,其中我描述了正确设计的框架:
来自同一端口号的多个客户端 [ 您的问题是由于挂起线程而导致应用程序未完成.一种解决方案可能是使用线程池-未完成的线程不会阻止您的进程关闭.这不会解决代码问题,这是线程生命周期控制的逻辑.我不想寻找您的错误,因为您的设计看起来不对.您需要按照我上面的说明进行设计.

-SA
You do a bad thing: you create threads in loop. For networking, you need either one thread (on client side) or two threads on server side: one is accepting new connections (adding info on remote thread to some container), another thread execute data exchange protocol: read/write from/to network stream. Also, don''t use lock — socket is itself a thread synchronization primitive. You need to create thread(s) from the very beginning and abort them when you close the application.

Also, if you use Thread.Abort, you must process ThreadAbortException.

The number of threads should be fixed.

See my answer to a similar question where I describe the skeleton of right design:
Multple clients from same port Number[^].

You never want to "remove application from Processes (Task Manager)"!
You problem is the application which is not finished due to pending thread(s). One solution could be using thread pool — the unfinished threads will not prevent your process from closing. This won''t be a resolution of the code problem, which is in the logic of your thread life-time control. I don''t want to look for your bug, because you design looks wrong. You need to address the design using my directions above.

—SA


Process[] Processes = Process.GetProcessesByName("name of process goes here");
            foreach (Process Proc in Processes)
            {
                Proc.Kill();
            }


这篇关于如何从进程(任务管理器)中删除应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆