This is my code:
public void Install( Credentials credentials )
{
this.OnInstallStart( new InstallEventArgs(
StatusStart.Replace( "%N", this.name ) ) );
Thread installer = new Thread( this.InstallDriver );
InstallDriverArgs args = new InstallDriverArgs();
args.DriverName = this.name;
args.DriverInf = this.location;
args.Credentials = credentials;
installer.Start( args );
}
private void InstallDriver( object data )
{
InstallDriverArgs myArgs = ( InstallDriverArgs )data;
Impersonation impersonator = new Impersonation();
impersonator.Impersonate( myArgs.Credentials.Username, "",
myArgs.Credentials.Password );
ManagementPath win32Path = new ManagementPath(
"Win32_PrinterDriver" );
ManagementClass mc = new ManagementClass( win32Path );
mc.Properties["Name"].Value = myArgs.DriverName;
mc.Properties["InfName"].Value = myArgs.DriverInf;
object[] args = { mc };
try
{
mc.InvokeMethod( "AddPrinterDriver", args );
}
catch ( ManagementException ex )
{
PrinterDriverInstallException myEx =
new PrinterDriverInstallException( this, ex );
throw myEx;
}
finally
{
impersonator.UndoImpersonation();
}
//TODO Find out if successful
//
http://msdn.microsoft.com/library/d...river_method_in_class_win32_printerdriver.asp
this.OnInstallEnd( new InstallEventArgs(
StatusEnd.Replace( "%N", this.name ) ) );
}
As you can see, I already have the try/catch block inside the working
thread, but I want to get it back to the UI thread to let them know an error
occured. Is it possible to get the exception back to the UI thread is the
question, or is it best to handle the exception inside the working thread and
raise an event to let the UI thread know there was an issue?
Thanks,
Chris
"Jeffrey Tan[MSFT]" said:
Hi Chris,
Thanks for your post.
To handle the exception in the thead other than UI thread, I think we can
wrap the thead proc code in the try..catch clause. Like this:
Thread t = new Thread(new ThreadStart(ThreadMethod));
t.Start();
void ThreadMethod() {
try
{
throw new InvalidOperationException();
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}
Additionally, for the handled exceptions in the .Net application, there is
some rules we should be aware of how to handle it.(recover from the
unhandled exception.), I recommanded you read "How .NET Deals with
Unhandled Exceptions" section in the excellent article writen by Jason
Clark below:
"Unexpected Errors in Managed Applications"
http://msdn.microsoft.com/msdnmag/issues/04/06/NET/
Hope this helps
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.