2012-07-26 3 views
0

내 프로그램에 간단한 텍스트 상자가 있습니다. 기타 기능 : 사용자 textbox1의 다른 입력 및 버튼. 사용자가 textbox1에 값을 입력하고 버튼을 누르면 사용자에게 메시지를 확인하고 인쇄합니다. 내 문제는 내가 한 번에 하나씩 실시간으로 그 메시지를 보지 못한다는 것이다. 메시지는 끝에 표시됩니다. 데이터 바인딩을 정의하지 않았습니다. 간단하기 때문에 필요하지 않거나 틀렸다고 생각했기 때문입니다. 이것은 프로그램의 아주 작은 부분이며 버튼 클릭 이벤트 핸들러에 있습니다.텍스트 상자에 appendtext 만 wpf로 모두 끝에 표시

MainText.AppendText("Starting Refiling...\u2028"); 
foreach (DocumentData doc in Docs) 
{ 
    try 
    { 
     wsProxy.RefileDocument(doc); 
     MainText.AppendText(String.Format("Refilling doC# {0}.{1}\u2028", doc.DocNum, doc.DocVer)); 
    } 
    catch (Exception exc) 
    { 
     if (exc.Message.Contains("Document is in use") == true) 
      MainText.AppendText(String.Format("There was a problem refilling doC# {0}, it is in use.\u2028",doc.DocNum)); 
     else 
      MainText.AppendText(String.Format("There was a problem refilling doC# {0} : {1}.\u2028", doc.DocNum, exc.Message)); 
    } 
} 

답변

1

GUI 스레드에서 모든 루핑/인쇄를 수행하고 있습니다. 기본적으로 당신은 새로운 아이템을 보여주고 보여줄 시간을주지 않습니다. background worker을 만들고 게시 한 foreach 루프에서 작업을 수행하게하십시오. 이렇게하면 모든 변경 사항이있는 끝에 하나의 업데이트를 가져 오는 대신 UI 스레드가 텍스트가 변경 될 때 뷰를 업데이트 할 수 있습니다. 내가 게시 한 링크에는 backgroundworker 클래스를 사용하는 방법에 대한 예제가 포함되어 있지만 여기에 내가 할 일이 있습니다.

private readonly BackgroundWorker worker = new BackgroundWorker(); 

그를 초기화 :

public MainWindow() 
    { 
    InitializeComponent(); 

    worker.DoWork += worker_DoWork; 
    } 

그를 위해 작업을 만들기 :이 버튼을 눌러 이벤트를 얻을 때

void worker_DoWork(object sender, DoWorkEventArgs e) 
{ 
    // Set up a string to hold our data so we only need to use the dispatcher in one place 
    string toAppend = ""; 
    foreach (DocumentData doc in Docs) 
    { 
     toAppend = ""; 
     try 
     { 
     wsProxy.RefileDocument(doc); 
     toAppend = String.Format("Refilling doC# {0}.{1}\u2028", doc.DocNum, doc.DocVer); 
     } 
     catch (Exception exc) 
     { 
     if (exc.Message.Contains("Document is in use")) 
      toAppend = String.Format("There was a problem refilling doC# {0}, it is in use.\u2028",doc.DocNum); 
     else 
      toAppend = String.Format("There was a problem refilling doC# {0} : {1}.\u2028", doc.DocNum, exc.Message); 
     } 

     // Update the text from the main thread to avoid exceptions 
     Dispatcher.Invoke((Action)delegate 
     { 
     MainText.AppendText(toAppend); 
     }); 
    } 
} 

것은 그 시작

는 백그라운드 작업자 만들기

private void Button_Click(object sender, RoutedEventArgs e) 
    { 
    worker.RunWorkerAsync(); 
    } 
+0

안녕 스티브, 그게 다야! 그것에 대해 생각하지 않았지만, 배경 작업자가 내 문제를 해결했습니다. 고마워요. – dusm

+0

기꺼이 도와 드리겠습니다. – steveg89

관련 문제