Page 1 of 1

Form Freezing

Posted: Thu Aug 04, 2011 4:58 pm
by Gunner1980
I am trying to launch a simple form with a few buttons on it which I made using designer. I am trying to show the form in a recordings user code using the following code and am having issues.

Code: Select all

             

public void AwaitingDataForm()
{   
CommonUtilLib.Forms.AwaitingDataForm ADform = new CommonUtilLib.Forms.AwaitingDataForm();
ADform.Show();
}

When I launch the form in this manner it shows up however only half of the data is present in the form and when I click on the form it says it is not responding and crashes. I have read around and thought that there may be a threading issue so I tried the following code but it gave me the same issue.

Code: Select all

        
public void NewThread()
        {
            Thread thread = new Thread(new ThreadStart(WorkThreadFunction));
            thread.Start();
        }
        
        public void WorkThreadFunction()
        {
            try
            {
                CommonUtilLib.Forms.AwaitingDataForm ADform = new CommonUtilLib.Forms.AwaitingDataForm();
                ADform.Show();
            }
            catch (Exception ex)
            {
                
            }   

Has anyone else launched a custom form from within user code? If so how do you do it?

Thanks in advance

Re: Form Freezing

Posted: Thu Aug 04, 2011 6:12 pm
by sdaly
Have you tried .ShowDialog instead of .Show?

Re: Form Freezing

Posted: Thu Aug 04, 2011 6:17 pm
by Support Team
Windows Forms controls need threads with an STA model, so make sure you set the apartment state of the thread:
thread.SetApartmentState(ApartmentState.STA);
Then you can try to use Application.Run to show the form:
System.Windows.Forms.Application.Run(ADform);
Regards,
Alex
Ranorex Team