Answered Exceptions In backgroundworker

Status
Not open for further replies.

markwitt

Member
Joined
Feb 5, 2021
Messages
6
Programming Experience
5-10
I'm using .NET 4.5. My program starts a background worker task. I've noticed that within this background worker task exceptions are not being thrown. If I run Debug I will then see the exception. I rely on the exception catching for Socket operations. Why aren't these exception being thrown?

Thank You,
Mark
 
Solution
Welcome to the forums btw.

If you are not using TryCatchBlocks, then the error will be thrown and the DoWork event will fire off into the RunWorkerCompleted event, and it does this because the DoWork event can't continue execution of that code.

You need to check the status of the RunWorkerCompleted event. To find the result, look in the RunWorkerCompletedEventArgs.Result Property to see how your event executed.

If the DoWork event experienced an error, then this error will be passed on to the RunWorkerCompleted Event where you can and should check it first by accessing the e.Error property of this event. By checking the AsyncCompletedEventArgs.Error Property on...
They are being thrown, and they're being caught. You use the RunWorkerCompleted event to determine whether an exception was thrown in the DoWork event handler and, if so, what it was.
 
Isn't the RunWorkerCompleted used to catch it from the main thread? Is there anyway to catch the exception within the background worker? The background worker is active until the main form closes.
 
Last edited:
You would catch it the same way you would any other exception, i.e. with a Try...Catch block. Any exception thrown and not caught in the DoWork event handler is caught by the BackgroundWorker itself and packaged up in the RunWorkerCompleted event handler.
 
Welcome to the forums btw.

If you are not using TryCatchBlocks, then the error will be thrown and the DoWork event will fire off into the RunWorkerCompleted event, and it does this because the DoWork event can't continue execution of that code.

You need to check the status of the RunWorkerCompleted event. To find the result, look in the RunWorkerCompletedEventArgs.Result Property to see how your event executed.

If the DoWork event experienced an error, then this error will be passed on to the RunWorkerCompleted Event where you can and should check it first by accessing the e.Error property of this event. By checking the AsyncCompletedEventArgs.Error Property on any Async run code, you can determine if there was in fact an error, and determine what that error was. If you wrap your error code in TryCatchBlocks, then the e.Error property will not show any error raised because it was handled or expects it was handled in the TryCatchBlock.

If you are not using TryCatchBlocks in the DoWork Event. Any attempts to access any other properties of the RunWorkerCompleted events properties will likely return null if an error was passed from your DoWork event, as the only property you will be able to check at such a point is the e.Error property, as to ascertain what the problem was that caused the error in your DoWork Event.

Given you see errors only in debug mode but you are not seen any errors on your executed code without the debugger attached; is normal. This is also indicative that you are running your code without TryCatchBlocks and your error is running silent through the RunWorkerCompleted event.

I will also mark this as answered, since it directly answers your question. I won't be around to answer any questions from today. If you have any, I'm the sure the guys can help you out.
 
Solution
You would catch it the same way you would any other exception, i.e. with a Try...Catch block. Any exception thrown and not caught in the DoWork event handler is caught by the BackgroundWorker itself and packaged up in the RunWorkerCompleted event handler.

I am trying to catch the exception if the port assigned is already in use. When the port is in use I am not getting any exception thrown. The way I've read the comments is that as long as there is a TryCatchBlock I should be catching it in the DoWork?
C#:
       public void ServerSocketInit()
        {
            try
            {
                connecting = true;

                // Create the listening socket.
                socketlistener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                // Establish the local endpoint for the socket.
                IPAddress ipAddress = IPAddress.Parse(_hostIP);
                IPEndPoint localEndPoint = new IPEndPoint(ipAddress, _hostPort);

                // Bind the socket to the local endpoint and listen for incoming connections.
                socketlistener.Bind(localEndPoint);

                // Start Listening
                socketlistener.Listen(1);
                ServerSocketListen();
            }
            catch (SocketException)
            {
                initfailed = false;
            }
        }
 
Last edited by a moderator:
Welcome to the forums btw.

If you are not using TryCatchBlocks, then the error will be thrown and the DoWork event will fire off into the RunWorkerCompleted event, and it does this because the DoWork event can't continue execution of that code.

You need to check the status of the RunWorkerCompleted event. To find the result, look in the RunWorkerCompletedEventArgs.Result Property to see how your event executed.

If the DoWork event experienced an error, then this error will be passed on to the RunWorkerCompleted Event where you can and should check it first by accessing the e.Error property of this event. By checking the AsyncCompletedEventArgs.Error Property on any Async run code, you can determine if there was in fact an error, and determine what that error was. If you wrap your error code in TryCatchBlocks, then the e.Error property will not show any error raised because it was handled or expects it was handled in the TryCatchBlock.

If you are not using TryCatchBlocks in the DoWork Event. Any attempts to access any other properties of the RunWorkerCompleted events properties will likely return null if an error was passed from your DoWork event, as the only property you will be able to check at such a point is the e.Error property, as to ascertain what the problem was that caused the error in your DoWork Event.

Given you see errors only in debug mode but you are not seen any errors on your executed code without the debugger attached; is normal. This is also indicative that you are running your code without TryCatchBlocks and your error is running silent through the RunWorkerCompleted event.

I will also mark this as answered, since it directly answers your question. I won't be around to answer any questions from today. If you have any, I'm the sure the guys can help you out.

I am using Try Catch Blocks, but for some reason the exception isn't being thrown. It's thrown in Debug Mode. I posted some of my code. I must be missing something or just confused.
 
I would assume that if the exception is thrown, you would set initfailed to true, but in the code you presented above, you are setting it to false.
 
I would assume that if the exception is thrown, you would set initfailed to true, but in the code you presented above, you are setting it to false.
Maybe you missed it. But that's not a background worker.

If you look at the title of the thread. And my response here Answered - Exceptions In backgroundworker - It explains how to handle the error and whether or not to use TryCatchBlocks.
 
If the OP is using a background worker, then they should not use a TryCatchBlock.

Since the code they posted, is non related to a background worker, they should handle their error in a TryCatchBlock using a finally statement.

I'm on the verge of closing this topic, since it does not reflect the topic title at all, and the code proposed has no connection to a background worker. This will be misleading for future readers.

@markwitt where is the backgroundworker?
 
I would assume that if the exception is thrown, you would set initfailed to true, but in the code you presented above, you are setting it to false.
Ah Geesh.... I can't believe it was that simple. I feel like a complete idiot. I totally overlook the obvious. Thanks to all for the help and pointing out my stupid over site.
 
In future, when you come to a forum for assistance whilst asking about one thing. Do not propose code that does not reflect what you are working with or asking about. This below snip uses DoWorkEventArgs, and also proves you are using a background worker.
C#:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
This does not have any such event arguments for a background worker. Nor does it belong to a background worker. Its only a method :
C#:
public void ServerSocketInit()
If you were using a background worker correctly, you wouldn't be catching the error in a DoWork event, for reasons already explained to you. I suggest you start reading up on the links I provided you to cover this component.

Next time post your code, EXACTLY as you have wrote it.
 
Status
Not open for further replies.
Back
Top Bottom