【正文】
le there is an outstanding async call active. And you can39。t create a new async call. TcpClient t = null。 // Disconnect each client foreach (SockWrapper s in m_aryClients) { try { ()。 } catch {} } m_aryClients = null。 t = new TcpClient((), )。 lock (this) { 9 // close everything down ()。 ()。 ShutDownReady = null。s no way to clean up // nicely. Moral: Always call Dispose. //Dispose()。 } public void SendToAll(byte [] b) { _SendToAll(b, )。 } } public event TcpConnected Connected。 public event TcpReceive DataReceived。 private void _TcpServer(int nPortListen, IPAddress ip) { try 10 { // Initialize member vars m_aryClients = new ArrayList(5)。 m_bShuttingDown = false。s IP address m_sockListener = new Socket( , , )。 ( MAXCONNECTIONS )。 } catch { m_bShuttingDown = true。 } } private void _SendToAll(byte [] b, int iLength) { lock (this) { if (!m_bShuttingDown) { foreach (SockWrapper s in m_aryClients) { _SendOne(s, b, iLength)。 if (Send != null) 11 Send(this, ref , ref bSend)。 } catch { // Ignore the error. If the client is dead, OnReceiveData // will be called to close the connection. I would remove it // anyway, except bad things happen if you remove an entry // from a list while using foreach. } } // Client has connected private void OnConnectRequest( IAsyncResult ar ) { // Get the listener and client Socket listener = (Socket)。 lock (this) { if (!m_bShuttingDown) { // Wrap the client and add it to the array SockWrapper s = new SockWrapper(client)。 // Fire the Connected event if (Connected != null) Connected(this, ref )。 ( , 0, , , receiveData, s )。 } else { 12 // If we are in shutdown mode, DON39。T setup // the async listen, and DO set the event // to say we are done. ()。 lock (this) { if (!m_bShuttingDown) { // Check if we got any data try { int nBytesRec = ( ar )。 // Restablish the callback AsyncCallback receiveData = new AsyncCallback( OnReceivedData )。 } else { // If no data was received then the connection is probably dead RemoveConnection(s)。 } 13 } } } // Remove a connection from the list of active connections private void RemoveConnection(SockWrapper s) { try { ( )。 } catch {} // Remove it from the array try { ( s )。 } // Wrapper for each client (stored in m_aryClients) internal class SockWrapper { // The buffer is used by receive public Socket Client。 public object obj。 byBuff = new byte[256]。 } } } public delegate void TcpConnected(Object sender, ref object o)。 public delegate void TcpReceive(Object sender, ref object o, ref byte [] b, int ByteCount)。 public WebCamService() { InitializeComponent()。 = WebCamService。 } } ( disposing )。 WebCamService ServiceToRun = new WebCamService() 。 } else { = false。 } } region Member Variables private const int MAXOUTSTANDINGPACKETS = 3。 private ManualResetEvent ConnectionReady。 private volatile int iConnectionCount。//實(shí)例化進(jìn)程 thread = new Thread(starter)。 } /// summary /// Stop this service. /// 停止服務(wù) /// The Run() Method tests for this thread state each second /// 每秒都為這個(gè)進(jìn)程啟動(dòng)方法測試 /// /summary protected override void OnStop() { 16 // Set exit condition //設(shè)置退出狀態(tài) bShutDown = true。 } public void Run() { const int VIDEODEVICE = 0。 // Depends on video device caps. Generally 430. const int VIDEOWIDTH = 640。 // Depends on video device caps