public void StartListening(int port) { var ipAddress = IPAddress.Parse("0.0.0.0"); var localEndPoint = new IPEndPoint(ipAddress, port); var listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); try { listener.Bind(localEndPoint); listener.Listen(20); while (true) { var handler = listener.Accept(); var callback = new ThreadPoolCallback(handler); ThreadPool.QueueUserWorkItem(callback.Execute); } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
public bool QueueWorkItem(object workid, object data, ThreadPoolCallback cb) { if(workid == null) { throw new ArgumentNullException("workid", "workid can not be null"); } if(cb == null) { throw new ArgumentNullException("item", "item can not be null"); } if(!this.IsWorkItemQueued(workid)) { this._workItemHash.Add(workid, cb); this._workDataHash.Add(workid, data); this._workQueue.Enqueue(workid); this._workItemQueuedEvent.Set(); return true; } return false; }
public void AssignWork(object id, ThreadPoolCallback item, object data) { this._assigned = true; this._workid = id; this._workItem = item; this._workData = data; this._workAssignedEvent.Set(); }
public bool QueueWorkItem(object workid, ThreadPoolCallback cb) { return this.QueueWorkItem(workid, null, cb); }