2010-12-21 8 views
0

スレッドでアプリケーションを作成すると、ユーザーが選択したコンピュータ(実際にはリモートコンピュータ)からテキストファイルが読み込まれます。ユーザーが元のファイルを変更した場合、このアプリケーションは変更されたファイル(全体)を表示する必要があります。C#の自動ファイルアップデータ?

成功していますが、私が使っているスレッドは、どこからどのように配置するのかわからず、バックグラウンドから元のファイルを連続的に読み取っています。私のアプリケーションは、このコード

public partial class FormFileUpdate : Form 
{ 
    // This delegate enables asynchronous calls for setting the text property on a richTextBox control. 
    delegate void UpdateTextCallback(object text); 

    // This thread is used to demonstrate both thread-safe and unsafe ways to call a Windows Forms control. 
    private Thread autoReadThread = null; 


    public FormFileUpdate() 
    { 
     InitializeComponent(); 

     //Creating Thread 
     this.autoReadThread = new Thread(new ParameterizedThreadStart(UpdateText)); 
    }  

    private void openToolStripButton_Click(object sender, EventArgs e) 
    { 
     OpenFileDialog fileOpen = new OpenFileDialog(); 
     fileOpen.Title = "Select a text document"; 
     fileOpen.Filter = @"Text File|*.txt|Word Document|*.rtf|MS office Documnet|*.doc|See All files|*.*"; 
     fileOpen.FilterIndex = 1; 
     fileOpen.RestoreDirectory = true; 
     fileOpen.Multiselect = false; 
     if (fileOpen.ShowDialog() == DialogResult.OK) 
     {   
      //Created Thread will start here 
      this.autoReadThread.Start(fileOpen.FileName); 
     } 
    } 

    private void UpdateText(object fileName) 
    {  
     StreamReader readerStream = null; 
     while(true) 
     { 
      if (this.richTextBox1.InvokeRequired) 
      { 
       UpdateTextCallback back = new UpdateTextCallback(UpdateText); 
       this.Invoke(back, new object[] { fileName }); 
      } 
      else 
      {  
       try 
       { 
        string fileToUpdate = (string)fileName; 
        readerStream = new StreamReader(fileToUpdate); 
        richTextBox1.Text = readerStream.ReadToEnd(); 
       }  
       catch (Exception ex) { MessageBox.Show(ex.Message); }  
       finally 
       { 
        if (readerStream != null) 
        { 
         readerStream.Close(); 
         Thread.Sleep(100); 
        } 
       } 
      } 
     } 
    } 
} 

答えて

0

追加注:GUIスレッドからThread.Sleepを呼び出している

!なぜあなたはファイルを閉じた後に遅れを取る必要がありますか?なんらかの理由でこの遅延が必要な場合(たとえば、ファイルをあまり頻繁に読み込まないようにする場合)、GUIスレッドにこの遅延を設定しないでください。これは、アプリケーションが応答しなくなるためです。

EDIT:コメント

であなたの質問に答えることができきれいな方法がBackgroundWorker x秒ごとに呼び出しますTimer設定することです。

BackgroundWorkerは、バックグラウンドスレッドでコードを実行し、作業が完了したときにGUIスレッドでコールバックを実行するのが非常に簡単です。そして、あなたはInvokeInvokeRequiredを直接扱う必要はありません。

さらに、私はBackgroundWorkerをラップするクラスを作成して、ファイルを読み込む操作(バックグラウンドスレッド)からUIを更新する(GUIスレッド内)までデータを簡単に渡すことができます。

+0

これを扱うスレッドで別のクラスを作成する必要がありますか? – PawanS

1
  1. あなたは時間がかかるアクションであるGUIスレッドから読み込むファイルを作っているとハングアップを取得します。 begin invokeを使用してGUIの更新のみを行う必要があります。
  2. while(true)を使用しないでください。FileSystemWatcherクラスを使用すると、ファイルが変更されたときにのみ表示を更新できます(here)。イタイが書いたものに
+0

ファイル読み取りのスレッドはどこに作成する必要がありますか?私がFileSystemWatchを使用していると思うので、スレッド化を実装する必要はありません – PawanS

+1

'FileSystemWatcher'の' Changed'イベントを使ってファイルの変更を監視し、イベントが発生したときにファイルを読み込みます。 –

+0

@Pawan:実際にスレッドは必要ありません。 –