2014-03-24 2 views
0

새로운 리본 탭 및 새 자식 컨트롤을 Grid에 추가하는 작업 응용 프로그램이 있습니다.UI 컨트롤 업데이트 wpf 백그라운드 스레드

나는 등 데이터베이스에서 데이터를 수집하는 동안,

을 취할 수있는 자식 컨트롤로 백그라운드 스레드에이 작업을 넣어 싶습니다 내가 가지고 지금까지 다음 코드를

Ribbon Ribbon_Main = new Ribbon(); 
Grid Grid_Main = new Grid(); 

Thread newthread2 = new Thread(new ThreadStart(delegate { Graphing_Template.add_report(); })); 
newthread2.SetApartmentState(ApartmentState.STA); //Is this required? 
newthread2.Start(); 


Class Graphing_Template() 
{ 
    static void add_report() 
    { 
    RibbonTab rt1 = new RibbonTab(); 
    MainWindow.Ribbon_Main.Items.Add(rt1); 
    // Create control with information from Database, etc. 
    // add control to MainWindow.Grid_Main 
    } 
} 

새 보고서 컨트롤을 백그라운드에서 만든 다음 준비가되면 기본 UI에 추가하고 싶습니다.

내가 갔다 솔루션은 다음과 같습니다 당신이 UI 스레드에서가 아니라면

일반적으로
 BackgroundWorker worker = new BackgroundWorker(); 
    worker.DoWork += delegate(object s, DoWorkEventArgs args) 
      { 
       DataTable dt1 = new DataTable(); 
       ---- Fill DataTable with 
       args.Result = datagrid_adventureworks_DT(); 
      }; 

worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args) 
      { 
       DataTable dt1 = (DataTable)args.Result; 
       Datagrid_Main.ItemsSource = dt1.AsDataView(); 
      }; 
+0

은 UI 객체를 만지지 마십시오. 백그라운드 스레드에서 제대로 작동하지만 UI 디스패처를 통해 UI 업데이트를 마샬링합니다. – Cameron

+0

두 개의 자식 개체를 생성하고 UI 스레드로 다시 전달할 수 있습니까? – user3329538

+0

확실한 답변을 드릴만큼 충분한 WPF를 모르겠습니다. 죄송합니다. Win32 UI 객체는 버튼을 생성하는 스레드 (예 : WPF는 버튼과 같은 단순한 것들에는 Win32 UI 객체를 사용하지 않기 때문에 Winform UI 객체는 UI 객체를 만져야하기 때문에 winforms에서는 작동하지 않을 것이라고 생각합니다. 기타.). – Cameron

답변

1
private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     Test4(); 

    } 
    private void Test1() 
    { 
     while (true) 
     { 
      this.Title = DateTime.Now.ToString(); 
      System.Threading.Thread.Sleep(5000); //locks up app 
     } 
    } 
    private void Test2() 
    { 
     var thd = new System.Threading.Thread(() => { 
      while (true) 
      { 
       this.Title = DateTime.Now.ToString(); //exception 
       System.Threading.Thread.Sleep(5000); 
      }    
     }); 
     thd.Start(); 
    } 

    private void Test3() 
    { //do the work on the background thread 
     var thd = new System.Threading.Thread(() => 
     { 
      while (true) 
      { //use dispatcher to manipulate the UI 
       this.Dispatcher.BeginInvoke((Action)(() 
        => { this.Title = DateTime.Now.ToString(); 
       })); 

       System.Threading.Thread.Sleep(5000); 

       //there's nothing to ever stop this thread! 
      } 
     }); 
     thd.Start(); 
    } 

    private async void Test4() 
    { //if you are using .Net 4.5 you can use the Async keyword 
     //I _think_ any computation in your async method runs on the UI thread, 
     //so don't use this for ray tracing, 
     //but for DB or network access your workstation can get on with 
     //other (UI) work whilst it's waiting 
     while (true) 
     { 
      await Task.Run(() => { System.Threading.Thread.Sleep(5000); }); 
      this.Title = DateTime.Now.ToString(); 
     } 
    } 
+0

Test3에 대한 추가 테스트가 필요합니다 ... 일부 문제가 있습니다. 나중에 명확히 할 것입니다. – user3329538

관련 문제