首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

又要来砸分高手了,很简单的一段异步代码,谁帮小弟我运行下啊为什么点完启动点停止就会有那些错误

2012-01-23 
又要来砸分求助高手了,很简单的一段异步代码,哪位高手帮我运行下啊,为什么点完启动点停止就会有那些异常啊

又要来砸分求助高手了,很简单的一段异步代码,哪位高手帮我运行下啊,为什么点完启动点停止就会有那些异常啊
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace AsyncServerWin
{
  public partial class Form1 : Form
  {
  int num = 0;
  EventWaitHandle ewh = new EventWaitHandle(false, EventResetMode.ManualReset);
  TcpListener tl = new TcpListener(IPAddress.Any, 9876);
  public Form1()
  {
  InitializeComponent();
  }

  public void BeginListen()
  {
  try
  {
  tl.Start();
  AsyncCallback ac = new AsyncCallback(MyAcceptTcpClient);
  while (true)
  {
  ewh.Reset();
  tl.BeginAcceptTcpClient(ac, tl);
  ewh.WaitOne();
  }
  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.ToString());
  tl.Stop();
  }
  }
   
  private void button1_Click(object sender, EventArgs e)
  {
  this.button1.Enabled = false;
  Thread t = new Thread(BeginListen);
  t.Start();
   
  }

  public void MyAcceptTcpClient(IAsyncResult ar)
  {
  try
  {
  ewh.Set();
  TcpListener tl = (TcpListener)ar.AsyncState;
  TcpClient tc = tl.EndAcceptTcpClient(ar);
  num++;
  lbConnectionID.Items.Add(((int)tc.Client.Handle).ToString());
  tbNum.Text = num.ToString();
  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.ToString());
  tl.Stop();
  }
  }

  private void button2_Click(object sender, EventArgs e)
  {

  tl.Stop();//---------------------------------------------------这里出问题了!!!
  this.button1.Enabled = true;
  }
  }
}

================================================================================

namespace AsyncServerWin
{
  partial class Form1
  {
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.IContainer components = null;

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
  protected override void Dispose(bool disposing)
  {
  if (disposing && (components != null))
  {
  components.Dispose();
  }
  base.Dispose(disposing);
  }

  #region Windows 窗体设计器生成的代码

  /// <summary>
  /// 设计器支持所需的方法 - 不要
  /// 使用代码编辑器修改此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
  this.button1 = new System.Windows.Forms.Button();


  this.tbNum = new System.Windows.Forms.TextBox();
  this.lbConnectionID = new System.Windows.Forms.ListBox();
  this.button2 = new System.Windows.Forms.Button();
  this.SuspendLayout();
  // 
  // button1
  // 
  this.button1.Location = new System.Drawing.Point(167, 51);
  this.button1.Name = "button1";
  this.button1.Size = new System.Drawing.Size(75, 23);
  this.button1.TabIndex = 0;
  this.button1.Text = "启动";
  this.button1.UseVisualStyleBackColor = true;
  this.button1.Click += new System.EventHandler(this.button1_Click);
  // 
  // tbNum
  // 
  this.tbNum.Location = new System.Drawing.Point(167, 133);
  this.tbNum.Name = "tbNum";
  this.tbNum.Size = new System.Drawing.Size(100, 21);
  this.tbNum.TabIndex = 1;
  // 
  // lbConnectionID
  // 
  this.lbConnectionID.FormattingEnabled = true;
  this.lbConnectionID.ItemHeight = 12;
  this.lbConnectionID.Location = new System.Drawing.Point(29, 133);
  this.lbConnectionID.Name = "lbConnectionID";
  this.lbConnectionID.ScrollAlwaysVisible = true;
  this.lbConnectionID.Size = new System.Drawing.Size(120, 88);
  this.lbConnectionID.TabIndex = 2;
  // 
  // button2
  // 
  this.button2.Location = new System.Drawing.Point(167, 198);
  this.button2.Name = "button2";
  this.button2.Size = new System.Drawing.Size(75, 23);
  this.button2.TabIndex = 3;
  this.button2.Text = "停止";
  this.button2.UseVisualStyleBackColor = true;
  this.button2.Click += new System.EventHandler(this.button2_Click);
  // 
  // Form1
  // 
  this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  this.ClientSize = new System.Drawing.Size(292, 266);
  this.Controls.Add(this.button2);
  this.Controls.Add(this.lbConnectionID);
  this.Controls.Add(this.tbNum);
  this.Controls.Add(this.button1);
  this.Name = "Form1";
  this.Text = "Form1";
  this.ResumeLayout(false);
  this.PerformLayout();

  }

  #endregion

  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.TextBox tbNum;
  private System.Windows.Forms.ListBox lbConnectionID;
  private System.Windows.Forms.Button button2;
  }
}



[解决办法]
你可以添加个状态变量,
bool listenerrunning;
修改下
private void button2_Click(object sender, EventArgs e) 

listenerrunning=false;


再修改下
 public void MyAcceptTcpClient(IAsyncResult ar) 

try 

ewh.Set(); 
if(listenerrunning)


{
TcpListener tl = (TcpListener)ar.AsyncState; 
TcpClient tc = tl.EndAcceptTcpClient(ar); 
num++; 
lbConnectionID.Items.Add(((int)tc.Client.Handle).ToString()); 
tbNum.Text = num.ToString(); 
}

catch (Exception ex) 

MessageBox.Show(ex.ToString()); 
tl.Stop(); 


热点排行