Thread Question -=> System.NullReferenceException after exiting main()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Elliott

    #1

    Thread Question -=> System.NullReferenceException after exiting main()

    I have created an application that will dynamically load other DLLs (plugins).
    The new plugin is a winform with an embedded IE Browser.

    I am wanting to have the form to run in its own thread. This would allow for other
    plugins and the main application to be free to do other work. I have written a
    little TestDriver for the plugin and am having some difficulty.

    If I don't use threads everything works just fine. If I do use threads, upon finishing
    the thread.Join() loop, I step to the end of main and I receive the same exception 3 times.

    Any help would be appreciated.

    Dave
    DavidElliott@Be llsouth.net

    =============== =============== =============== =============== =

    An unhandled exception of type 'System.NullRef erenceException '
    occurred in system.windows. forms.dll

    Additional information: Object reference not set to an instance of an object.

    =============== =============== =============== =============== =============== ===
    [STAThread]
    static void Main()
    {
    IPlugIn plugin = null;
    Assembly assembly = null;
    Object obj = null;
    String s = null;
    Thread thr = null;

    try
    {
    assembly = Assembly.LoadFr om("..\\..\\..\ \bin\\Debug\\AJ B.dll");

    Type[] types = assembly.GetExp ortedTypes( );
    foreach (Type t in types )
    {
    Type interfaceT = t.GetInterface( "IPlugIn" );
    if( null != interfaceT )
    {
    plugin = (IPlugIn)assemb ly.CreateInstan ce(t.ToString() );

    obj = assembly.Create Instance(t.Full Name);
    s = obj.GetType().T oString();

    plugin.PluginID = 74;

    thr = new Thread( new ThreadStart(plu gin.Execute) );
    thr.ApartmentSt ate = ApartmentState. STA;
    thr.IsBackgroun d = false;
    thr.Start();
    //plugin.Execute( );
    }
    }
    }
    catch (Exception e)
    {
    s = Application.Exe cutablePath.ToS tring() + "\n" + e.ToString() + e.Message + e.StackTrace;
    MessageBox.Show (s, "ExecutePlugin( ) Error");
    }

    thr.Join();
    }

  • Dave

    #2
    Re: Thread Question -=> System.NullRefe renceException after exiting main()

    A couple of questions...wha t is the exception you are getting? Are you
    getting the exception when you call Join or after the thread has exited and
    you have left the try-catch block and main is about to exit?

    I would set the IsBackground to true. Setting it to a false will prevent
    your application from exiting until the thread has completetly stopped which
    is usually not the type of behavior you will want. In the catch block you
    can simply do a e.ToString() to have the exception print both the message
    and its stack trace. And I would probably move the thr.Join inside the
    try-catch block.

    You should also be aware that using LoadFrom("..\\. .\\..\\.. etc") may cause
    you problems. This path is not in or below the application base directory,
    so if the plugin has other assemblies that it depends on the runtime will
    not be able to automatically resolve them. You would be better off moving
    the plugin to a subdirectory below the application base directory and then
    adding the relative path to the subdirectory to the private bin path of the
    appdomain. Then you could use Load(xxx) rather then LoadFrom.

    "David Elliott" <DavidElliott@B ellSouth.net> wrote in message
    news:3ge5kv8h55 u8l0cd99c24j0q2 rkld2j5nb@4ax.c om...[color=blue]
    > I have created an application that will dynamically load other DLLs[/color]
    (plugins).[color=blue]
    > The new plugin is a winform with an embedded IE Browser.
    >
    > I am wanting to have the form to run in its own thread. This would allow[/color]
    for other[color=blue]
    > plugins and the main application to be free to do other work. I have[/color]
    written a[color=blue]
    > little TestDriver for the plugin and am having some difficulty.
    >
    > If I don't use threads everything works just fine. If I do use threads,[/color]
    upon finishing[color=blue]
    > the thread.Join() loop, I step to the end of main and I receive the same[/color]
    exception 3 times.[color=blue]
    >
    > Any help would be appreciated.
    >
    > Dave
    > DavidElliott@Be llsouth.net
    >
    > =============== =============== =============== =============== =
    >
    > An unhandled exception of type 'System.NullRef erenceException '
    > occurred in system.windows. forms.dll
    >
    > Additional information: Object reference not set to an instance of an[/color]
    object.[color=blue]
    >
    >[/color]
    =============== =============== =============== =============== =============== =
    ==[color=blue]
    > [STAThread]
    > static void Main()
    > {
    > IPlugIn plugin = null;
    > Assembly assembly = null;
    > Object obj = null;
    > String s = null;
    > Thread thr = null;
    >
    > try
    > {
    > assembly = Assembly.LoadFr om("..\\..\\..\ \bin\\Debug\\AJ B.dll");
    >
    > Type[] types = assembly.GetExp ortedTypes( );
    > foreach (Type t in types )
    > {
    > Type interfaceT = t.GetInterface( "IPlugIn" );
    > if( null != interfaceT )
    > {
    > plugin = (IPlugIn)assemb ly.CreateInstan ce(t.ToString() );
    >
    > obj = assembly.Create Instance(t.Full Name);
    > s = obj.GetType().T oString();
    >
    > plugin.PluginID = 74;
    >
    > thr = new Thread( new ThreadStart(plu gin.Execute) );
    > thr.ApartmentSt ate = ApartmentState. STA;
    > thr.IsBackgroun d = false;
    > thr.Start();
    > //plugin.Execute( );
    > }
    > }
    > }
    > catch (Exception e)
    > {
    > s = Application.Exe cutablePath.ToS tring() + "\n" + e.ToString() +[/color]
    e.Message + e.StackTrace;[color=blue]
    > MessageBox.Show (s, "ExecutePlugin( ) Error");
    > }
    >
    > thr.Join();
    > }
    >[/color]


    Comment

    • David Elliott

      #3
      Re: Thread Question -=&gt; System.NullRefe renceException after exiting main()

      See comments inline...

      Any other thoughts are welcomed.

      Thanks,
      Dave
      DavidElliott@Be llSouth.net

      On Wed, 20 Aug 2003 05:13:28 -0500, "Dave" <kdlevine@wi.rr .com> wrote:
      [color=blue]
      >A couple of questions...wha t is the exception you are getting? Are you[/color]

      This is the exception:
      An unhandled exception of type 'System.NullRef erenceException '
      occurred in system.windows. forms.dll
      Additional information: Object reference not set to an instance of an object.

      [color=blue]
      >getting the exception when you call Join or after the thread has exited and
      >you have left the try-catch block and main is about to exit?[/color]

      The join has successfully completed. I step to the last brace } of main and step once
      more. So the application is ending.
      [color=blue]
      >
      > I would set the IsBackground to true. Setting it to a false will prevent
      >your application from exiting until the thread has completetly stopped which
      >is usually not the type of behavior you will want.[/color]

      Will give this a shot.
      [color=blue]
      >In the catch block you
      >can simply do a e.ToString() to have the exception print both the message
      >and its stack trace. And I would probably move the thr.Join inside the
      >try-catch block.
      >
      >You should also be aware that using LoadFrom("..\\. .\\..\\.. etc") may cause
      >you problems. This path is not in or below the application base directory,[/color]

      The main application will be set up correctly. I was just trying to create a driver
      for the plugin to test.
      [color=blue]
      >so if the plugin has other assemblies that it depends on the runtime will
      >not be able to automatically resolve them. You would be better off moving
      >the plugin to a subdirectory below the application base directory and then
      >adding the relative path to the subdirectory to the private bin path of the
      >appdomain. Then you could use Load(xxx) rather then LoadFrom.
      >
      >"David Elliott" <DavidElliott@B ellSouth.net> wrote in message
      >news:3ge5kv8h5 5u8l0cd99c24j0q 2rkld2j5nb@4ax. com...[color=green]
      >> I have created an application that will dynamically load other DLLs[/color]
      >(plugins).[color=green]
      >> The new plugin is a winform with an embedded IE Browser.
      >>
      >> I am wanting to have the form to run in its own thread. This would allow[/color]
      >for other[color=green]
      >> plugins and the main application to be free to do other work. I have[/color]
      >written a[color=green]
      >> little TestDriver for the plugin and am having some difficulty.
      >>
      >> If I don't use threads everything works just fine. If I do use threads,[/color]
      >upon finishing[color=green]
      >> the thread.Join() loop, I step to the end of main and I receive the same[/color]
      >exception 3 times.[color=green]
      >>
      >> Any help would be appreciated.
      >>
      >> Dave
      >> DavidElliott@Be llsouth.net
      >>
      >> =============== =============== =============== =============== =
      >>
      >> An unhandled exception of type 'System.NullRef erenceException '
      >> occurred in system.windows. forms.dll
      >>
      >> Additional information: Object reference not set to an instance of an[/color]
      >object.[color=green]
      >>
      >>[/color]
      >============== =============== =============== =============== =============== ==
      >==[color=green]
      >> [STAThread]
      >> static void Main()
      >> {
      >> IPlugIn plugin = null;
      >> Assembly assembly = null;
      >> Object obj = null;
      >> String s = null;
      >> Thread thr = null;
      >>
      >> try
      >> {
      >> assembly = Assembly.LoadFr om("..\\..\\..\ \bin\\Debug\\AJ B.dll");
      >>
      >> Type[] types = assembly.GetExp ortedTypes( );
      >> foreach (Type t in types )
      >> {
      >> Type interfaceT = t.GetInterface( "IPlugIn" );
      >> if( null != interfaceT )
      >> {
      >> plugin = (IPlugIn)assemb ly.CreateInstan ce(t.ToString() );
      >>
      >> obj = assembly.Create Instance(t.Full Name);
      >> s = obj.GetType().T oString();
      >>
      >> plugin.PluginID = 74;
      >>
      >> thr = new Thread( new ThreadStart(plu gin.Execute) );
      >> thr.ApartmentSt ate = ApartmentState. STA;
      >> thr.IsBackgroun d = false;
      >> thr.Start();
      >> //plugin.Execute( );
      >> }
      >> }
      >> }
      >> catch (Exception e)
      >> {
      >> s = Application.Exe cutablePath.ToS tring() + "\n" + e.ToString() +[/color]
      >e.Message + e.StackTrace;[color=green]
      >> MessageBox.Show (s, "ExecutePlugin( ) Error");
      >> }
      >>
      >> thr.Join();
      >> }
      >>[/color]
      >[/color]


      Comment

      • David Elliott

        #4
        Re: Thread Question -=&gt; System.NullRefe renceException after exiting main()

        If I comment out this line:
        this.browser = new AxSHDocVw.AxWeb Browser();
        from my plugin, then I don't get the exception.

        Any thoughts?
        Dave

        DavidElliott@Be llSouth.net


        On Wed, 20 Aug 2003 05:13:28 -0500, "Dave" <kdlevine@wi.rr .com> wrote:
        [color=blue]
        >A couple of questions...wha t is the exception you are getting? Are you
        >getting the exception when you call Join or after the thread has exited and
        >you have left the try-catch block and main is about to exit?
        >
        > I would set the IsBackground to true. Setting it to a false will prevent
        >your application from exiting until the thread has completetly stopped which
        >is usually not the type of behavior you will want. In the catch block you
        >can simply do a e.ToString() to have the exception print both the message
        >and its stack trace. And I would probably move the thr.Join inside the
        >try-catch block.
        >
        >You should also be aware that using LoadFrom("..\\. .\\..\\.. etc") may cause
        >you problems. This path is not in or below the application base directory,
        >so if the plugin has other assemblies that it depends on the runtime will
        >not be able to automatically resolve them. You would be better off moving
        >the plugin to a subdirectory below the application base directory and then
        >adding the relative path to the subdirectory to the private bin path of the
        >appdomain. Then you could use Load(xxx) rather then LoadFrom.
        >
        >"David Elliott" <DavidElliott@B ellSouth.net> wrote in message
        >news:3ge5kv8h5 5u8l0cd99c24j0q 2rkld2j5nb@4ax. com...[color=green]
        >> I have created an application that will dynamically load other DLLs[/color]
        >(plugins).[color=green]
        >> The new plugin is a winform with an embedded IE Browser.
        >>
        >> I am wanting to have the form to run in its own thread. This would allow[/color]
        >for other[color=green]
        >> plugins and the main application to be free to do other work. I have[/color]
        >written a[color=green]
        >> little TestDriver for the plugin and am having some difficulty.
        >>
        >> If I don't use threads everything works just fine. If I do use threads,[/color]
        >upon finishing[color=green]
        >> the thread.Join() loop, I step to the end of main and I receive the same[/color]
        >exception 3 times.[color=green]
        >>
        >> Any help would be appreciated.
        >>
        >> Dave
        >> DavidElliott@Be llsouth.net
        >>
        >> =============== =============== =============== =============== =
        >>
        >> An unhandled exception of type 'System.NullRef erenceException '
        >> occurred in system.windows. forms.dll
        >>
        >> Additional information: Object reference not set to an instance of an[/color]
        >object.[color=green]
        >>
        >>[/color]
        >============== =============== =============== =============== =============== ==
        >==[color=green]
        >> [STAThread]
        >> static void Main()
        >> {
        >> IPlugIn plugin = null;
        >> Assembly assembly = null;
        >> Object obj = null;
        >> String s = null;
        >> Thread thr = null;
        >>
        >> try
        >> {
        >> assembly = Assembly.LoadFr om("..\\..\\..\ \bin\\Debug\\AJ B.dll");
        >>
        >> Type[] types = assembly.GetExp ortedTypes( );
        >> foreach (Type t in types )
        >> {
        >> Type interfaceT = t.GetInterface( "IPlugIn" );
        >> if( null != interfaceT )
        >> {
        >> plugin = (IPlugIn)assemb ly.CreateInstan ce(t.ToString() );
        >>
        >> obj = assembly.Create Instance(t.Full Name);
        >> s = obj.GetType().T oString();
        >>
        >> plugin.PluginID = 74;
        >>
        >> thr = new Thread( new ThreadStart(plu gin.Execute) );
        >> thr.ApartmentSt ate = ApartmentState. STA;
        >> thr.IsBackgroun d = false;
        >> thr.Start();
        >> //plugin.Execute( );
        >> }
        >> }
        >> }
        >> catch (Exception e)
        >> {
        >> s = Application.Exe cutablePath.ToS tring() + "\n" + e.ToString() +[/color]
        >e.Message + e.StackTrace;[color=green]
        >> MessageBox.Show (s, "ExecutePlugin( ) Error");
        >> }
        >>
        >> thr.Join();
        >> }
        >>[/color]
        >[/color]


        Comment

        • Dave

          #5
          Re: Thread Question -=&gt; System.NullRefe renceException after exiting main()

          Without seeing all the code it's almost impossible to tell what's going on.
          One thing to look for is that you may have finalizers running attempting to
          access objects that have already been garbage collected. You can have your
          plugin call the Dispose method on any objects it have created immediately
          before it's thread finishes running.


          "David Elliott" <DavidElliott@B ellSouth.net> wrote in message
          news:qsk7kv4np8 2bpckft7dsenkdk ntlnv1skt@4ax.c om...[color=blue]
          > If I comment out this line:
          > this.browser = new AxSHDocVw.AxWeb Browser();
          > from my plugin, then I don't get the exception.
          >
          > Any thoughts?
          > Dave
          >
          > DavidElliott@Be llSouth.net
          >
          >
          > On Wed, 20 Aug 2003 05:13:28 -0500, "Dave" <kdlevine@wi.rr .com> wrote:
          >[color=green]
          > >A couple of questions...wha t is the exception you are getting? Are you
          > >getting the exception when you call Join or after the thread has exited[/color][/color]
          and[color=blue][color=green]
          > >you have left the try-catch block and main is about to exit?
          > >
          > > I would set the IsBackground to true. Setting it to a false will[/color][/color]
          prevent[color=blue][color=green]
          > >your application from exiting until the thread has completetly stopped[/color][/color]
          which[color=blue][color=green]
          > >is usually not the type of behavior you will want. In the catch block you
          > >can simply do a e.ToString() to have the exception print both the message
          > >and its stack trace. And I would probably move the thr.Join inside the
          > >try-catch block.
          > >
          > >You should also be aware that using LoadFrom("..\\. .\\..\\.. etc") may[/color][/color]
          cause[color=blue][color=green]
          > >you problems. This path is not in or below the application base[/color][/color]
          directory,[color=blue][color=green]
          > >so if the plugin has other assemblies that it depends on the runtime will
          > >not be able to automatically resolve them. You would be better off moving
          > >the plugin to a subdirectory below the application base directory and[/color][/color]
          then[color=blue][color=green]
          > >adding the relative path to the subdirectory to the private bin path of[/color][/color]
          the[color=blue][color=green]
          > >appdomain. Then you could use Load(xxx) rather then LoadFrom.
          > >
          > >"David Elliott" <DavidElliott@B ellSouth.net> wrote in message
          > >news:3ge5kv8h5 5u8l0cd99c24j0q 2rkld2j5nb@4ax. com...[color=darkred]
          > >> I have created an application that will dynamically load other DLLs[/color]
          > >(plugins).[color=darkred]
          > >> The new plugin is a winform with an embedded IE Browser.
          > >>
          > >> I am wanting to have the form to run in its own thread. This would[/color][/color][/color]
          allow[color=blue][color=green]
          > >for other[color=darkred]
          > >> plugins and the main application to be free to do other work. I have[/color]
          > >written a[color=darkred]
          > >> little TestDriver for the plugin and am having some difficulty.
          > >>
          > >> If I don't use threads everything works just fine. If I do use[/color][/color][/color]
          threads,[color=blue][color=green]
          > >upon finishing[color=darkred]
          > >> the thread.Join() loop, I step to the end of main and I receive the[/color][/color][/color]
          same[color=blue][color=green]
          > >exception 3 times.[color=darkred]
          > >>
          > >> Any help would be appreciated.
          > >>
          > >> Dave
          > >> DavidElliott@Be llsouth.net
          > >>
          > >> =============== =============== =============== =============== =
          > >>
          > >> An unhandled exception of type 'System.NullRef erenceException '
          > >> occurred in system.windows. forms.dll
          > >>
          > >> Additional information: Object reference not set to an instance of an[/color]
          > >object.[color=darkred]
          > >>
          > >>[/color][/color]
          >
          >============== =============== =============== =============== =============== =[/color]
          =[color=blue][color=green]
          > >==[color=darkred]
          > >> [STAThread]
          > >> static void Main()
          > >> {
          > >> IPlugIn plugin = null;
          > >> Assembly assembly = null;
          > >> Object obj = null;
          > >> String s = null;
          > >> Thread thr = null;
          > >>
          > >> try
          > >> {
          > >> assembly = Assembly.LoadFr om("..\\..\\..\ \bin\\Debug\\AJ B.dll");
          > >>
          > >> Type[] types = assembly.GetExp ortedTypes( );
          > >> foreach (Type t in types )
          > >> {
          > >> Type interfaceT = t.GetInterface( "IPlugIn" );
          > >> if( null != interfaceT )
          > >> {
          > >> plugin = (IPlugIn)assemb ly.CreateInstan ce(t.ToString() );
          > >>
          > >> obj = assembly.Create Instance(t.Full Name);
          > >> s = obj.GetType().T oString();
          > >>
          > >> plugin.PluginID = 74;
          > >>
          > >> thr = new Thread( new ThreadStart(plu gin.Execute) );
          > >> thr.ApartmentSt ate = ApartmentState. STA;
          > >> thr.IsBackgroun d = false;
          > >> thr.Start();
          > >> //plugin.Execute( );
          > >> }
          > >> }
          > >> }
          > >> catch (Exception e)
          > >> {
          > >> s = Application.Exe cutablePath.ToS tring() + "\n" + e.ToString() +[/color]
          > >e.Message + e.StackTrace;[color=darkred]
          > >> MessageBox.Show (s, "ExecutePlugin( ) Error");
          > >> }
          > >>
          > >> thr.Join();
          > >> }
          > >>[/color]
          > >[/color]
          >
          >[/color]


          Comment

          • David Elliott

            #6
            Re: Thread Question -=&gt; System.NullRefe renceException after exiting main() - &quot;BrowerPlu gin.zip&quot; (0/1) 13.3 KBytes yEnc

            I have attached a bare minimum, do nothing, uncomplicated project with
            source code to demonstrate the problem.

            There is a Driver that loads an assembly (WebBrowser.dll ) and starts a thread
            with a method that is implemented through an interface (Execute).

            That method (Execute) performs a ShowDialog for a form (Form1) that contains
            an AxWebBrowser. Upon display the AxWebBrowser goes to Google.com.

            Click on the close box and watch the exceptions come in.

            Thanks for you help.

            Dave


            On Wed, 20 Aug 2003 21:40:55 -0500, "Dave" <kdlevine@wi.rr .com> wrote:
            [color=blue]
            >Without seeing all the code it's almost impossible to tell what's going on.
            >One thing to look for is that you may have finalizers running attempting to
            >access objects that have already been garbage collected. You can have your
            >plugin call the Dispose method on any objects it have created immediately
            >before it's thread finishes running.
            >
            >
            >"David Elliott" <DavidElliott@B ellSouth.net> wrote in message
            >news:qsk7kv4np 82bpckft7dsenkd kntlnv1skt@4ax. com...[color=green]
            >> If I comment out this line:
            >> this.browser = new AxSHDocVw.AxWeb Browser();
            >> from my plugin, then I don't get the exception.
            >>
            >> Any thoughts?
            >> Dave
            >>
            >> DavidElliott@Be llSouth.net
            >>
            >>
            >> On Wed, 20 Aug 2003 05:13:28 -0500, "Dave" <kdlevine@wi.rr .com> wrote:
            >>[color=darkred]
            >> >A couple of questions...wha t is the exception you are getting? Are you
            >> >getting the exception when you call Join or after the thread has exited[/color][/color]
            >and[color=green][color=darkred]
            >> >you have left the try-catch block and main is about to exit?
            >> >
            >> > I would set the IsBackground to true. Setting it to a false will[/color][/color]
            >prevent[color=green][color=darkred]
            >> >your application from exiting until the thread has completetly stopped[/color][/color]
            >which[color=green][color=darkred]
            >> >is usually not the type of behavior you will want. In the catch block you
            >> >can simply do a e.ToString() to have the exception print both the message
            >> >and its stack trace. And I would probably move the thr.Join inside the
            >> >try-catch block.
            >> >
            >> >You should also be aware that using LoadFrom("..\\. .\\..\\.. etc") may[/color][/color]
            >cause[color=green][color=darkred]
            >> >you problems. This path is not in or below the application base[/color][/color]
            >directory,[color=green][color=darkred]
            >> >so if the plugin has other assemblies that it depends on the runtime will
            >> >not be able to automatically resolve them. You would be better off moving
            >> >the plugin to a subdirectory below the application base directory and[/color][/color]
            >then[color=green][color=darkred]
            >> >adding the relative path to the subdirectory to the private bin path of[/color][/color]
            >the[color=green][color=darkred]
            >> >appdomain. Then you could use Load(xxx) rather then LoadFrom.
            >> >
            >> >"David Elliott" <DavidElliott@B ellSouth.net> wrote in message
            >> >news:3ge5kv8h5 5u8l0cd99c24j0q 2rkld2j5nb@4ax. com...
            >> >> I have created an application that will dynamically load other DLLs
            >> >(plugins).
            >> >> The new plugin is a winform with an embedded IE Browser.
            >> >>
            >> >> I am wanting to have the form to run in its own thread. This would[/color][/color]
            >allow[color=green][color=darkred]
            >> >for other
            >> >> plugins and the main application to be free to do other work. I have
            >> >written a
            >> >> little TestDriver for the plugin and am having some difficulty.
            >> >>
            >> >> If I don't use threads everything works just fine. If I do use[/color][/color]
            >threads,[color=green][color=darkred]
            >> >upon finishing
            >> >> the thread.Join() loop, I step to the end of main and I receive the[/color][/color]
            >same[color=green][color=darkred]
            >> >exception 3 times.
            >> >>
            >> >> Any help would be appreciated.
            >> >>
            >> >> Dave
            >> >> DavidElliott@Be llsouth.net
            >> >>
            >> >> =============== =============== =============== =============== =
            >> >>
            >> >> An unhandled exception of type 'System.NullRef erenceException '
            >> >> occurred in system.windows. forms.dll
            >> >>
            >> >> Additional information: Object reference not set to an instance of an
            >> >object.
            >> >>
            >> >>[/color]
            >>
            >>============= =============== =============== =============== =============== ==[/color]
            >=[color=green][color=darkred]
            >> >==
            >> >> [STAThread]
            >> >> static void Main()
            >> >> {
            >> >> IPlugIn plugin = null;
            >> >> Assembly assembly = null;
            >> >> Object obj = null;
            >> >> String s = null;
            >> >> Thread thr = null;
            >> >>
            >> >> try
            >> >> {
            >> >> assembly = Assembly.LoadFr om("..\\..\\..\ \bin\\Debug\\AJ B.dll");
            >> >>
            >> >> Type[] types = assembly.GetExp ortedTypes( );
            >> >> foreach (Type t in types )
            >> >> {
            >> >> Type interfaceT = t.GetInterface( "IPlugIn" );
            >> >> if( null != interfaceT )
            >> >> {
            >> >> plugin = (IPlugIn)assemb ly.CreateInstan ce(t.ToString() );
            >> >>
            >> >> obj = assembly.Create Instance(t.Full Name);
            >> >> s = obj.GetType().T oString();
            >> >>
            >> >> plugin.PluginID = 74;
            >> >>
            >> >> thr = new Thread( new ThreadStart(plu gin.Execute) );
            >> >> thr.ApartmentSt ate = ApartmentState. STA;
            >> >> thr.IsBackgroun d = false;
            >> >> thr.Start();
            >> >> //plugin.Execute( );
            >> >> }
            >> >> }
            >> >> }
            >> >> catch (Exception e)
            >> >> {
            >> >> s = Application.Exe cutablePath.ToS tring() + "\n" + e.ToString() +
            >> >e.Message + e.StackTrace;
            >> >> MessageBox.Show (s, "ExecutePlugin( ) Error");
            >> >> }
            >> >>
            >> >> thr.Join();
            >> >> }
            >> >>
            >> >[/color]
            >>
            >>[/color]
            >[/color]


            Comment

            • Dave

              #7
              Re: Thread Question -=&gt; System.NullRefe renceException after exiting main() - &quot;BrowerPlu gin.zip&quot; (0/1) 13.3 KBytes yEnc

              All that came through was gibberish - did you attach a file to the email?

              Doing UI in a worker thread is risky - unless you have a message pump
              somewhere delivering window events things will not work properly. If the
              dialog form has a dispose method you should call it. You can also call
              GC.WaitForPendi ngFinalizers to ensure that it has cleaned itself up before
              letting the thread terminate.

              "David Elliott" <DavidElliott@B ellSouth.net> wrote in message
              news:svg8kv4scs gts1no2n6j38cu1 8gr5qih24@4ax.c om...[color=blue]
              > I have attached a bare minimum, do nothing, uncomplicated project with
              > source code to demonstrate the problem.
              >
              > There is a Driver that loads an assembly (WebBrowser.dll ) and starts a[/color]
              thread[color=blue]
              > with a method that is implemented through an interface (Execute).
              >
              > That method (Execute) performs a ShowDialog for a form (Form1) that[/color]
              contains[color=blue]
              > an AxWebBrowser. Upon display the AxWebBrowser goes to Google.com.
              >
              > Click on the close box and watch the exceptions come in.
              >
              > Thanks for you help.
              >
              > Dave
              >
              >
              > On Wed, 20 Aug 2003 21:40:55 -0500, "Dave" <kdlevine@wi.rr .com> wrote:
              >[color=green]
              > >Without seeing all the code it's almost impossible to tell what's going[/color][/color]
              on.[color=blue][color=green]
              > >One thing to look for is that you may have finalizers running attempting[/color][/color]
              to[color=blue][color=green]
              > >access objects that have already been garbage collected. You can have[/color][/color]
              your[color=blue][color=green]
              > >plugin call the Dispose method on any objects it have created immediately
              > >before it's thread finishes running.
              > >
              > >
              > >"David Elliott" <DavidElliott@B ellSouth.net> wrote in message
              > >news:qsk7kv4np 82bpckft7dsenkd kntlnv1skt@4ax. com...[color=darkred]
              > >> If I comment out this line:
              > >> this.browser = new AxSHDocVw.AxWeb Browser();
              > >> from my plugin, then I don't get the exception.
              > >>
              > >> Any thoughts?
              > >> Dave
              > >>
              > >> DavidElliott@Be llSouth.net
              > >>
              > >>
              > >> On Wed, 20 Aug 2003 05:13:28 -0500, "Dave" <kdlevine@wi.rr .com> wrote:
              > >>
              > >> >A couple of questions...wha t is the exception you are getting? Are you
              > >> >getting the exception when you call Join or after the thread has[/color][/color][/color]
              exited[color=blue][color=green]
              > >and[color=darkred]
              > >> >you have left the try-catch block and main is about to exit?
              > >> >
              > >> > I would set the IsBackground to true. Setting it to a false will[/color]
              > >prevent[color=darkred]
              > >> >your application from exiting until the thread has completetly stopped[/color]
              > >which[color=darkred]
              > >> >is usually not the type of behavior you will want. In the catch block[/color][/color][/color]
              you[color=blue][color=green][color=darkred]
              > >> >can simply do a e.ToString() to have the exception print both the[/color][/color][/color]
              message[color=blue][color=green][color=darkred]
              > >> >and its stack trace. And I would probably move the thr.Join inside the
              > >> >try-catch block.
              > >> >
              > >> >You should also be aware that using LoadFrom("..\\. .\\..\\.. etc") may[/color]
              > >cause[color=darkred]
              > >> >you problems. This path is not in or below the application base[/color]
              > >directory,[color=darkred]
              > >> >so if the plugin has other assemblies that it depends on the runtime[/color][/color][/color]
              will[color=blue][color=green][color=darkred]
              > >> >not be able to automatically resolve them. You would be better off[/color][/color][/color]
              moving[color=blue][color=green][color=darkred]
              > >> >the plugin to a subdirectory below the application base directory and[/color]
              > >then[color=darkred]
              > >> >adding the relative path to the subdirectory to the private bin path[/color][/color][/color]
              of[color=blue][color=green]
              > >the[color=darkred]
              > >> >appdomain. Then you could use Load(xxx) rather then LoadFrom.
              > >> >
              > >> >"David Elliott" <DavidElliott@B ellSouth.net> wrote in message
              > >> >news:3ge5kv8h5 5u8l0cd99c24j0q 2rkld2j5nb@4ax. com...
              > >> >> I have created an application that will dynamically load other DLLs
              > >> >(plugins).
              > >> >> The new plugin is a winform with an embedded IE Browser.
              > >> >>
              > >> >> I am wanting to have the form to run in its own thread. This would[/color]
              > >allow[color=darkred]
              > >> >for other
              > >> >> plugins and the main application to be free to do other work. I[/color][/color][/color]
              have[color=blue][color=green][color=darkred]
              > >> >written a
              > >> >> little TestDriver for the plugin and am having some difficulty.
              > >> >>
              > >> >> If I don't use threads everything works just fine. If I do use[/color]
              > >threads,[color=darkred]
              > >> >upon finishing
              > >> >> the thread.Join() loop, I step to the end of main and I receive the[/color]
              > >same[color=darkred]
              > >> >exception 3 times.
              > >> >>
              > >> >> Any help would be appreciated.
              > >> >>
              > >> >> Dave
              > >> >> DavidElliott@Be llsouth.net
              > >> >>
              > >> >> =============== =============== =============== =============== =
              > >> >>
              > >> >> An unhandled exception of type 'System.NullRef erenceException '
              > >> >> occurred in system.windows. forms.dll
              > >> >>
              > >> >> Additional information: Object reference not set to an instance of[/color][/color][/color]
              an[color=blue][color=green][color=darkred]
              > >> >object.
              > >> >>
              > >> >>
              > >>[/color][/color]
              >[color=green]
              >>============= =============== =============== =============== =============== =[/color][/color]
              =[color=blue][color=green]
              > >=[color=darkred]
              > >> >==
              > >> >> [STAThread]
              > >> >> static void Main()
              > >> >> {
              > >> >> IPlugIn plugin = null;
              > >> >> Assembly assembly = null;
              > >> >> Object obj = null;
              > >> >> String s = null;
              > >> >> Thread thr = null;
              > >> >>
              > >> >> try
              > >> >> {
              > >> >> assembly = Assembly.LoadFr om("..\\..\\..\ \bin\\Debug\\AJ B.dll");
              > >> >>
              > >> >> Type[] types = assembly.GetExp ortedTypes( );
              > >> >> foreach (Type t in types )
              > >> >> {
              > >> >> Type interfaceT = t.GetInterface( "IPlugIn" );
              > >> >> if( null != interfaceT )
              > >> >> {
              > >> >> plugin = (IPlugIn)assemb ly.CreateInstan ce(t.ToString() );
              > >> >>
              > >> >> obj = assembly.Create Instance(t.Full Name);
              > >> >> s = obj.GetType().T oString();
              > >> >>
              > >> >> plugin.PluginID = 74;
              > >> >>
              > >> >> thr = new Thread( new ThreadStart(plu gin.Execute) );
              > >> >> thr.ApartmentSt ate = ApartmentState. STA;
              > >> >> thr.IsBackgroun d = false;
              > >> >> thr.Start();
              > >> >> //plugin.Execute( );
              > >> >> }
              > >> >> }
              > >> >> }
              > >> >> catch (Exception e)
              > >> >> {
              > >> >> s = Application.Exe cutablePath.ToS tring() + "\n" + e.ToString() +
              > >> >e.Message + e.StackTrace;
              > >> >> MessageBox.Show (s, "ExecutePlugin( ) Error");
              > >> >> }
              > >> >>
              > >> >> thr.Join();
              > >> >> }
              > >> >>
              > >> >
              > >>
              > >>[/color]
              > >[/color]
              >
              >[/color]


              Comment

              • David Elliott

                #8
                Re: Thread Question -=&gt; System.NullRefe renceException after exiting main() - &quot;BrowerPlu gin.zip&quot; (0/1) 13.3 KBytes yEnc

                Between the thread and using ShowDialog(), I should have a message pump.
                This is one of the reasons that I was doing it this way. This would allow the main
                application to start other plugins as well as continue with its own thing.

                I added the generic Dispose() that was inherited (ie I didn't implement IDisposable)
                as you suggested and this took care of the problem.

                I don't really understand why just instantiating a class of type AxSHDocVw.AxWeb Browser
                would create the exceptions. I will have to see if I can find an answer.

                Thanks for your time, patience and solution.

                Cheers,
                Dave

                On Thu, 21 Aug 2003 05:48:26 -0500, "Dave" <kdlevine@wi.rr .com> wrote:
                [color=blue]
                >All that came through was gibberish - did you attach a file to the email?
                >
                >Doing UI in a worker thread is risky - unless you have a message pump
                >somewhere delivering window events things will not work properly. If the
                >dialog form has a dispose method you should call it. You can also call
                >GC.WaitForPend ingFinalizers to ensure that it has cleaned itself up before
                >letting the thread terminate.
                >
                >"David Elliott" <DavidElliott@B ellSouth.net> wrote in message
                >news:svg8kv4sc sgts1no2n6j38cu 18gr5qih24@4ax. com...[color=green]
                >> I have attached a bare minimum, do nothing, uncomplicated project with
                >> source code to demonstrate the problem.
                >>
                >> There is a Driver that loads an assembly (WebBrowser.dll ) and starts a[/color]
                >thread[color=green]
                >> with a method that is implemented through an interface (Execute).
                >>
                >> That method (Execute) performs a ShowDialog for a form (Form1) that[/color]
                >contains[color=green]
                >> an AxWebBrowser. Upon display the AxWebBrowser goes to Google.com.
                >>
                >> Click on the close box and watch the exceptions come in.
                >>
                >> Thanks for you help.
                >>
                >> Dave
                >>
                >>
                >> On Wed, 20 Aug 2003 21:40:55 -0500, "Dave" <kdlevine@wi.rr .com> wrote:
                >>[color=darkred]
                >> >Without seeing all the code it's almost impossible to tell what's going[/color][/color]
                >on.[color=green][color=darkred]
                >> >One thing to look for is that you may have finalizers running attempting[/color][/color]
                >to[color=green][color=darkred]
                >> >access objects that have already been garbage collected. You can have[/color][/color]
                >your[color=green][color=darkred]
                >> >plugin call the Dispose method on any objects it have created immediately
                >> >before it's thread finishes running.
                >> >
                >> >
                >> >"David Elliott" <DavidElliott@B ellSouth.net> wrote in message
                >> >news:qsk7kv4np 82bpckft7dsenkd kntlnv1skt@4ax. com...
                >> >> If I comment out this line:
                >> >> this.browser = new AxSHDocVw.AxWeb Browser();
                >> >> from my plugin, then I don't get the exception.
                >> >>
                >> >> Any thoughts?
                >> >> Dave
                >> >>
                >> >> DavidElliott@Be llSouth.net
                >> >>
                >> >>
                >> >> On Wed, 20 Aug 2003 05:13:28 -0500, "Dave" <kdlevine@wi.rr .com> wrote:
                >> >>
                >> >> >A couple of questions...wha t is the exception you are getting? Are you
                >> >> >getting the exception when you call Join or after the thread has[/color][/color]
                >exited[color=green][color=darkred]
                >> >and
                >> >> >you have left the try-catch block and main is about to exit?
                >> >> >
                >> >> > I would set the IsBackground to true. Setting it to a false will
                >> >prevent
                >> >> >your application from exiting until the thread has completetly stopped
                >> >which
                >> >> >is usually not the type of behavior you will want. In the catch block[/color][/color]
                >you[color=green][color=darkred]
                >> >> >can simply do a e.ToString() to have the exception print both the[/color][/color]
                >message[color=green][color=darkred]
                >> >> >and its stack trace. And I would probably move the thr.Join inside the
                >> >> >try-catch block.
                >> >> >
                >> >> >You should also be aware that using LoadFrom("..\\. .\\..\\.. etc") may
                >> >cause
                >> >> >you problems. This path is not in or below the application base
                >> >directory,
                >> >> >so if the plugin has other assemblies that it depends on the runtime[/color][/color]
                >will[color=green][color=darkred]
                >> >> >not be able to automatically resolve them. You would be better off[/color][/color]
                >moving[color=green][color=darkred]
                >> >> >the plugin to a subdirectory below the application base directory and
                >> >then
                >> >> >adding the relative path to the subdirectory to the private bin path[/color][/color]
                >of[color=green][color=darkred]
                >> >the
                >> >> >appdomain. Then you could use Load(xxx) rather then LoadFrom.
                >> >> >
                >> >> >"David Elliott" <DavidElliott@B ellSouth.net> wrote in message
                >> >> >news:3ge5kv8h5 5u8l0cd99c24j0q 2rkld2j5nb@4ax. com...
                >> >> >> I have created an application that will dynamically load other DLLs
                >> >> >(plugins).
                >> >> >> The new plugin is a winform with an embedded IE Browser.
                >> >> >>
                >> >> >> I am wanting to have the form to run in its own thread. This would
                >> >allow
                >> >> >for other
                >> >> >> plugins and the main application to be free to do other work. I[/color][/color]
                >have[color=green][color=darkred]
                >> >> >written a
                >> >> >> little TestDriver for the plugin and am having some difficulty.
                >> >> >>
                >> >> >> If I don't use threads everything works just fine. If I do use
                >> >threads,
                >> >> >upon finishing
                >> >> >> the thread.Join() loop, I step to the end of main and I receive the
                >> >same
                >> >> >exception 3 times.
                >> >> >>
                >> >> >> Any help would be appreciated.
                >> >> >>
                >> >> >> Dave
                >> >> >> DavidElliott@Be llsouth.net
                >> >> >>
                >> >> >> =============== =============== =============== =============== =
                >> >> >>
                >> >> >> An unhandled exception of type 'System.NullRef erenceException '
                >> >> >> occurred in system.windows. forms.dll
                >> >> >>
                >> >> >> Additional information: Object reference not set to an instance of[/color][/color]
                >an[color=green][color=darkred]
                >> >> >object.
                >> >> >>
                >> >> >>
                >> >>[/color]
                >>[color=darkred]
                >>>============ =============== =============== =============== =============== ==[/color][/color]
                >=[color=green][color=darkred]
                >> >=
                >> >> >==
                >> >> >> [STAThread]
                >> >> >> static void Main()
                >> >> >> {
                >> >> >> IPlugIn plugin = null;
                >> >> >> Assembly assembly = null;
                >> >> >> Object obj = null;
                >> >> >> String s = null;
                >> >> >> Thread thr = null;
                >> >> >>
                >> >> >> try
                >> >> >> {
                >> >> >> assembly = Assembly.LoadFr om("..\\..\\..\ \bin\\Debug\\AJ B.dll");
                >> >> >>
                >> >> >> Type[] types = assembly.GetExp ortedTypes( );
                >> >> >> foreach (Type t in types )
                >> >> >> {
                >> >> >> Type interfaceT = t.GetInterface( "IPlugIn" );
                >> >> >> if( null != interfaceT )
                >> >> >> {
                >> >> >> plugin = (IPlugIn)assemb ly.CreateInstan ce(t.ToString() );
                >> >> >>
                >> >> >> obj = assembly.Create Instance(t.Full Name);
                >> >> >> s = obj.GetType().T oString();
                >> >> >>
                >> >> >> plugin.PluginID = 74;
                >> >> >>
                >> >> >> thr = new Thread( new ThreadStart(plu gin.Execute) );
                >> >> >> thr.ApartmentSt ate = ApartmentState. STA;
                >> >> >> thr.IsBackgroun d = false;
                >> >> >> thr.Start();
                >> >> >> //plugin.Execute( );
                >> >> >> }
                >> >> >> }
                >> >> >> }
                >> >> >> catch (Exception e)
                >> >> >> {
                >> >> >> s = Application.Exe cutablePath.ToS tring() + "\n" + e.ToString() +
                >> >> >e.Message + e.StackTrace;
                >> >> >> MessageBox.Show (s, "ExecutePlugin( ) Error");
                >> >> >> }
                >> >> >>
                >> >> >> thr.Join();
                >> >> >> }
                >> >> >>
                >> >> >
                >> >>
                >> >>
                >> >[/color]
                >>
                >>[/color]
                >[/color]


                Comment

                Working...