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

D

David Elliott

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
(e-mail address removed)

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

An unhandled exception of type 'System.NullReferenceException'
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.LoadFrom("..\\..\\..\\bin\\Debug\\AJB.dll");

Type[] types = assembly.GetExportedTypes( );
foreach (Type t in types )
{
Type interfaceT = t.GetInterface( "IPlugIn" );
if( null != interfaceT )
{
plugin = (IPlugIn)assembly.CreateInstance(t.ToString());

obj = assembly.CreateInstance(t.FullName);
s = obj.GetType().ToString();

plugin.PluginID = 74;

thr = new Thread( new ThreadStart(plugin.Execute) );
thr.ApartmentState = ApartmentState.STA;
thr.IsBackground = false;
thr.Start();
//plugin.Execute();
}
}
}
catch (Exception e)
{
s = Application.ExecutablePath.ToString() + "\n" + e.ToString() + e.Message + e.StackTrace;
MessageBox.Show(s, "ExecutePlugin() Error");
}

thr.Join();
}
 
D

Dave

A couple of questions...what 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 said:
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
(e-mail address removed)

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

An unhandled exception of type 'System.NullReferenceException'
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.LoadFrom("..\\..\\..\\bin\\Debug\\AJB.dll");

Type[] types = assembly.GetExportedTypes( );
foreach (Type t in types )
{
Type interfaceT = t.GetInterface( "IPlugIn" );
if( null != interfaceT )
{
plugin = (IPlugIn)assembly.CreateInstance(t.ToString());

obj = assembly.CreateInstance(t.FullName);
s = obj.GetType().ToString();

plugin.PluginID = 74;

thr = new Thread( new ThreadStart(plugin.Execute) );
thr.ApartmentState = ApartmentState.STA;
thr.IsBackground = false;
thr.Start();
//plugin.Execute();
}
}
}
catch (Exception e)
{
s = Application.ExecutablePath.ToString() + "\n" + e.ToString() + e.Message + e.StackTrace;
MessageBox.Show(s, "ExecutePlugin() Error");
}

thr.Join();
}
 
D

David Elliott

See comments inline...

Any other thoughts are welcomed.

Thanks,
Dave
(e-mail address removed)

A couple of questions...what is the exception you are getting? Are you

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

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?

The join has successfully completed. I step to the last brace } of main and step once
more. So the application is ending.
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.

Will give this a shot.
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,

The main application will be set up correctly. I was just trying to create a driver
for the plugin to test.
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 said:
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
(e-mail address removed)

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

An unhandled exception of type 'System.NullReferenceException'
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.LoadFrom("..\\..\\..\\bin\\Debug\\AJB.dll");

Type[] types = assembly.GetExportedTypes( );
foreach (Type t in types )
{
Type interfaceT = t.GetInterface( "IPlugIn" );
if( null != interfaceT )
{
plugin = (IPlugIn)assembly.CreateInstance(t.ToString());

obj = assembly.CreateInstance(t.FullName);
s = obj.GetType().ToString();

plugin.PluginID = 74;

thr = new Thread( new ThreadStart(plugin.Execute) );
thr.ApartmentState = ApartmentState.STA;
thr.IsBackground = false;
thr.Start();
//plugin.Execute();
}
}
}
catch (Exception e)
{
s = Application.ExecutablePath.ToString() + "\n" + e.ToString() + e.Message + e.StackTrace;
MessageBox.Show(s, "ExecutePlugin() Error");
}

thr.Join();
}
 
D

David Elliott

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

Any thoughts?
Dave

(e-mail address removed)


A couple of questions...what 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 said:
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
(e-mail address removed)

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

An unhandled exception of type 'System.NullReferenceException'
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.LoadFrom("..\\..\\..\\bin\\Debug\\AJB.dll");

Type[] types = assembly.GetExportedTypes( );
foreach (Type t in types )
{
Type interfaceT = t.GetInterface( "IPlugIn" );
if( null != interfaceT )
{
plugin = (IPlugIn)assembly.CreateInstance(t.ToString());

obj = assembly.CreateInstance(t.FullName);
s = obj.GetType().ToString();

plugin.PluginID = 74;

thr = new Thread( new ThreadStart(plugin.Execute) );
thr.ApartmentState = ApartmentState.STA;
thr.IsBackground = false;
thr.Start();
//plugin.Execute();
}
}
}
catch (Exception e)
{
s = Application.ExecutablePath.ToString() + "\n" + e.ToString() + e.Message + e.StackTrace;
MessageBox.Show(s, "ExecutePlugin() Error");
}

thr.Join();
}
 
D

Dave

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 said:
If I comment out this line:
this.browser = new AxSHDocVw.AxWebBrowser();
from my plugin, then I don't get the exception.

Any thoughts?
Dave

