System.Reflection.TargetInvocationException导致问题无处不在

时间:2015-01-01 20:33:11

标签: c#

突然间,我没有对代码进行任何更改,而是获得了System.Reflection.TargetInvocationException。跟踪如下:

  

at System.RuntimeTypeHandle.CreateInstance(RuntimeType type,Boolean publicOnly,Boolean noCheck,Boolean& canBeCached,RuntimeMethodHandleInternal& ctor,Boolean& bNeedSecurityCheck)      在System.RuntimeType.CreateInstanceSlow(Boolean publicOnly,Boolean skipCheckThis,Boolean fillCache,StackCrawlMark& stackMark)

我的入口点的代码(抛出异常的内容)如下:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using DS4Control;
using System.Threading;
using System.Runtime.InteropServices;

namespace DS4Windows
{
    public class EntryPoint
    {
        private const int SW_SHOWNORMAL = 1;
        private const int SW_SHOW = 5;
        private const int SW_SHOWMAXIMIZED = 3;
        private const int SW_RESTORE = 9;
        const uint WM_SHOWWINDOW = 0x0018;
        const int SW_PARENTOPENING = 3;

        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);


        [STAThread]
        public static void Main(string[] args) {

            bool createdNew = true;
            using (Mutex mutex = new Mutex(true, "DS4Windows", out createdNew))
            {
                if (createdNew)
                {
                    App.Main();
                }
                else
                {
                    IntPtr HWND = FindWindow(null, "DS4Windows DSDCS Build");
                    ShowWindow(HWND, 5);
                    SendMessage(HWND, WM_SHOWWINDOW, IntPtr.Zero, new IntPtr(SW_PARENTOPENING));
                }
            }
        }
    }


    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public App()
        {
            Global.SetupLogging();
        }
        private void HandleDispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            string CrashString1 = e.Exception.Message + " (" + e.Exception.TargetSite + " in " + e.Exception.Source + ") [" + e.Exception.InnerException + "]" + Environment.NewLine + e.Exception.StackTrace;
            string CrashString2 = Environment.NewLine + Environment.NewLine + string.Join(Environment.NewLine, Global.DebugLog);
            Trace.TraceError(CrashString1);

            try
            {
                System.IO.StreamWriter file = new System.IO.StreamWriter(System.IO.Path.Combine(Global.appdatapath, "Crash.log"));
                file.WriteLine(CrashString1 + CrashString2);
                file.Close();
            }
            catch { }
            e.Handled = true;
        }

    }
}

在我的互斥检查中调用App.Main()时会抛出异常。

我知道这段代码的工作时间最长,

0 个答案:

没有答案