Why WinForm distroyed and demolished after working threadProc execution completes?
I am facing a WinForm destroyal problem after the thread finishes as shown below, just a code will clear the picture.
//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<CustomCon开发者_JAVA技巧trol> 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();
}
}
I consumed and house-kept the handle of thread "GetControlsAsynchronously"
at the top of the constructor. As the control passes through the end of this function (thread proc GetControlsAsynch) it just gets demolished and the main process GUI is still there. What can be the reason for this?
Note :
"ControlsContainerMgt " is a separate class library whose instance this process keep where ControlsContainerMgt is :
public class ControlsContainerMgt : Form
{
public ControlsContainerMgt()
{
base.Text = "Tsdfsfd";
base.Name = "sads";
}
public IntPtr GetHandle()
{
return base.Handle;
}
}
Is there any way to solve this problem?
You should clone the control in your side otherwise the reference will represent the real one. In manage code handle passed as reference. Try to clone the control to have real instance (duplicate the control), otherwise you will have few references to one instance.
精彩评论