How do I know that the IWin32Window was created when SetCompatibleTextRenderingDefault throws an InvalidOperationException?

After a major refactoring, I launched a WinForms application and immediately worked on Application.SetCompatibleTextRenderingDefault(false)

. In general, I understand what is causing this: IWin32Window

already created.

My problem is that for life I can't figure out how such an object (or objects?) Was created. The "where" this was generated as hopefully easier than the later question :) The line that throws literally the second line of my code: if I enter my application (starting with F10, not F5, to be clear) it goes through normal Application.EnableVisualStyles()

no problem, then it stops at the second line.

Is there a way to "inspect" the generated windows? I'm looking for any solution, lines of code, debugger functions, external programs, whatever. Thank.

PLEASE NOTE that it is obvious to me that I must have messed up something during my refactoring. I couldn't compile for three days, so most likely I created the error myself. I ask if there is a way to confidently find the offending object IWin32Window

.

UPDATE

I added the following code at the very beginning Program.Main()

:

        var thisProcess = System.Diagnostics.Process.GetCurrentProcess();
        if (thisProcess != null)
        {
            var mainWindow = thisProcess.MainWindowHandle;
        }

      

Well, mainWindow

- (pointer to) zero.

+3


source to share


1 answer


You need to look for the code that runs before the Main () method runs. There are not many candidates for such code, only a static constructor (aka type initializer) for the Program class. You would know if you wrote one and can set a breakpoint on it, the less obvious is the variable static

in the Program class with the field initializer. Not actually supported by the CLR, the C # compiler works around it, creating the static constructor itself and moving around the field initializer code.

If you know this is caused by the field initializer it won't help, you might consider setting a breakpoint on the appropriate struct method. This requires some understanding of how it works internally and there is no simple shortcut for that. Well, apart from the request in SO :)



First use Tools> Options> Debug> General and Disable My Code. This is not your code. Next, use Debug> New Breakpoint> Function Breakpoint> type "System.Windows.Forms.NativeWindow.AddWindowToTable". It is an internal method that maintains a reference to any window that is created, ensuring that the wrapper object of the .NET class does not get garbage collected too early.

Press F5, the Call Stack window points to the attacker.

+3


source







All Articles