Exception in thread

  • Thread starter Thread starter PGP
  • Start date Start date
P

PGP

Hello,

I have a library which does some database operations. There are threads in
the library which are transparent to the user. Database operations are done
in threads as they could be lengthy. When an exception occurs in a library
thread, i want to make this exception available to the user (user is usually
in the app's main thread). What is the common practice in such a scenario?

TIA
Priyesh
 
Hello,

I have a library which does some database operations. There are threads in
the library which are transparent to the user. Database operations are done
in threads as they could be lengthy. When an exception occurs in a library
thread, i want to make this exception available to the user (user is usually
in the app's main thread). What is the common practice in such a scenario?

TIA
Priyesh

Remember that threads share memory. They simply have their own
instruction pointer and stack. If you have one thread raise a
MessageBox, it will show up. If you modify a GUI, it will modify the
UI, not a copy of the UI. You simply have to ensure you have
concurrency. The common problem is that the thread starts at a point
such that it doesn't have any access to the interface. Well, you can
have the thread start in a method that is still at the interface
level, then wrap the non-interface code in a try/catch.

void threaded() // this is the entry-point of your thread
{
try
{
// start DB code
}
catch
{
// do UI concurrency to handle error.
}
}

You do most UI concurrency using BeginInvoke and EndInvoke. You should
be able to get what you want from what I have said.
 
Remember that threads share memory. They simply have their own
instruction pointer and stack. If you have one thread raise a
MessageBox, it will show up. If you modify a GUI, it will modify the
UI, not a copy of the UI. You simply have to ensure you have
concurrency. The common problem is that the thread starts at a point
such that it doesn't have any access to the interface. Well, you can
have the thread start in a method that is still at the interface
level, then wrap the non-interface code in a try/catch.

void threaded() // this is the entry-point of your thread
{
try
{
// start DB code
}
catch
{
// do UI concurrency to handle error.
}
}

You do most UI concurrency using BeginInvoke and EndInvoke. You should
be able to get what you want from what I have said.

I do have a need to pass the Exception object back to the user. This i do
right now
by raising an event from catch handler with the exception in the event args.
This seems to work
correctly. One of my initial doubts was that if it would have lifetime
issues with the
exception object as it was created in another thread. Any information on why
this works
correctly will be helpful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top