C#在执行函数时填充进度条

C#在执行函数时填充进度条,c#,C#,我目前正在尝试读取一个文本文件并提取其中的所有电子邮件地址。使用以下函数实现此操作: 我的C#函数: public void extractMails(string filePath) { List<string> mailAddressList = new List<string>(); string data = File.ReadAllText(filePath); Regex emailRegex = new Regex(@"\w+([-

我目前正在尝试读取一个文本文件并提取其中的所有电子邮件地址。使用以下函数实现此操作:

我的C#函数:

public void extractMails(string filePath)
{
    List<string> mailAddressList = new List<string>();

    string data = File.ReadAllText(filePath);
    Regex emailRegex = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.IgnoreCase);
    MatchCollection emailMatches = emailRegex.Matches(data);

    StringBuilder sb = new StringBuilder();

    foreach (Match emailMatch in emailMatches)
    {
        sb.AppendLine(emailMatch.Value);
    }

    string exePath = System.Reflection.Assembly.GetEntryAssembly().Location;
    string dirPath = Path.GetDirectoryName(exePath);

    File.WriteAllText(dirPath + "extractedEmails.txt", sb.ToString());
}
公共邮件(字符串文件路径)
{
List mailAddressList=新列表();
字符串数据=File.ReadAllText(文件路径);
Regex emailRegex=newregex(@“\w+([-+.]\w+*@\w+([-.]\w+*\)。\w+([-.]\w+*”,RegexOptions.IgnoreCase);
MatchCollection emailMatches=emailRegex.Matches(数据);
StringBuilder sb=新的StringBuilder();
foreach(在emailMatches中匹配emailMatch)
{
sb.AppendLine(emailMatch.Value);
}
字符串exePath=System.Reflection.Assembly.GetEntryAssembly().Location;
字符串dirPath=Path.GetDirectoryName(exePath);
WriteAllText(dirPath+“extractedEmails.txt”,sb.ToString());
}
现在我添加了一个progressbar,因为加载的文本文件可能很大。如何在执行函数时填充progressbar,使progressbar最终填充到100%呢


如果您能提供任何帮助,我将不胜感激。

您只需遍历文件中所需的所有对象即可。您需要其中的对象数量,然后将当前迭代器乘以100除以对象总数。这是你的责任。现在用您得到的值更新条的进程。

@user3185569注释正确。我提供了一种不同的解决方案,不使用
async
wait
,以防您使用的是较旧版本的Visual Studio

基本上,您需要在一个新线程中启动任务,然后使用
Invoke()
更新进度条。下面是一个简单的例子:

private int _progress;
private delegate void Delegate();

private void btnStartTask_Click(object sender, EventArgs e)
{
    // Initialize progress bar to 0 and task a new task
    _progress = 0;
    progressBar1.Value = 0;
    Task.Factory.StartNew(DoTask);
}

private void DoTask()
{
    // Simulate a long 5 second task
    // Obviously you'll replace this with your own task
    for (int i = 0; i < 5; i++)
    {
        System.Threading.Thread.Sleep(1000);
        _progress = (i + 1)*20;
        if (progressBar1.InvokeRequired)
        {
            var myDelegate = new Delegate(UpdateProgressBar);
            progressBar1.Invoke(myDelegate);
        }
        else
        {
            UpdateProgressBar();
        }
    }
}

private void UpdateProgressBar()
{
    progressBar1.Value = _progress;
}
private int\u进度;
私有委托void delegate();
私有无效btnStartTask_单击(对象发送者,事件参数e)
{
//将进度条初始化为0并执行新任务
_进度=0;
progressBar1.值=0;
Task.Factory.StartNew(DoTask);
}
私有void DoTask()
{
//模拟一个长5秒的任务
//显然,您将用自己的任务替换此任务
对于(int i=0;i<5;i++)
{
系统线程线程睡眠(1000);
_进度=(i+1)*20;
如果(progressBar1.InvokeRequired)
{
var myDelegate=新委托(UpdateProgressBar);
progressBar1.Invoke(myDelegate);
}
其他的
{
UpdateProgressBar();
}
}
}
私有void UpdateProgressBar()
{
progressBar1.Value=\u进度;
}

假设使用WinForms,使用BackgroundWorker您需要将
ReadAllText
替换为
ReadLines
,逐行读取文件,这是大文件的最佳做法,并使用异步等待模式同步执行长时间运行的代码。是的,这是winform应用程序