在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