(e-mail address removed)


A couple of questions...what 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 said:
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
(e-mail address removed)

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

An unhandled exception of type 'System.NullReferenceException'
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.LoadFrom("..\\..\\..\\bin\\Debug\\AJB.dll");

Type[] types = assembly.GetExportedTypes( );
foreach (Type t in types )
{
Type interfaceT = t.GetInterface( "IPlugIn" );
if( null != interfaceT )
{
plugin = (IPlugIn)assembly.CreateInstance(t.ToString());

obj = assembly.CreateInstance(t.FullName);
s = obj.GetType().ToString();

plugin.PluginID = 74;

thr = new Thread( new ThreadStart(plugin.Execute) );
thr.ApartmentState = ApartmentState.STA;
thr.IsBackground = false;
thr.Start();
//plugin.Execute();
}
}
}
catch (Exception e)
{
s = Application.ExecutablePath.ToString() + "\n" + e.ToString() + e.Message + e.StackTrace;
MessageBox.Show(s, "ExecutePlugin() Error");
}

thr.Join();
}
 
D

David Elliott

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


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 said:
If I comment out this line:
this.browser = new AxSHDocVw.AxWebBrowser();
from my plugin, then I don't get the exception.

Any thoughts?
Dave

(e-mail address removed)


A couple of questions...what 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.

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
(e-mail address removed)

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

An unhandled exception of type 'System.NullReferenceException'
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.LoadFrom("..\\..\\..\\bin\\Debug\\AJB.dll");

Type[] types = assembly.GetExportedTypes( );
foreach (Type t in types )
{
Type interfaceT = t.GetInterface( "IPlugIn" );
if( null != interfaceT )
{
plugin = (IPlugIn)assembly.CreateInstance(t.ToString());

obj = assembly.CreateInstance(t.FullName);
s = obj.GetType().ToString();

plugin.PluginID = 74;

thr = new Thread( new ThreadStart(plugin.Execute) );
thr.ApartmentState = ApartmentState.STA;
thr.IsBackground = false;
thr.Start();
//plugin.Execute();
}
}
}
catch (Exception e)
{
s = Application.ExecutablePath.ToString() + "\n" + e.ToString() +
e.Message + e.StackTrace;
MessageBox.Show(s, "ExecutePlugin() Error");
}

thr.Join();
}
 
D

Dave

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.WaitForPendingFinalizers to ensure that it has cleaned itself up before
letting the thread terminate.

David Elliott said:
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


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.


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

try
{
assembly = Assembly.LoadFrom("..\\..\\..\\bin\\Debug\\AJB.dll");

Type[] types = assembly.GetExportedTypes( );
foreach (Type t in types )
{
Type interfaceT = t.GetInterface( "IPlugIn" );
if( null != interfaceT )
{
plugin = (IPlugIn)assembly.CreateInstance(t.ToString());

obj = assembly.CreateInstance(t.FullName);
s = obj.GetType().ToString();

plugin.PluginID = 74;

thr = new Thread( new ThreadStart(plugin.Execute) );
thr.ApartmentState = ApartmentState.STA;
thr.IsBackground = false;
thr.Start();
//plugin.Execute();
}
}
}
catch (Exception e)
{
s = Application.ExecutablePath.ToString() + "\n" + e.ToString() +
e.Message + e.StackTrace;
MessageBox.Show(s, "ExecutePlugin() Error");
}

thr.Join();
}
 
D

David Elliott

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.AxWebBrowser
would create the exceptions. I will have to see if I can find an answer.

Thanks for your time, patience and solution.

Cheers,
Dave

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.WaitForPendingFinalizers to ensure that it has cleaned itself up before
letting the thread terminate.

David Elliott said:
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


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.


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

Any thoughts?
Dave

(e-mail address removed)



A couple of questions...what 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.

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
(e-mail address removed)

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

An unhandled exception of type 'System.NullReferenceException'
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.LoadFrom("..\\..\\..\\bin\\Debug\\AJB.dll");

Type[] types = assembly.GetExportedTypes( );
foreach (Type t in types )
{
Type interfaceT = t.GetInterface( "IPlugIn" );
if( null != interfaceT )
{
plugin = (IPlugIn)assembly.CreateInstance(t.ToString());

obj = assembly.CreateInstance(t.FullName);
s = obj.GetType().ToString();

plugin.PluginID = 74;

thr = new Thread( new ThreadStart(plugin.Execute) );
thr.ApartmentState = ApartmentState.STA;
thr.IsBackground = false;
thr.Start();
//plugin.Execute();
}
}
}
catch (Exception e)
{
s = Application.ExecutablePath.ToString() + "\n" + e.ToString() +
e.Message + e.StackTrace;
MessageBox.Show(s, "ExecutePlugin() Error");
}

thr.Join();
}
 

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