How to: Handle an Error in BackgroundWorker

I am trying to read data in a background thread. If the data is from a database or Excel, I check for a null field. If the field is null I want to terminate the process and allow the user to check the entire file for nulls in another subroutine. The process loads another form and the user can check the fields he/she wants to check. I have not figured out the best way to do this and would like suggestions, both for starting the new process, terminating the thread. I can set an error code to check on the RunWorkCompleted event so that should not be a problem.  To summarize, the steps are:
1. Detect null field
2. Terminate thread
3. Allow user to choose to check for nulls or skip check. If checking, load form and set list boxes, etc.
4. Ensure proper clean up of objects and garbage collection.
I have done all this when the process was in the main thread. The problem is moving it to the background. If I can't work this out, I'll have to keep it in the main process.
LVL 1
rkulpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Malik1947Commented:
Check the code
'to check Null Field value
If IsNull(Field) then
... DO SOMETHING
End If
 
to terminate Thread
Call TerminateThread(ThreadName, ByVal 0&)

Open in new window

0
rkulpAuthor Commented:
Thaks for your quick reply.
I already use the null check. How do I call the terminate sub from within the thread? Here is my structure:

DoWork event contains:

        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        ThreadSub(worker, e)

In another module:
Public Sub ThreadSub(pWorker as BackgroundWorker, e as DoWorkEventArgs)
' some other processing here
'The error is detected. Error code is set. What is the thread name? Is it pWorker, ThreadSub or something different?
      ECode = 10
      Call TerminateThread(ThreadName, ByVal 0&)

So, would it be better to do this (which I thought of after posting the question):

The error is detected:
    ECode = 10
   Do necessary clean up
   Exit Sub

Then, back in the RunWorkerCompleted subroutine, after checking e.error and e.cancelled:
    IIf ECode = 10 then
Do the null checking stuff here.
  Exit Sub
  End if
0
Malik1947Commented:
ThreadName is pWorker (the name of the thread that you specified(
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

rkulpAuthor Commented:
Malik1947,

I finally found TerminateThread in http://msdn2.microsoft.com/en-us/library/ms686717.aspx. It looks dangerous to me since it won't release resources.

I think I will keep trying to find a way to exit the thread that won't cause a memory leak. I'll return to this question to let you know what the result is within a day or so.
0
Malik1947Commented:
good luck.
0
rkulpAuthor Commented:
Malik1947,

The following seems to work:
In the thread:
      Catch error and set error code (ECode = 10)
      Branch to the last line in the subroutine prior to the "Catch ex as exception."  This seems to make the thread think it is done working.

In the RunWorkerCompleted event handler:
      If Not (e.error is nothing) then
            do something to handle the error
     ElseIf e.cancelled then
            do something to handle the cancellation
     ElseIf ECode = 10 then
             handle the error you caught including message boxes, loading new forms, clean up
     Else
            normal completion routines, if any
     End If
End Sub

Thnaks for looking at this question.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.