1

此代码使用后台工作线程并行从套接字读取图像。我想再次为我的新图像重用相同的套接字,但出现以下错误。

*错误:该进程无法访问文件“D:\image 1.png,因为它正被另一个进程使用*

即我的想法是杀死前一个进程并在同一个套接字中启动一个新进程。

enter code here

命名空间 ScreenCapture { 公共部分类 Form1 : Form { String ipaddress; 公共 Form1() { InitializeComponent(); backgroundWorker1.WorkerReportsProgress = true; }

    private void button1_Click(object sender, EventArgs e)
    {

        ipaddress=textBox1.Text;
        try
        {
            TcpClient client = new TcpClient(ipaddress, 5000);                                  // create the client socket and connect to port number 5000
            Stream s = client.GetStream();                                                      // create the object of stream and assign client socket stream
            StreamWriter sw = new StreamWriter(s);
            StreamReader sr = new StreamReader(s);
            sw.AutoFlush = true;                                                                // auto flush the stream writer
            sw.WriteLine("Send");
            client.Close();
            s.Close();
            Thread.Sleep(15000);
            backgroundWorker1.RunWorkerAsync();
        }
        catch (Exception ee)
        {
            MessageBox.Show(ee.ToString());
        }

    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {

        for (int i = 1; i < 10; i++)
        {
            try
            {
                TcpClient client = new TcpClient(ipaddress, 5001);                                  // create the client socket and connect to port number 5000
                Stream s = client.GetStream();                                                      // create the object of stream and assign client socket stream
                StreamWriter sw = new StreamWriter(s);
                sw.AutoFlush = true;
                sw.WriteLine(i.ToString());
                client.Close();
                s.Close();

                Thread proces = new Thread(delegate()
                {
                    int portno = 5001 + i;
                    int j = i;
                //    MessageBox.Show(portno.ToString());
                    TcpClient clientt = new TcpClient(ipaddress,portno);                                // create client socket and connect to prot number 5000   
                    using (var stream = clientt.GetStream())                                            // create the stream variable
                    using (var output = File.Create("D:\\img"+j+".png"))           // create variable output
                    {
                        var buffer = new byte[1024];                                                    // crfeate variable buffer of length 1024
                        int bytesRead;
                        while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)                 // get the number of bytes from stream and greater then
                        {
                            output.Write(buffer, 0, bytesRead);
                        }
                    }
                    backgroundWorker1.ReportProgress(j);
                    clientt.Close();
                    return;
                });
                proces.Start();
                Thread.Sleep(5200);

            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.ToString());
            }

            if (i == 9)
                i = 0;
        }
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {

        this.SuspendLayout();
        pictureBox1.Image = new Bitmap(@"D:\\img" +e.ProgressPercentage + ".png");
       if (File.Exists(@"D:\\img" + (e.ProgressPercentage - 1).ToString() + ".png"))
        {
           File.Delete(@"D:\\img" + (e.ProgressPercentage - 1).ToString() + ".png");
        }
        this.ResumeLayout();

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }


}

}

4

0 回答 0