【问题标题】:Why WinForm distroyed and demolished after working threadProc execution completes?为什么在工作 threadProc 执行完成后 WinForm 被破坏和拆除?
【发布时间】:2011-05-31 08:19:38
【问题描述】:

线程完成后我遇到WinForm破坏问题如下图,只需一段代码即可清除图片。

//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" 的句柄。当控件通过此函数(线程 proc GetControlsAsynch)的末尾时,它只会被拆除,并且主进程 GUI 仍然存在。这可能是什么原因?

注意:

“ControlsContainerMgt”是一个单独的类库,这个进程的实例保存在哪里 ControlsContainerMgt 是:

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

有没有办法解决这个问题?

【问题讨论】:

  • 会不会和跨线程操作有关?您是否尝试过检查控件中的 InvokeRequired 并酌情调用?
  • Elbaorat 呢?跨线程操作?我刚刚开发了一个包含 2 个函数的 samll dll,即 GetHandle(),它返回其类的句柄(IntPtr)和构造函数,主要是这个类只是继承“/Form”继承。所以,我只是从 Thread Proc 过程的底部调用它的 show 方法。就这样!!。往上看。该表单尚未显示在屏幕上,但是当控件传递到线程之外时,它就消失了并被拆除,即使我在类级别也可以处理它的表单。可能是什么原因?
  • 你想解决什么问题?
  • @Jonas :我的问题写得很清楚,实际上由其父进程创建的表单在刚刚调用其 show 方法的线程在调试模式下完成执行后没有显示。当线程完成时,其类在构造函数级别创建的 Form 甚至消失了,而父进程仍在运行
  • 这似乎更像是一个实现问题,而不是您要解决的实际问题。

标签: c# winforms .net-4.0


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多