c# - 有没有办法知道 C# Windows 窗体应用程序启动期间发生了什么

标签 c# .net debugging

我有一个具有 Windows 窗体输出类型的 C# 应用程序。当我从 bin/Debug 或 bin/Release 文件夹启动它时它工作正常(我使用 Visual Studio Express 2008 来开发它)。但是,当我将该应用程序从 Release 文件夹复制到另一个文件夹时,然后启动它,发生了一些奇怪的事情。有时它加载的时间太长(在窗口显示之前),甚至在加载时,位于组框上的文本区域也不可见!

我不知道为什么会这样,但我想尝试看看在应用程序加载期间发生了什么。 有什么方法可以知道 C# Windows 窗体应用程序启动期间发生了什么?

更新: 我发现 TextArea 消失并且应用程序加载有一些延迟,因为我将控制台输出重定向到该文本区域:

public class TextBoxStreamWriter : TextWriter
{
    TextBox _output = null;
    delegate void SetTextCallback(char value);


    public TextBoxStreamWriter(TextBox output)
    {
        _output = output;
    }

    public override void Write(char value)
    {
        if (MainForm.closed)
            return;
        base.Write(value);
        if (_output.InvokeRequired)
        {
            SetTextCallback d = new SetTextCallback(Write);
            _output.Invoke(d, new object[] { value });
        }
        else
        {
            _output.AppendText(value.ToString());
        }

    }

    public override Encoding Encoding
    {
        get { return System.Text.Encoding.UTF8; }
    }
}

如果我评论这两行,它就有效了!:

//_writer = new TextBoxStreamWriter(logTextArea);
//Console.SetOut(_writer);

但我还是不明白为什么!

最佳答案

在抛出错误的那一行之前,放上这段代码:

System.Diagnostics.Debugger.Launch()

从出现异常的地方运行您的代码,当它遇到此行时,您将有机会将该进程附加到调试器。

关于c# - 有没有办法知道 C# Windows 窗体应用程序启动期间发生了什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8644022/

相关文章:

.net - 避免使用 C++/CLI 属性进行双重转换

performance - 随着时间的推移,GDB 变得越来越慢

debugging - IntelliJ/Android Studio 调试 : Breakpoint in callback does not get called

c# - 我应该如何处理反序列化的字符串实习?

c# - 错误 : "The Id field is required." while registering new user in database with AspNet. 身份 2.0

c# - C# 泛型类型中的 "Current Type"占位符?

c# - 为什么将图像写入流会产生与直接保存到文件不同的结果?

.net - ThreadStatic 与 ThreadLocal<T> 性能 : speedups or alternatives?

linux - 什么是“无法在00000000313337000处理内核分页请求”

C# 输出参数性能