首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Form.HandleCreated事件从不发生

Form.HandleCreated事件从不发生
EN

Stack Overflow用户
提问于 2011-05-13 20:55:47
回答 1查看 1.6K关注 0票数 1

按照要求,我为我的一个开源项目提供了一个小型的内部日志查看器(如下图所示)。在这样做的时候,我遇到了一个非常奇怪的问题,新创建的窗口的HandleCreated事件根本不会发生。我正在观察这个事件,以确保在调用任何控件之前创建了句柄。

下面是有问题的代码(静态构造函数)。有没有人有办法解决这个问题,因为所有强制创建句柄的常用技巧(比如var a= form.Handle;)都没有任何帮助。

代码语言:javascript
复制
internal partial class InternalLogViewer : Form
{
    static InternalLogViewer()
    {
        viewer = new InternalLogViewer();
        formShown = new ManualResetEvent(false);
        viewer.HandleCreated += (sender, e) => formShown.Set();
        Task.Factory.StartNew(() => viewer.ShowDialog());
        formShown.WaitOne(); // ToDo: This needs a workaround as it waits for an eternity
    }

    private static InternalLogViewer viewer;
    private static ManualResetEvent formShown;

    public static void LogEntry(string message, LoggerCategory category)
    {
        viewer.Invoke((MethodInvoker)delegate
        {
            viewer.InternalLogEntry(message, category);
        });
    }

    internal InternalLogViewer()
    {
        InitializeComponent();
        this.Icon = Properties.Resources.NBug_icon_16;
        this.notifyIcon.Icon = Properties.Resources.NBug_icon_16;
    }

    internal void InternalLogEntry(string message, LoggerCategory category)
    {
        this.loggerListView.Items.Add(new ListViewItem(new[] { category.ToString().Remove(0, 4), DateTime.Now.ToString("HH:mm:ss"), message }));
    }
}

编辑:代码取自NBug库。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-05-14 16:25:24

该问题与static constructor deadlocks有关。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5992150

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档