2012-03-13 3 views
0

일부 데이터를 얻기 위해 스레드를 시작하는 C# 폼 응용 프로그램을 실행 중입니다. 이 스레드는 내부에 이벤트가 있습니다. 즉, 이벤트가 스레드에서 실행되고 동일한 스레드에 의해 캡처되어야합니다. 그러나 스레드의 이벤트가 발생하지 않는 것 같습니다. 모든 단서?백그라운드에서 실행 된 이벤트가 무시 됨

void PointCreated(object sender, IdEventArgs e) // a certain event that should fire and it doesn't 
     { 
      Console.WriteLine("Event Fired!");    
     } 



public void onlinerun() 
{ 
    Console.WriteLine("run started"); // this is printed on console 
    while (true) 
    { 
     do_some_work(); 
     //this work could result in the PointCreated event firing 
    } 
} 
+1

WinForm 응용 프로그램에는 콘솔이 없습니다. 대신 Debug.WriteLine을 사용하여 VS2010 출력 창으로 이동하십시오. –

+1

실제로 이벤트 이외의 것을 인쇄하고 있는데, 프로젝트 유형을 콘솔 응용 프로그램으로 선택했지만, 프로그램에서 나는 STAThread에 창을 실행했습니다. –

+0

어떻게 이벤트를 실행하고 있습니까? –

답변

0

이 같은 시도 보내기 : 스레드 내부

private void btnPlay_Click(object sender, EventArgs e) 
    { 
     Thread thread = new Thread(kinect.onlineRun); 
     thread.IsBackground = true; 
     thread.Start(); 
    } 

당신의 호출 클래스가 컨트롤러라고하며 대리인이 ControlEventHandler라고 가정

을 ...

private void PointCreated(object sender, IdEventArgs e) 
{ 
    // Ensure the event was received in the calling thread 
    if (this.InvokeRequired) 
    { 
     if (e != null) 
     { 
      // We aren't in the correct thread so pass on the event 
      this.BeginInvoke(new Controller.ControllerEventHandler(this.PointCreated), new object[] { sender, e }); 
     } 
    } 
    else 
    { 
     lock (this) 
     { 
      Console.WriteLine("Event Fired!"); 

      // TODO: Do some stuff here 
     } 
    } 
} 
관련 문제