为什么WinForm在working threadProc执行完成后被破坏

本文关键字:执行 WinForm working threadProc 为什么 | 更新日期: 2023-09-27 17:57:57

线程完成后,我面临一个WinForm destroyal问题,如下所示,只需一个代码即可清除图片。

//Constructor
=============
private HookCoreMangerClass()//ref ApplicationInfo mCurrentAttachedApplicationInfo)
{
    m_ContainerHandler = new ControlsContainerMgt();            
    m_hThreadAsyncControls = new Thread(this.GetControlsAsynchronously);
    m_hThreadAsyncControls.Start();
    ...
    ...
}
public void GetControlsAsynchronously()
{
    Thread.CurrentThread.Suspend();
    List<CustomControl> Objects = null;
    IntPtr ContainerHandle = IntPtr.Zero;
    if (m_IHooker != null)
    {
        m_IHooker.GetControlsHandles(m_processHandle, out Objects);
        List<ITAKOControl> ListOfControls = new List<ITAKOControl>();
        foreach (CustomControl customControl in Objects)
        {
            ITAKOControl TakoControlHandler = factry.CreateControl(customControl);                    
            TakoControlHandler.setPropeties(customControl);
            ContainerHandle = m_ContainerHandler.GetHandle();
            TakoControlHandler.SetContainer(ContainerHandle);
            ListOfControls.Add(TakoControlHandler);                    
        }
        m_ParentForm = Control.FromHandle(ContainerHandle);
        m_ParentForm.Show();
        m_ParentForm.Refresh();
        m_ParentForm.Update();
    }
}

我使用并将线程"GetControlsAsynchronously"的句柄保留在构造函数的顶部。当控件通过这个函数的末尾(线程进程GetControlsAsynch)时,它就被拆除了,主进程GUI仍然在那里。这可能是什么原因?

注:

"ControlsContainerMgt"是一个单独的类库,此进程将其实例保存在ControlsContainerMgt是:

public class ControlsContainerMgt : Form
{                
    public ControlsContainerMgt()
    {
        base.Text = "Tsdfsfd";
        base.Name = "sads";
    }
    public IntPtr GetHandle()
    {
        return base.Handle;
    }
}

有什么办法解决这个问题吗?

为什么WinForm在working threadProc执行完成后被破坏

您应该克隆您一侧的控件,否则引用将代表真实的控件。在管理代码中,句柄作为引用传递。尝试克隆控件以具有实际实例(复制控件),否则您将很少引用一个实例。