Trouble with BackgroundWorker and blocking operation

0

Im writing a TFTP server application by using WPF of C#. I create a toggle button to start and stop a server. I use Backgroundworker class to send cancel sign immediately in order to prevent the UI hangs while transferring file. The problem is when I click Button to start server and then click again to stop it. The code breaks and give me an exception:

System.Net.Sockets.SocketException HResult=0x80004005 Message=A blocking operation was interrupted by a call to WSACancelBlockingCall Source=System StackTrace: at System.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, EndPoint& remoteEP) at System.Net.Sockets.UdpClient.Receive(IPEndPoint& remoteEP) at TftpServerApp.Code.TftpServer.StartTftpServices(Object sender, DoWorkEventArgs e) in D:\DA Mang\Do an TFTP\TftpServerApp\TftpServerApp\Code\TftpServer.cs:line 52 at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e) at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)

This exception was originally thrown at this call stack:

System.Net.Sockets.Socket.ReceiveFrom(byte[], int, int, System.Net.Sockets.SocketFlags, ref System.Net.EndPoint) System.Net.Sockets.UdpClient.Receive(ref System.Net.IPEndPoint) TftpServerApp.Code.TftpServer.StartTftpServices(object, System.ComponentModel.DoWorkEventArgs) in TftpServer.cs System.ComponentModel.BackgroundWorker.OnDoWork(System.ComponentModel.DoWorkEventArgs) System.ComponentModel.BackgroundWorker.WorkerThreadStart(object)

...and the code that throws exception:

while (true)
            {
                if (worker1.CancellationPending)
                {
                    tftpServer.Close();
                    break;
                }
                int packetNr = 0;
                recBuffer = tftpServer.Receive(ref iep);/**Exception thrown here**/
                if (((Opcode)recBuffer[1]) == Opcode.READ_REQUEST)
                {.....

How can I handle in this situation?

c#
backgroundworker
asked on Stack Overflow Dec 8, 2019 by David Nguyen • edited Dec 8, 2019 by MickyD

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0