0
点赞
收藏
分享

微信扫一扫

这样的程序怎样关闭!


这样的程序怎样正常退出,退出程序还有什么办法! 
我已经把对于阻碍程序退出不重要的东西删掉了,便于阅读. 

using System; 
 using System.Drawing; 
 using System.Collections; 
 using System.ComponentModel; 
 using System.Windows.Forms; 
 using System.Data; 
 using System.Net; 
 using System.Net.Sockets; 
 using System.Text; 
 using System.Threading; namespace UDPTESTserver 
 { 
    /// <summary> 
    /// Form1 的摘要说明。 
    /// </summary> 
    public class Form1 : System.Windows.Forms.Form 
    { 
       private System.Windows.Forms.Button button1; 
       private System.Windows.Forms.Button button2; 
       private System.Windows.Forms.RichTextBox richTextBox1; 
       private System.ComponentModel.IContainer components; 
       private bool mdone=true;//循环参数 
       private System.Threading.Thread thread; 
       private UdpClient receivingUdpClient = new UdpClient(888);//设定监听端口为888 
       private byte[] receiveBytes;//接收数据缓存 
       private IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"),888);//暂设监听本机888端口       public Form1() 
       { 
          // 
          // Windows 窗体设计器支持所必需的 
          // 
          InitializeComponent(); 
          thread = new Thread(new ThreadStart(Listener));//监听线程 
          // 
          // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 
          // 
       } 
       /// <summary> 
       /// 清理所有正在使用的资源。 
       /// </summary> 
       protected override void Dispose( bool disposing ) 
       { 
          if( disposing ) 
          { 
             if (components != null)  
             { 
                components.Dispose(); 
             } 
          } 
          base.Dispose( disposing ); 
       }       #region Windows Form Designer generated code 
       /// <summary> 
       /// 设计器支持所需的方法 - 不要使用代码编辑器修改 
       /// 此方法的内容。 
       /// </summary> 
       private void InitializeComponent() 
       { 
           
       } 
       #endregion       /// <summary> 
       /// 应用程序的主入口点。 
       /// </summary> 
       [MTAThread] 
       static void Main()  
       { 
          Application.Run(new Form1()); 
       }        
       private void Listener()//监听线程 
       { 
           
          while(mdone) 
          { 
           this.richTextBox1.AppendText(".");//辅助语句,这句用来表现线程在不断循环,但是只动作过一次以后在下句没有接到数据的情况下都不会再次动作. 
                                  receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint); //接收字符数组,****但是现在线程会停在这里等待接收数据无法进入到下次循环 
          } 
       } 
       //弹出报警窗口 
        
       private void button1_Click(object sender, System.EventArgs e) 
       { 
          if((thread.ThreadState&ThreadState.Unstarted)!=0) 
          { 
             thread.Start();//开始线程    
          } 
       } 
       private void button2_Click(object sender, System.EventArgs e) 
       { 
          this.mdone=false;//由于 receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);在等待,所以线程的循环无法接收到这句,所以无法跳出循环,线程无法结束!!! 
          //thread.Abort();//即使加上这句,线程也结束不了 
          Application.Exit();//由于线程结束不了,所以整个程序也就无法正常关闭,只是表面的窗口关了,内存中整个进程仍然生存. 
       } 
    } 
 }


 

举报

相关推荐

0 条评论