创建句柄C#时出错

时间:2014-01-23 22:38:37

标签: c# windows winforms forms handle

当我去调用我的加载器表单的新实例时,我的程序出现了这个令人讨厌的错误。

此表单的功能是从前一个表单中获取文件列表,并允许根据按下的热键加载每个文件。

我无法理解为什么我得到Handler异常,因为处理程序在private System.ComponentModel.IContainer components = null;文件中分配到.Designer.cs。之后它会引发异常。我已经做了很多在线搜索,我倾向于提出的关于这个例外的内存问题。我没有任何证据表明这一点;我很难过。

这是类代码:

   [DllImport("user32.dll")]
    private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
   [DllImport("user32.dll")]
    private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

    private BackgroundWorker documentHandler;
    private readonly List<string> files;

    private bool wait;
    private readonly string fileDirectory;
    private string previousFile;
    private string currentFile;
    private string nextFile;
    private int position;

    //Form Constructor
    public FileLoader(string directory, string[] filesToLoad)
    {
        this.InitializeComponent();

        files = new List<string>();
        this.files.AddRange(filesToLoad);
        this.fileDirectory = directory;
    }

    //WorkerSetup
    public void WorkerSetup()
    {
        documentHandler = new BackgroundWorker();

        documentHandler.DoWork += documentHandler_DoWork;
        documentHandler.RunWorkerCompleted += documentHandler_RunWorkerCompleted;

        this.HotKeyManager(true);
        this.FileManager(null);

        documentHandler.RunWorkerAsync(files[0]);
    }

    //ActionMethods
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x0312)
        {
            int id = m.WParam.ToInt32();

            if (id == 1)
            {
                this.FileManager(false);
                wait = false;
            }
            if (id == 2)
            {
                this.FileManager(true);
                wait = false;
            }
            if (id == 3)
            {
                documentHandler.CancelAsync();
            }
        }

        this.PreProcessMessage(ref m);
    }
    private void HotKeyManager(bool register)
    {
        var regUnreg = register ? 1 : 2;

        switch (regUnreg)
        {
            case 1:
                RegisterHotKey(this.Handle, 1, 0x0004, 0x25); //Left Key
                RegisterHotKey(this.Handle, 2, 0x0004, 0x27); //Right Key
                RegisterHotKey(this.Handle, 3, 0x0004, 0x1B); //Esc Key
                break;
            case 2:
                UnregisterHotKey(this.Handle, 1);
                UnregisterHotKey(this.Handle, 2);
                UnregisterHotKey(this.Handle, 3);
                break;
        }
    }
    private void FilePosition()
    {
        if (position <= 0)
        {
            this.position = 0;
            this.previousFile = files[position];
            this.currentFile = files[position];
            this.nextFile = files[position + 1];
        }
        else if (position == files.Count)
        {
            this.previousFile = files[position - 1];
            this.currentFile = files[position];
            this.nextFile = files[position];

            documentHandler.CancelAsync();
        }
        else
        {
            this.previousFile = files[position - 1];
            this.currentFile = files[position];
            this.nextFile = files[position + 1];
        }

    }
    private void SetupLabels()
    {
        labelPrevious.Text = string.IsNullOrEmpty(this.previousFile) ? "On First" : previousFile;
        labelCurrent.Text = string.IsNullOrEmpty(this.currentFile) ? "Error" : previousFile;
        labelNext.Text = string.IsNullOrEmpty(this.nextFile) ? "On Last" : previousFile;
    }

    //Controller
    private void FileManager(bool? increment)
    {
        if (increment == true)
        {
            position++;
        }
        else if (increment == false)
        {
            position--;
        }

        this.FilePosition();
        this.SetupLabels();
    }

    //Workers
    private void documentHandler_DoWork(object sender, DoWorkEventArgs e)
    {
        var adobeProcessInfo = new ProcessStartInfo("Acrobat.exe", this.fileDirectory +   "\\" + currentFile + ".pdf");
        var adobeProcess = new Process { StartInfo = adobeProcessInfo };

        adobeProcess.Start();

        while (wait)
        this.SetupLabels();
    }
    private void documentHandler_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            MessageBox.Show(e.Error.Message, "Error");
        }

        this.HotKeyManager(false);
        this.Close();
    }

这里是堆栈输出:

  

PDFInformationExtraction.exe!PDFInformationExtraction.FileLoader.HotKeyManager(bool register)第85行C#       PDFInformationExtraction.exe!PDFInformationExtraction.FileLoader.WorkerSetup()第47行+ 0xd字节C#       PDFInformationExtraction.exe!PDFInformationExtraction.FormExtract.ButtonLoaderClick(object sender,System.EventArgs e)第122行+ 0xa字节C#       System.Windows.Forms.dll!System.Windows.Forms.Control.OnClick(System.EventArgs e)+ 0x7f bytes
      System.Windows.Forms.dll!System.Windows.Forms.Button.OnClick(System.EventArgs e)+ 0xa2 bytes
      System.Windows.Forms.dll!System.Windows.Forms.Button.OnMouseUp(System.Windows.Forms.MouseEventArgs mevent)+ 0xac bytes       System.Windows.Forms.dll!System.Windows.Forms.Control.WmMouseUp(ref System.Windows.Forms.Message m,System.Windows.Forms.MouseButtons button,int clicks)+ 0x2d1 bytes       System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m)+ 0x93a bytes       System.Windows.Forms.dll!System.Windows.Forms.ButtonBase.WndProc(ref System.Windows.Forms.Message m)+ 0x127 bytes
      System.Windows.Forms.dll!System.Windows.Forms.Button.WndProc(ref System.Windows.Forms.Message m)+ 0x20 bytes
      System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m)+ 0x13 bytes
      System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m)+ 0x31 bytes
      System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd,int msg,System.IntPtr wparam,System.IntPtr lparam)+ 0x64 bytes       [原产于管理过渡]
      [管理到原生过渡]
      System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(System.IntPtr dwComponentID,int reason,int pvLoopData)+ 0x287 bytes
      System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason,System.Windows.Forms.ApplicationContext context)+ 0x16c bytes
      System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason,System.Windows.Forms.ApplicationContext context)+ 0x61 bytes
      System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.Form mainForm)+ 0x31 bytes
      PDFInformationExtraction.exe!PDFInformationExtraction.Program.Main()第18行+ 0x1d字节C#       [原产于管理过渡]
      [管理到原生过渡]
      mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile,System.Security.Policy.Evidence assemblySecurity,string [] args)+ 0x6d bytes
      Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()+ 0x2a bytes
      mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(对象状态)+ 0x63字节
      mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback callback,object state,bool ignoreSyncCtx)+ 0xb0 bytes
      mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback callback,object state)+ 0x2c bytes
      mscorlib.dll!System.Threading.ThreadHelper.ThreadStart()+ 0x44 bytes
      [原生于管理过渡]

修改 结果我的方法来监听热键格式错误:

  • this.PreProcessMessage(ref m);应该是base.WndProc(ref m);

0 个答案:

没有答案