[英]TargetInvocationException was unhandled - c#
我真的無法弄清楚發生了什么以及它為什么會一直發生
調用的目標拋出了異常。
代碼在frm5中運行
然后錯誤顯示在
Program.cs中:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace BlueStitch
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMain());
}
}
}
它在Application.Run(new frmMain());
它說TargetInvocationException was unhandled - Exception has been thrown by the target of an invocation.
這個例外
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: Parameter is not valid.
at System.Drawing.Image.get_FrameDimensionsList()
at System.Drawing.ImageAnimator.CanAnimate(Image image)
at System.Drawing.ImageAnimator.ImageInfo..ctor(Image image)
at System.Drawing.ImageAnimator.Animate(Image image, EventHandler onFrameChangedHandler)
at System.Windows.Forms.PictureBox.Animate(Boolean animate)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at BlueStitch.frmStitch.backgroundWorker1_RunWorkerCompleted(Object sender, RunWorkerCompletedEventArgs e) in C:\Users\Freddie Rosillo\Documents\Visual Studio 2008\Projects\BlueStitch\BlueStitch\BlueStitch\frmStitch.cs:line 976
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at BlueStitch.Program.Main() in C:\Users\Freddie Rosillo\Documents\Visual Studio 2008\Projects\BlueStitch\BlueStitch\BlueStitch\Program.cs:line 21
您正在執行frmMain構造函數中的一些代碼,這些代碼不應該在那里執行。 移動該代碼並在Form Shown事件上執行它。
我不是圖形人,但失敗的線:
System.Drawing.Image.get_FrameDimensionsList()
進行一些GDI +調用並且似乎想要圖像的FrameDimensionsCount的某些值...它是你正在處理的GIF動畫嗎? 還是TIFF?
查看GdipImageGetFrameDimensionsCount的文檔,看看是否有任何樹枝。 下面的反編譯失敗的getter:
[Browsable(false)]
public Guid[] FrameDimensionsList
{
get
{
int count;
int frameDimensionsCount = SafeNativeMethods.Gdip.GdipImageGetFrameDimensionsCount(new HandleRef((object) this, this.nativeImage), out count);
if (frameDimensionsCount != 0)
throw SafeNativeMethods.Gdip.StatusException(frameDimensionsCount);
if (count <= 0)
return new Guid[0];
int num1 = Marshal.SizeOf(typeof (Guid));
IntPtr num2 = Marshal.AllocHGlobal(num1 * count);
if (num2 == IntPtr.Zero)
throw SafeNativeMethods.Gdip.StatusException(3);
int frameDimensionsList = SafeNativeMethods.Gdip.GdipImageGetFrameDimensionsList(new HandleRef((object) this, this.nativeImage), num2, count);
if (frameDimensionsList != 0)
{
Marshal.FreeHGlobal(num2);
throw SafeNativeMethods.Gdip.StatusException(frameDimensionsList);
}
else
{
Guid[] guidArray = new Guid[count];
try
{
for (int index = 0; index < count; ++index)
guidArray[index] = (Guid) System.Drawing.UnsafeNativeMethods.PtrToStructure((IntPtr) ((long) num2 + (long) (num1 * index)), typeof (Guid));
}
finally
{
Marshal.FreeHGlobal(num2);
}
return guidArray;
}
}
}
當您嘗試從frmStitch.backgroundWorker1_RunWorkerCompleted方法中獲取FrameDimensionsList屬性時,它無法找到該屬性,因為您在BackgroundWorker線程之外。 在BackgroundWorker線程中完成工作后,RunWorkerCompleted將在原始線程中執行代碼。
我們通常使用BackgroundWorker.ReportProgress方法從BGW線程到主線程進行通信。 為了反過來傳達(這似乎是你想要做的事情),我會考慮從BGW線程訂閱主線程中的事件,以便BGW線程可以基於某些東西更新相關屬性你是在主線程中做的。
.NET中的每個異常都將落在Application.Run(new frmMain());
因為此方法運行消息循環並且除非窗口關閉,否則它將保持活動狀態。
但是,您的異常是在GDI +上播放圖像時總是拋出的常見異常,並且GDI已知內存泄漏導致此錯誤,最終由於沒有更多內存可用錯誤。
一旦我看到你正在使用圖像做什么,我就可以寫更多關於你的錯誤的信息,很可能你的圖像所用的記憶沒有被釋放導致這個問題。 在WPF中解決了這些帶有圖像的內存問題,在WPF上切換到圖像操作不會導致這些錯誤。 您仍然可以在.NET Forms中使用WPF來處理圖像而不會導致內存錯誤,並根據需要使用Windows窗體。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.