A background task can fail while exposing only the message “Exception has been thrown by the target of an invocation.” In this case, reflection invokes Run, so the visible exception describes the invocation boundary rather than the failure inside the method. Log the complete exception object and inspect its nested exception data instead of recording only e.Message.
Identify the Actual Array Failure
The array mass contains 10 elements, but the loop attempts 12 indexed reads. When i reaches the first invalid index, Q = mass[i] throws an exception. Reflection then presents an invocation-level wrapper to the calling delegate.
| Code element | Evidence | Decision |
|---|---|---|
mass |
10 elements | Use the array length as the loop limit. |
| Loop condition | i < 12 |
It permits invalid array indexes. |
| Logged value | e.Message |
Replace it with the complete exception object. |
Capture the Complete Exception
Catch the exception at the task delegate or inside Run, then pass the complete exception object to the application's logging interface. A full exception representation preserves the exception type, stack information, and nested exception details available from the reflection call; the message string alone does not.
Action action = delegate ()
{
try
{
object obj = instance;
object[] parameters = [];
MethodInfo mi = type.GetMethod("Run");
mi.Invoke(obj, parameters);
}
catch (Exception e)
{
Log(e); // Log the complete exception, not only e.Message.
}
};
Task task = Task.Factory.StartNew(action);
Log represents the existing application logging interface; the evidence does not specify its implementation. Capturing the exception inside the delegate does not require awaiting the task.
Correct the Loop Boundary
Bind the loop limit to the actual array length so the code remains valid if the array changes. This removes the demonstrated out-of-range access while preserving the existing cycle behavior.
for (int i = 0; i < mass.Length; i++)
{
Q = mass[i];
Thread.Sleep(cycle);
}
Verify the Diagnosis and Repair
- Run the original
i < 12logic and confirm that the logger receives the complete exception object, including the underlying failure details. - Change the condition to
i < mass.Lengthand repeat the enabled cycle. - Confirm that all 10 elements are read without an array-bound failure and that task termination still follows the existing
terminatedflag.
FAQ
Why does the task report only “Exception has been thrown by the target of an invocation”?
Run is called through reflection, so the observed message describes the invocation boundary. Log the complete exception object and inspect its nested details to find the failure raised inside Run.
Why does the array loop fail when i is less than 12?
mass has 10 elements, while i < 12 permits 12 reads. Replace the fixed limit with i < mass.Length.
How can I log a detached Task exception without async or await?
Wrap the reflection invocation in try/catch inside the task delegate and send the complete Exception object to the logger. Do not reduce it to e.Message.