Handeling exceptions when using threads

B

Bob Calvanese

I have a main form (frmMain).
frmMain creates a sub form (frmSub).
frmSub creates a business object (busObj).
busObj creates a thread and calls a method to run in the thread.
The method creates a server side buiness object (busObjSrv).
busObjSrv creates an Excel.Application (exAp) object on the server.
exAp opens an automated excel woekbook (exWb) (automated by an outside
company)
busObjSrv fills the information from frmSub on exWb and calls the automation
methods.
The automation methods generate 2 word documents based on the information
from frmSub, and data from the other company located on the work book.

Sorry for the long explenation, but heres the problem...

once busObj spins the thread off, frmSub closes and breaks the object link
between busObj and frmMain. If an exception is thrown in busObjSrv, I cannot
pass it back to frmMain to display to the user through the catch blocks. A
general exception gets thrown (break/continue) and the program does not blow
up, but the exception is not being handled properly (IMO).

Does anyone know a way that I can pass the exception back to frmMain to
display in a MessageBox?

We have several business objects that use threading, and I would like to
incorporate a solution into a utility object that we can use for all
threading.

If I figure it out first... I will post the solution in case someone else
needs it.

Thanks in advance.

bobc
 
S

sadhu

How about an event that is fired when a exception gets thrown. You can
then have an exception handler that subscribes to the events and
displays something in the UI when the event gets fired.
 
B

Bob Calvanese

If the sub form creates the business object and then goes away, how does the
main form know that the business object even exists?

bobc
 
S

sadhu

Well, you have to make the MainForm subscribe to events fired from the
business object.

Regards
Senthil
 
B

Bob Calvanese

So even though the main form creates the sub form, and the sub form creates
the business object, and the sub form closes... That the main form knows
that there is a business object?

I thought that the business object would only be known by the object that
created it. Could you explain how that is form me so I can understand?

I'll give it a try... Thanks
bobc
 
S

sadhu

Something like this

public class MainForm : Form
{
public void CreateSubForm()
{
SubForm s = new SubForm(this);
s.Show();
}

internal void ExceptionHandler(Exception e)
{
MessageBox.Show(e.ToString());
}
}

class SubForm : Form
{
MainForm f;
public SubForm(MainForm f)
{
this.f = f;
}
void CreateBusinessObject()
{
BusinessObject bo = new BusinessObject();
bo.ExceptionOccurred += new
ExceptionOccurredEventHandler(f.ExceptionHandler);
}
}
 

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

Top