Question background process

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
520
Programming Experience
3-5
During debug, if the program is running some kind of background work, either loading a picturebox async or doing backgroundworker. If i exit the program, there will be an error cause the controls it need is already gone. This error only happen in debug but not after release. Should i do something about that? What can i do?
 
Exactly what to do depends on what exactly is happening at the time but, as an example, in the case of calling LoadAsync on a PictureBox, you can call CancelAsync to cancel the asynchronous operation. You'd want to do that in the FormClosing event handler, before the form closes and disposes all its child controls.. If that asynchronous pattern is implemented correctly then there will always be a CancelAsync method to cancel the DoSomethingAsync method, e.g. the RunWorkerAsync and CancelAsync methods of the BackgroundWorker.
 
Hi,

You could do a couple of things here:-

1) Add a Try/Catch statement to the Background worker and deal with the relevant error when the application closes.

or

2) You could code the Background worker to support cancellation and then cancel the Background worker before the application closes.

Cheers,

Ian
 
If i put all the cancel in the formclosing, the program doesnt seem to want to close until all are canceled and i need to hit close again.
 
I think it's time to show us the code. You may have to delay until the asynchronous operations have been cancelled but there's no reason that you can't call Close in code when that's done rather than require the user to do it. You might pop up a dialogue with a Marquee ProgressBar telling the user that background processes are being terminated and then close everything when that's done. If we can see what you've got then we can probably help sort it out.
 
bgwAll_Cancel() just close all the backgroundworkers
VB.NET:
    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        bgwAll_Cancel()
        For Each Control In FlowLayoutPanel1.Controls
            If TypeOf (Control) Is GroupBox Then
                Dim gBox As GroupBox = DirectCast(Control, GroupBox)
                Dim picBox As PictureBox = DirectCast(gBox.Controls(0), PictureBox)
                picBox.CancelAsync()
            End If
        Next
    End Sub
it usually stop at the progressChange section of the backgroundwroker or picBox
VB.NET:
    Private Sub bgw_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles bgw.ProgressChanged
        tspb1.Value = e.ProgressPercentage
    End Sub
VB.NET:
    Private Sub pbArray_LoadProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs)
            For Each Control As Control In gBox.Controls
                If TypeOf (Control) Is ProgressbarWithLabel1 Then
                    DirectCast(Control, ProgressbarWithLabel1).Value = e.ProgressPercentage
                    Exit For
                End If
            Next
    End Sub

is there no switch to turn off this error? since its in the debugger? those error never happen after i published it. And why does it only happen during debug?
right now i just put a try around all the progresschange
 
Last edited:
Back
Top