在Windows应用程序中执行任务时的ProgressBar [英] ProgressBar when performing a task in windows application
本文介绍了在Windows应用程序中执行任务时的ProgressBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Windows应用程序中执行任务时要执行的ProgressBar操作
我有浏览按钮,RichtextBox&表单中的进度条[openfiledialog,backgroundworker]。
任务
第一个线程读取目录文本文件并放入多行文本框
另一个线程显示进度条根据目录中的文件数量工作。
ProgressBar action to be done when performing a task in windows application
i have Browse Button ,RichtextBox & Progressbar [openfiledialog,backgroundworker] in the form.
Task
First Thread reads a directory text files and put in multiline text box
another thread to show progress bar works based on the number of files in the directory.
private void btnBrowse_Click(object sender, EventArgs e)
{
openFileandRead();
}
public void openFileandRead()
{
Stream myStream = null;
OpenFileDialog Dialog = new OpenFileDialog();
Dialog.Title = "Open Text File";
Dialog.Filter = "TXT files|*.txt";
Dialog.Multiselect = true; //allows to select multiple files in openfiledialog
if (Dialog.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = Dialog.OpenFile()) != null)
{
using (myStream)
{
string[] filePaths = Directory.GetFiles(@"D:\TestTxtFiles", "*.txt", SearchOption.AllDirectories);
foreach (string file in filePaths)
{
rtxtFile.AppendText(System.IO.File.ReadAllText(file));
Thread.Sleep(2000);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
在此先感谢
[Agent_Spock]
- 添加代码括号
Thanks in Advance
[Agent_Spock]
- Added code brackets
推荐答案
http://msdn.microsoft.com/en-us/library/system。 windows.forms.progressbar(v = vs.110).aspx [ ^ ]。
我有浏览按钮,RichtextBox&表单中的Progressbar [openfiledialog,backgroundworker]。
任务
First Thread读取目录文本文件并放入多行文本框
另一个显示进度条的线程根据目录中的文件数量工作。
private void btnBrowse_Click(对象发送者,EventArgs e)
{
openFileandRead();
}
public void openFileandRead()< br $>
{
流myStream = null;
OpenFileDialog Dialog = new OpenFileDialog();
Dialog.Title =打开文本文件;
Dialog.Filter =TXT文件| * .txt;
Dialog.Multiselect = true; //允许在openfiledialog中选择多个文件
if(Dialog.ShowDialog()== DialogResult.OK)
{
尝试
{
if((myStream = Dialog.OpenFile())!= null)
{
使用(myStream)
{
string [] filePaths = Directory.GetFiles(@D:\ TestTxtFiles ,* .txt,SearchOption.AllDirectories);
foreach(filePaths中的字符串文件)
{
rtxtFile.AppendText(System .IO.File.ReadAllText(file));
Thread.Sleep(2000);
}
}
$
}
catch(Exception ex)
{
MessageBox.Show(错误:无法从磁盘读取文件。原始错误:+ ex.Message);
}
}
}
i have Browse Button ,RichtextBox & Progressbar [openfiledialog,backgroundworker] in the form.
Task
First Thread reads a directory text files and put in multiline text box
another thread to show progress bar works based on the number of files in the directory.
private void btnBrowse_Click(object sender, EventArgs e)
{
openFileandRead();
}
public void openFileandRead()
{
Stream myStream = null;
OpenFileDialog Dialog = new OpenFileDialog();
Dialog.Title = "Open Text File";
Dialog.Filter = "TXT files|*.txt";
Dialog.Multiselect = true; //allows to select multiple files in openfiledialog
if (Dialog.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = Dialog.OpenFile()) != null)
{
using (myStream)
{
string[] filePaths = Directory.GetFiles(@"D:\TestTxtFiles", "*.txt", SearchOption.AllDirectories);
foreach (string file in filePaths)
{
rtxtFile.AppendText(System.IO.File.ReadAllText(file));
Thread.Sleep(2000);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
这篇关于在Windows应用程序中执行任务时的ProgressBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文