Categories
Development

CallbackOnCollectedDelegate detected

After few minutes of successfull running I get this error in my application.

Asistente para la depuración administrada ‘CallbackOnCollectedDelegate’
Se realizó una devolución de llamada al delegado recolectado de tipo ‘Threads!Threads.Hud+NativeMethods+WinEventDelegate::Invoke’.

Since I cant figure out which part of the code is in responsible for this issue, I’m posting the complete class here. (I guess it has no issue with my other classes)

    private void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
    {
        Rect move = new Rect();

        if (eventType == EVENT_SYSTEM_MOVESIZESTART)
        {
            NativeMethods.GetWindowRect(hwnd, ref move);
            Formularios f = new Formularios();
            f.posicionformbynumjugador(NumJugador,JugadoresMesa);
            this.Left = move.Left + Convert.ToInt32(f.DataTable.Rows[0][0]);
            this.Top = move.Top + Convert.ToInt32(f.DataTable.Rows[0][1]);

            NativeMethods.SetWindowLong(this.Handle, -8 /*GWL_HWNDPARENT*/, hwnd);

            Debug.WriteLine("EVENT_SYSTEM_MOVESIZESTART");
        }
        else if (eventType == EVENT_SYSTEM_MOVESIZEEND)
        {
            NativeMethods.GetWindowRect(hwnd, ref move);
            Formularios f = new Formularios();
            f.posicionformbynumjugador(NumJugador, JugadoresMesa);
            this.Left = move.Left + Convert.ToInt32(f.DataTable.Rows[0][0]);
            this.Top = move.Top + Convert.ToInt32(f.DataTable.Rows[0][1]);

            NativeMethods.SetWindowLong(this.Handle, -8 /*GWL_HWNDPARENT*/, hwnd);

            Debug.WriteLine("EVENT_SYSTEM_MOVESIZEEND");
        }
        else if (eventType == EVENT_OBJECT_DESTROY)
        {
            if (IntPtr.Zero == NativeMethods.FindWindow(null, Tituloventana))
            {
                this.Close();
                Debug.WriteLine("EVENT_SYSTEM_MOVESIZEEND");
            }

        }


        Console.WriteLine(eventType.ToString());


    }


    static class NativeMethods
    {


        [DllImport("user32.dll")]
        public static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);

        public delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);

        [DllImport("user32.dll")]
        public static extern bool GetWindowRect(IntPtr hwnd, ref Rect rectangle);
        [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
        public static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
        [DllImport("user32.dll", EntryPoint = "FindWindowA")]
        public static extern IntPtr FindWindow(string sClass, string sWindow);

    }
    [DllImport("user32.dll", SetLastError = true)]
    static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

    IntPtr hwnd;

    uint processID = 0;


    public void car()
    {
        Rect move = new Rect();


        //this will also be triggered by mouse moving over the process windows


        hwnd = NativeMethods.FindWindow(null, Tituloventana);


        GetWindowThreadProcessId(hwnd, out processID);

        //NativeMethods.SetWinEventHook(EVENT_SYSTEM_MOVESIZESTART, EVENT_SYSTEM_MOVESIZEEND, IntPtr.Zero, WinEventProc, (uint)processID, (uint)0, WINEVENT_OUTOFCONTEXT);
        //NativeMethods.SetWinEventHook(EVENT_OBJECT_DESTROY, EVENT_OBJECT_LOCATIONCHANGE, IntPtr.Zero, WinEventProc, (uint)processID, (uint)0, WINEVENT_OUTOFCONTEXT);
         NativeMethods.SetWinEventHook(EVENT_SYSTEM_MOVESIZESTART, EVENT_OBJECT_DESTROY, IntPtr.Zero, WinEventProc, (uint)processID, (uint)0, WINEVENT_OUTOFCONTEXT);

        NativeMethods.GetWindowRect(hwnd, ref move);

        mover(move);


    }
    public delegate void posicion();
    public void mover(Rect move)
    {
        Formularios f = new Formularios();
        f.posicionformbynumjugador(NumJugador,JugadoresMesa);
            this.Left = move.Left + Convert.ToInt32(f.DataTable.Rows[0][0]);
            this.Top = move.Top + Convert.ToInt32(f.DataTable.Rows[0][1]);


    }



    private void Form2_Load_1(object sender, EventArgs e)
    {

        car();
    }




    public const int WM_NCLBUTTONDOWN = 0xA1;
    public const int HT_CAPTION = 0x2;

    [DllImportAttribute("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
    [DllImportAttribute("user32.dll")]
    public static extern bool ReleaseCapture();

    private void Hud_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            ReleaseCapture();
            SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            Formularios F = new Formularios();
            Rect move = new Rect();
            //hwnd = NativeMethods.FindWindow(null, Tituloventana);
            NativeMethods.GetWindowRect(hwnd, ref move);
            F.updateFormulario(NumJugador, (int)Hud.MousePosition.X - move.Left, (int)Hud.MousePosition.Y - move.Top, JugadoresMesa);
            //label2.Text = "x:" + Hud.MousePosition.X.ToString() + "y:" + Hud.MousePosition.Y.ToString();
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *