在设计时在 VS(10) 中调试 C# .NET 自定义组件/控件

Debugging a C# .NET custom component / control in VS(10) during design time

提问人:7three 提问时间:11/15/2020 最后编辑:7three 更新时间:11/16/2020 访问量:299

问:

我看到了很多问题(在这样)和人们尝试使用调试组件的其他任何地方的主题(包括一开始的我自己)。这绝对是一个糟糕的方法。原因是,函数在设计时类似或不起作用。因此,您可以执行下一个最好的操作,以输出字符串。MesssageBox.ShowDebug.WriteConsole.Write

还有一点是,我以前从未真正研究过组件。当我编写程序时,我通常直接将其作为代码/类进行,并在运行时对其进行测试。

如果您对我写的内容感兴趣(或感到困惑或两者兼而有之),请随时纠正我,如果我错了和/或添加您的建议。

我的问题实际上应该是:“在设计时调试组件的最佳方法是什么?但是堆栈溢出不喜欢这样的问题。

或者只是下面的问题:“设计时调试/控制台输出问题”。

我首先尝试的:在设计时👎调试组件的 MS 文档指南

我使用与本问题中提到的相同的做法在设计时调试组件。

断点有效,但我仍然没有使用控制台/调试文本获得任何输出文本: debug line


正常运行时调试会话中,控制台和/或调试输出将传递到 VS 窗格。只是为了注意调试窗格输出任何内容。
VS Pane

当然,在这种情况下,您应该使用 Debug 而不是 Console。但是由于不起作用,我仍然尝试直接在控制台中输出一些东西。

经过一些更深入的测试,我得出的结论是 MS Docs 中的粒子是垃圾,我放弃了,因为:
Debug.WriteLine

  • 每次都花费太多时间和精力连接到其他会话。
  • 它杂乱无章,令人困惑
  • 它实际上不会执行与运行时相同的调试例程。
  • 未处理的错误会导致设计器停用组件,从而导致重启(两个)VS 会话。
  • 如前所述,我没有找到为什么我没有得到调试/控制台输出的线索。

    这进一步使我得出以下结论
  1. 对我来说,在关键点在控制台中输出一些东西基本上就足够了。非常烦人的是,我无法删除组件并再次插入它们,因为 Visual Studio 卡在内部的某个地方。这将是我接下来要解决的问题。

  2. 将 VS 进程附加到另一个 VS 实例绝对没有必要,而且在逻辑上是错误的。VS 的会话已经对组件执行此操作。并在 appdata 中生成一个临时 exe。

  3. 自定义或扩展错误处理程序(记录器)是个好主意。就像你做一个公共exe一样,其他人可以向你发送调试文本。在这种情况下,您可以将其发回给自己。;)

下一步:堆栈溢出和其他页面提供哪些选项?📌

有关堆栈溢出的相关主题

其他网站上的相关主题

我目前的目标 🏆

  • 远离 MS 文档的“解决方案”
  • 为设计人员设置一个简单的调试输出系统
C# WinForms Visual-Studio-2010 调试

评论

0赞 11/15/2020
提示:在调试设计器组件、控件、某些VSIX_时,请考虑更改项目的“启动操作”,而不是“附加到进程...”(这可能为时已晚)。请参阅“调试”>“启动外部程序”><项目>>属性,并指定 devenv.exe (Visual Studio) 的路径。在“命令行参数”中,输入包含组件的新最小测试平台解决方案项目的路径。理想情况下,它应该是一个不同的解决方案。现在,当你调试>启动调试时,Visual Studio 将启动并加载测试平台
1赞 11/15/2020
嗯......假设你的是 DEBUG 版本,xxx 应该可以工作。输出可能正在写入第二个 Visual Studio 输出窗口。如果失败,请使用 Windows 本机函数 OutputDebugString 并使用 MS 的 DebugViewDebug.Write
0赞 TnTinMn 11/15/2020
如果“MS 文档”是指演练:在设计时调试自定义 Windows 窗体控件,则此过程有效。我遇到的一个问题是将“DebuggingExample”项目包含在与“DebugControlLibrary”项目相同的解决方案中;我宁愿把它们分开。写入控制台不会写入 VS 输出窗口。但是,与您的断言相反,Debgug.WriteXXXX 方法将生成输出。
0赞 TnTinMn 11/15/2020
如果在“输出”窗口中看不到这些调用的结果,请确保未启用“将所有输出窗口文本重定向到即时窗口”选项。请考虑不要将 Console 方法用于调试输出,Debug/Trace 方法是生成此信息的方法。
0赞 7three 11/16/2020
Debug.XYZ 应该有效,是的。如前所述,我已经阅读了许多链接。我注意到许多其他人也同意 MS Docs 的调试方法一点也不好。即使程序基本有效。

答:

1赞 7three 11/15/2020 #1

到目前为止,我所做的是一个简单的消息系统,到目前为止似乎可以工作。我不想在设计器生成的窗口中输出调试内容。我知道,我可以创建一个表单窗口。或者更好的是,我可以创建一个其他应用程序来与设计师进行通信。目前,我对一个非常简单的解决方案感到满意。只是为了获得任何输出。

我做了一些有趣的经历,例如在设计时获取解决方案/项目文件夹。

这有点修补,但我对此很满意。因此,我有一个荒谬的想法,只需将文本内部输出到记事本窗口中即可。我没有“使用”命名空间来使其更加灵活和清晰地表达。还有一个 dispose 实现。我不确定这是否有意义。但我的想法是,如果在设计时解构类,可以避免持久线程。或者只是为了为案件做好准备。代码如下:

public class SendToNotepad : System.IDisposable
{
    private bool _disposed;

    public SendToNotepad() { }

    public static void Text(string text, bool d) { Send.SendText(text, d); }

    ~SendToNotepad() { Dispose(false); }

    public void Dispose()
    {
        Dispose(true);
        System.GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (_disposed) { return; }
        if (disposing)
        {
            // Dispose managed state
        }
        Send.Dispose();
        _disposed = true;
    }

    private static class Send
    {
        private const int STARTF_USESHOWWINDOW = 1;
        private const int SW_SHOWNOACTIVATE = 4;
        private const int SW_SHOWMINNOACTIVE = 7;

        private const int CREATE_NEW_CONSOLE = 0x000010;

        private const int EM_SETSEL = 0x00B1;
        private const int EM_REPLACESEL = 0x00C2;
        private const int WM_SETTEXT = 0x000C;
        private const int WM_GETTEXTLENGTH = 0x000E;

        private const int WM_COMMAND = 0x0111;
        private const int WM_APPCOMMAND = 0x0319;
        private const int WM_QUIT = 0x0012;

        private const int APPCOMMAND_SAVE = 0x201000;

        private const int SleepTime = 10;

        private static UnsafeNativeMethods.STARTUPINFO _si;
        private static UnsafeNativeMethods.PROCESS_INFORMATION _pi;

        private static System.Diagnostics.Process _p;
        private static System.Threading.Timer _timer;
        private static bool _isWaiting;
        private static string _waitingCatcher;
        private static string _debugFileName;
        private static readonly string Namespace = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Namespace;
        private static readonly string[] TitleLoopAniChars = new[] { "|", "/", "––", "\\" };
        private static readonly int TitleLoopAniCharsLength = TitleLoopAniChars.Length - 1;

        public static void Dispose()
        {
            if (_p != null)
            {
                try
                {
                    UnsafeNativeMethods.CloseHandle(_pi.hProcess);
                    UnsafeNativeMethods.CloseHandle(_pi.hThread);
                }
                catch { }
                _p.Dispose();
                _p = null;
            }
            if (_timer != null)
            {
                _timer.Dispose();
                _timer = null;
            }
        }

        public static void SendText(string text, bool d)
        {
            if (!d) { return; }
            text = System.DateTime.Now.TimeOfDay + ": " + text + System.Environment.NewLine;
            if (_isWaiting)
            {
                _waitingCatcher += text;
                return;
            }
            _isWaiting = true;
            int maxWait = 200; // Max timeout over all (* SleepTime)
            if (_p == null)
            {
                _debugFileName = GetDebugFileName();
                if (System.String.IsNullOrEmpty(_debugFileName))
                {
                    _waitingCatcher += text;
                    _isWaiting = false;
                    return;
                }
                if (!System.IO.File.Exists(_debugFileName))
                {
                    try { System.IO.File.Create(_debugFileName).Dispose(); }
                    catch { }
                }
                if (!System.IO.File.Exists(_debugFileName))
                {
                    _waitingCatcher += text;
                    _isWaiting = false;
                    return;
                }
                _si = new UnsafeNativeMethods.STARTUPINFO
                {
                    dwFlags = STARTF_USESHOWWINDOW,
                    wShowWindow = SW_SHOWMINNOACTIVE,
                    cb = System.Runtime.InteropServices.Marshal.SizeOf(_si)
                };
                bool success = UnsafeNativeMethods.CreateProcess(null, "notepad /W \"" + _debugFileName + "\"", System.IntPtr.Zero, System.IntPtr.Zero, true, 0, System.IntPtr.Zero, null, ref _si, out _pi);
                while (maxWait-- > 0 && success)
                {
                    System.Threading.Thread.Sleep(SleepTime);
                    try { _p = System.Diagnostics.Process.GetProcessById(_pi.dwProcessId); } // grab Process to handle WaitForExit()
                    catch { }
                    if (_p != null) { break; }
                }
                if (_p == null)
                {
                    _waitingCatcher += text;
                    _isWaiting = false;
                    return;
                }
                while (maxWait-- > 0 && (!_p.Responding || !_p.WaitForInputIdle())) { System.Threading.Thread.Sleep(SleepTime); }
                _timer = new System.Threading.Timer(NotifyOnProcessExits);
                _timer.Change(0, 0);
            }
            else
            {
                while (maxWait-- > 0 && (!_p.Responding || !_p.WaitForInputIdle())) { System.Threading.Thread.Sleep(SleepTime); }
            }

            System.IntPtr fwx = UnsafeNativeMethods.FindWindowEx(_p.MainWindowHandle, System.IntPtr.Zero, "Edit", null);
            UnsafeNativeMethods.SendMessage(fwx, EM_SETSEL, 0, -1);
            UnsafeNativeMethods.SendMessage(fwx, EM_SETSEL, -1, -1);
            UnsafeNativeMethods.SendMessageW(fwx, EM_REPLACESEL, 1, text);
            if (!System.String.IsNullOrEmpty(_waitingCatcher))
            {
                UnsafeNativeMethods.SendMessage(fwx, EM_SETSEL, 0, -1);
                UnsafeNativeMethods.SendMessage(fwx, EM_SETSEL, -1, -1);
                UnsafeNativeMethods.SendMessageW(fwx, EM_REPLACESEL, 1, _waitingCatcher);
                _waitingCatcher = "";
            }
            UnsafeNativeMethods.SendMessage(_p.MainWindowHandle, WM_COMMAND, 0x0003, 0x0); // first menu, item 3 (save)
            _isWaiting = false;
        }

        private static void NotifyOnProcessExits(object timer)
        {
            if (_p == null) { return; }
            // _p.WaitForExit();
            // This is just for fun as response feedback
            int i = 0;
            while (!_p.HasExited)
            {
                System.Threading.Thread.Sleep(500);
                UnsafeNativeMethods.SetWindowText(_p.MainWindowHandle, Namespace + "  –>  " + _debugFileName + "       " + TitleLoopAniChars[i]);
                i = i == TitleLoopAniCharsLength ? 0 : i + 1;
            }
            Dispose();
        }

        private static string GetDebugFileName() // Hack to get solution path while design time
        {
            string s;
            try
            {
                var trace = new System.Diagnostics.StackTrace(true);
                var frame = trace.GetFrame(0);
                s = System.IO.Path.GetDirectoryName(frame.GetFileName());
            }
            catch { return null; }
            return s == null ? null : s + "Debug.txt";
        }

        [System.Security.SuppressUnmanagedCodeSecurity]
        private static class UnsafeNativeMethods
        {
            [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
            public struct STARTUPINFO
            {
                public System.Int32 cb;
                public string lpReserved;
                public string lpDesktop;
                public string lpTitle;
                public System.Int32 dwX;
                public System.Int32 dwY;
                public System.Int32 dwXSize;
                public System.Int32 dwYSize;
                public System.Int32 dwXCountChars;
                public System.Int32 dwYCountChars;
                public System.Int32 dwFillAttribute;
                public System.Int32 dwFlags;
                public System.Int16 wShowWindow;
                public System.Int16 cbReserved2;
                public System.IntPtr lpReserved2;
                public System.IntPtr hStdInput;
                public System.IntPtr hStdOutput;
                public System.IntPtr hStdError;
            }

            [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
            internal struct PROCESS_INFORMATION
            {
                public System.IntPtr hProcess;
                public System.IntPtr hThread;
                public int dwProcessId;
                public int dwThreadId;
            }

            [System.Runtime.InteropServices.DllImport("kernel32.dll")]
            public static extern bool CreateProcess(
                string lpApplicationName,
                string lpCommandLine,
                System.IntPtr lpProcessAttributes,
                System.IntPtr lpThreadAttributes,
                bool bInheritHandles,
                uint dwCreationFlags,
                System.IntPtr lpEnvironment,
                string lpCurrentDirectory,
                [System.Runtime.InteropServices.In] ref STARTUPINFO lpStartupInfo,
                out PROCESS_INFORMATION lpProcessInformation
            );

            [System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)]
            [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
            public static extern bool CloseHandle(System.IntPtr hObject);

            [System.Runtime.InteropServices.DllImport("user32.dll")]
            public static extern int SendMessage(System.IntPtr hWnd, int uMsg, int wParam, int lParam);
            [System.Runtime.InteropServices.DllImport("user32.dll")]
            public static extern System.IntPtr SendMessage(System.IntPtr hWnd, int uMsg, System.IntPtr wParam, System.IntPtr lParam);
            [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
            public static extern int SendMessageW(System.IntPtr hWnd, int uMsg, int wParam, string lParam);

            [System.Runtime.InteropServices.DllImport("user32.dll")]
            public static extern System.IntPtr DefWindowProc(System.IntPtr hWnd, int msg, System.IntPtr wParam, System.IntPtr lParam);

            [System.Runtime.InteropServices.DllImport("user32.dll")]
            public static extern System.IntPtr FindWindowEx(System.IntPtr hwndParent, System.IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

            [System.Runtime.InteropServices.DllImport("user32.dll")]
            public static extern int SetWindowText(System.IntPtr hWnd, string text);
        }
    }
}

在组件类中:

private static readonly bool DesignTime = LicenseManager.UsageMode == LicenseUsageMode.Designtime;
SendToNotepad.Text("Debug text", DesignTime);

这有一个很好的(无例外)副作用,即如果 VS 崩溃,调试内容将被保留。Debug component

现在我有了一个更荒谬的想法,即挂钩到 vs 进程并将文本发回。我想我正在把自己挖得越来越深。;)

评论

1赞 11/15/2020
如果 xxx 不起作用,为什么不使用您最喜欢的日志记录库写入文件并使用像 BareTail 这样的工具。后者将实时显示文件的内容和任何更改,并根据需要滚动内容。Debug.Write
2赞 TnTinMn 11/15/2020
您实质上已经创建了 TraceListener 的功能。您可以考虑将其编写为适当的 TraceListener 类,以便它与普通的 Debug.Write 方法一起使用。
0赞 7three 11/16/2020
@MickyD 因为我没有事先想得足够多就把自己投入到某件事中。然后我完成了它。你说的就是我在学习过程中打算做的事情。考虑到您的意见和其他意见。
0赞 7three 11/16/2020
@TnTinMn我一定会这样做的。TraceListener 类看起来非常有前途。也许我甚至可以用它潜入组件。让我们看看结果如何。
1赞 11/16/2020
真诚。(我是给你投赞成票的人之一)。关于“我正在弄清楚 VS 到底是做什么的”,你可能想要查看 Visual Studio 扩展性 SDK