Rakesh

Rakesh

  • NA
  • 1
  • 0

Thrwoing exceptions to a different thread

Jan 8 2008 5:02 AM

Hi,

I have a situation in which the worker thread invoke a method for doing some operation. In the method before doing the operations it invokes the mainthread and does the operation in the mainthread. The operations throws an exception which will inturn gives a crash in the worker thread as the stack already unwinded when it reaches the worker thread.

Sample code

public class A
{
      main()
      {
         Thread wrkThrd = new Thread(DowrkThrd );
         wrkThrd .Name = "My worker";
          wrkThrd .Start();

               // Put the main thread to sleep to
            // allow the worker thread to do some work:
            Thread.Sleep(1000);
         wrkThrd .join();
        }

          DowrkThrd()
          {
             //worker thread
            // do some operation
           // Calls an api method
           api_CreateMyWindow();
          }
}

//different place
api_CreateMyWindow()
{

            ///checks whether to invoke main thread
            if(!isMainthread)
            {
                  // change to mainthread and invoke the method
                     mainthread.invoke(api_CreateMyWindow)
                     //using delegate call ths same method

            }
           else
          {
            // call comes here from the mainthread
            // do some operation which throws exception
              
             createwindow();

//When an exception ocuured here in the mainthread which cannot be propagted to the wrkThrd
           
            }
}