Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

System.Reflection.TargetInvocationException was unhandled

Status
Not open for further replies.

otoprpradip

Programmer
Nov 10, 2008
3
In my program following line:
-------------
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
------------
is giving System.Reflection.TargetInvocationException was unhandled by user code Message "Exception has been thrown by the target of an invocation."Source="mscorlib"
Note: It is thrown only when I am out of memory, otherwise it is ok.
What is the solution?
 
what is the full stack trace. usually TargetInvocationException wrap another exception (inner exception).

if memory is infact the problem you have 2 choices.
1. profile the assembly for memory leaks.
2. add more memory to the box.

Two big culprits for memory usage are:
1. queries pulling too much data.
2. not properly disposing of disposable resource (database connections and IO objects). the best way to handle that is
Code:
using(IDisposable obj = new disposable_object())
{
   //do work here
}
this will implement dispose automatically, even in the event of an exception. if you don't use using statements, or they are not the right choice for the scenario. you would need to do something like this
Code:
IDisposable obj = new disposable_object();
try
{
   //do work here
}
Finally
{
   obj.Dispose();
}
note I never catch the exception. this is by design. I don't want to swallow it, and I can log it later down the line.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top