在c#中創建一個基於線程的應用程序,它從用戶選擇的計算機(實際上是遠程計算機)讀取一個文本文件。如果用戶對原始文件進行了任何更改,則該應用程序應該顯示修改後的文件(整體)。在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);
}
}
}
}
}
}
所以我應該創建另一個線程處理這個類? – PawanS 2010-12-21 12:31:12