2011-05-13 9 views
1

요청되었으므로 필자는 오픈 소스 프로젝트 중 하나에 대한 작은 내부 로그 뷰어를 설치했습니다 (자체 설명 이미지). 그렇게하는 동안 새로 생성 된 윈도우의 HandleCreated 이벤트가 전혀 발생하지 않는 매우 이상한 문제가 발생했습니다. 이 이벤트를보고 컨트롤을 호출하기 전에 핸들이 만들어 졌는지 확인하고 있습니다.Form.HandleCreated 이벤트가 발생하지 않습니다.

NBug Internal Log Viewer

다음은 문제가있는 코드 (정적 생성자)입니다. 핸들을 생성하도록 강제하는 일반적인 트릭 (예 : var a = form.Handle;)이 전혀 도움이되지 않기 때문에 아무도이 문제를 해결할 방법이 없습니까?

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 라이브러리에서 가져옵니다.

+4

아마도 다음과 관련이 있습니다. http://blogs.msdn.com/b/pfxteam/archive/2011/05/03/10159682.aspx – Dmitry

+0

오, 정적 인 생성자의 내용을 별도의 함수로 이동하고 초기화가 실제로 작동하지 않습니다. 나는 정직하게 그런 교착 상태를 상상할 수 없었다! –

+0

@Dmitry : 솔루션을 찾은 것처럼 보이므로 아래의 "답변"으로 게시하여 독자가 좋은 답변을 알 수 있도록하십시오. –

답변

관련 문제