user2635491
user2635491

Reputation: 51

Windows Form Won't Display in Debug Mode

I recently upgraded to VS 2012. I have a set of coded UI tests that I've coded in VS 2010 and I'm trying to spin them up in VS 2012. I have a windows form that I'm displaying at the beginning of the test run by using the AssemblyInitialize attribute. I use this form to allow users to select from sets of values and those values are used to data feed the tests. Here's a copy of my code that displays the form:

[AssemblyInitialize]
public static void AssemblyInitialize(TestContext context)
{
    ProcessUtility.TerminateAll();
    if (!File.Exists(Directory.GetCurrentDirectory() + @"\RunInfo.ser"))
    {
        InitializeForm initForm = new InitializeForm();
        initForm.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        initForm.ShowDialog();
    }
}

So, here's my headache: the form displays just fine in Run mode. However, if I try to spin it up in Debug mode, it never displays. I've stepped through the code. It's loading all of the controls for the form with no errors. I make it to the 'initForm.ShowDialog()' line of code. It runs that line of code but then nothing happens. I don't receive any errors and the status in the lower left of the IDE is 'Ready'. It's almost as if the IDE thinks the form is displayed but it's not. I've double-checked the Task Manager and it's just not there. I've verified the build configuration is set to Debug. I've tried cleaning the solution and re-building. This code continues to work in VS 2010. Please tell me someone out there has ran into a similar problem because I'm out of ideas. I'm new to stackoverflow so let me know if there is anything else I can provide to better explain the issue. Thank you in advance for taking a look at it.

Upvotes: 3

Views: 15226

Answers (4)

Jeffrey P
Jeffrey P

Reputation: 367

Not sure why this solution works, but I was able to solve this issue in VS2013 by setting the visible property on the form I was trying to display to true and then false before calling ShowDialog.

VB.Net example code

Dim form as Form = new Form
form.Visible = True
form.Visible = False
form.ShowDialog

Upvotes: 3

Ünsal Ersöz
Ünsal Ersöz

Reputation: 350

I was experiencing the same thing while debugging an old code and resolved the situation by adding [STAThread] attribute on top of container method which contains form.ShowDialog(); For example:

[STAThread]
public void MessageBoxShow(string errorMessage)
{
    using (frmError errorForm = new frmError(errorMessage))
    {
        errorForm.ShowDialog();
    }
}

This has solved any hanging occured while hitting-continuing debug point. Platform Windows 7 x64 enterprise edition and VS2008 (both has latest updates as of today).

Hope this helps.

Update 1: Please ignore using statement in example since I am using a custom form which inherits IDisposable in addition to Windows.Form and has custom disposition routines. Sorry if it has created any confusion.

Upvotes: 0

user2635491
user2635491

Reputation: 51

I was able to get the form to display using the following code instead of ShowDialog. I still have no idea why ShowDialog wasn't working but this does the trick:

InitializeForm initForm = new InitializeForm();
initForm.Visible = true;
initForm.Focus();
Application.Run(initForm);

Upvotes: 2

Scott Chamberlain
Scott Chamberlain

Reputation: 127543

Most likely a exception is happening during the initialization, Go in to the Debug->Exceptions dropdown menu and be sure the checkbox thrown for Common Language Runtime Exceptions is checked, this will let your code break on the exception that is happening.

enter image description here

If you are still not catching the exception go to Debug->Option and Settings then uncheck the box for Enable Just My Code and check the box for Break when exceptions cross AppDomain or managed/native boundries

enter image description here

This may give you some "read herring" exceptions, as some .NET processes use exceptions for control of flow logic. So just be aware that the first exception you see may not be the cause of your problem.

Upvotes: 1

Related Questions