又要来砸分求助高手了,很简单的一段异步代码,哪位高手帮我运行下啊,为什么点完启动点停止就会有那些异常啊
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>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// &