[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

How to: Handle an Error in BackgroundWorker

Posted on 2008-02-07
6
Medium Priority
?
483 Views
Last Modified: 2013-12-17
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.
0
Comment
Question by:rkulp
  • 3
  • 3
6 Comments
 
LVL 5

Expert Comment

by:Malik1947
ID: 20842330
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
 
LVL 1

Author Comment

by:rkulp
ID: 20842683
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
 
LVL 5

Assisted Solution

by:Malik1947
Malik1947 earned 1000 total points
ID: 20843163
ThreadName is pWorker (the name of the thread that you specified(
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 1

Author Comment

by:rkulp
ID: 20843547
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
 
LVL 5

Expert Comment

by:Malik1947
ID: 20844031
good luck.
0
 
LVL 1

Accepted Solution

by:
rkulp earned 0 total points
ID: 20844890
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Native ability to set a user account password via AD GPO was removed because the passwords can be easily decrypted by any authenticated user in the domain. Microsoft recommends LAPS as a replacement and I have written an article that does something …
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
Suggested Courses
Course of the Month7 days, 19 hours left to enroll

608 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question