private void FilePlayThread(object obj) { IPAddress localaddr = IPAddress.Parse("0.0.0.0"); TcpListener tcpserver = new TcpListener(localaddr, 0); tcpserver.Start(); IPEndPoint localep = (IPEndPoint)tcpserver.LocalEndpoint; WorkProcessHandler h = new WorkProcessHandler(FilePlayProcess); ThreadParam ap = (ThreadParam)obj; DeviceListener.Device d = deviceListener.Find(ap.id); if (d == null) { return; } int ret = deviceListener.FilePlayStart(d, "0.0.0.0", localep.Port, ap.streamtype, ap.volume, null); if (ret != 0) { return; } TcpClient tc = null; try { long starttime = DateTime.Now.Ticks; while ((DateTime.Now.Ticks - starttime) < 3000 * 10000)//3000ms { if (tcpserver.Pending()) { tc = tcpserver.AcceptTcpClient(); break; } } if (tc != null) { FileStream fs = File.OpenRead(ap.filename); if (fs != null) { bool eof = false; long writebytes = 0; byte[] fbs = new byte[1024]; NetworkStream tcs = tc.GetStream(); while (!eof) { if (tcs.CanWrite) { int readbytes = fs.Read(fbs, 0, fbs.Length); tcs.Write(fbs, 0, readbytes); writebytes += readbytes; if (readbytes < fbs.Length) { eof = true; } int process = (int)(100 * writebytes / fs.Length); this.BeginInvoke(h, d, ap.filename, false, process); } else { Thread.Sleep(10); } } } tc.Close(); } Thread.Sleep(500); deviceListener.FilePlayStop(d); this.BeginInvoke(h, d, ap.filename, true, 0); dataThread = null; } catch (ThreadAbortException abortException) { //this.BeginInvoke(h, ap.filename, true, 0); if (tc != null) { tc.Close(); } dataThread = null; } }