本文介绍了C#更新和使用BackgroundWorker的过程中添加文本框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#Windows窗体应用程序我扔在一起。这是相当简单:\

I've got a c# windows form app I threw together. It's fairly simple:\

输入:


  • 文本字符串

  • 源文件夹路径

  • 目标文件夹路径

  • 整数计数

  • text string
  • source folder path
  • destination folder path
  • integer count

通过文本文件的搜索应用程序在输入文本字符串源文件夹;如果发现该文件中的字符串,然后将其复制并使用相同的名称到目标文件夹的图像文件。它基于输入的整数这可是很多次了。

The app searches through text files in the source folder for the entered text string; if it finds the string then it copies that file and an image file with the same name to the destination folder. It does this however many times based on the integer input.

所以我有一个按钮,在按钮单击事件我称之为

So I have a button, and in the button click event I call

ProcessImages(tbDID.Text, tbSource.Text, tbDest.Text, comboBoxNumberImages.SelectedItem.ToString());



这就是:

which is:

private void ProcessImages(string DID, string SourceFolder, string DestFolder, string strNumImages)
        {         
            int ImageCounter = 0;
            int MaxImages = Convert.ToInt32(strNumImages);

            DirectoryInfo di = new DirectoryInfo(SourceFolder);

            foreach (FileInfo fi in di.GetFiles("*.txt"))
            {
                if (fi.OpenText().ReadToEnd().Contains(DID))
                {
                    //found one!
                    FileInfo fi2 = new FileInfo(fi.FullName.Replace(".txt", ".tif"));
                    if (fi2.Exists)
                    {
                        try
                        {
                            tbOutput.Text += "Copying " + fi2.FullName + " to " + tbDest.Text + "\r\n";
                            fi2.CopyTo(tbDest.Text + @"\" + fi2.Name, true);
                            tbOutput.Text += "Copying " + fi.FullName + " to " + tbDest.Text + "\r\n";
                            fi.CopyTo(tbDest.Text + @"\" + fi.Name, true);

                            ImageCounter++;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                }

                if (ImageCounter >= MaxImages)
                    break;

            }

        }



会发生什么该工艺运行良好,但我想与进度表上更新一个文本框的文件拷贝。基本形式清空了,而它的运行,并且它完成后的输出是在文本框中。我想实现一个BackgroundWorker得到它,而它的运行时更新UI。

What happens is that the process runs fine, but I want to update a textbox on the form with progress as the files are copied. Basically the form blanks out while it's running, and after it's finished the output is in the textbox. I'd like to implement a BackgroundWorker get it to update the UI while it's running.

我已经通过实例看,但我不是真的跟了上去。我没有一个个完整的价值,我只是想更新。文本改变每个迭代并将它显示。我甚至不认为我一定是需要把实际的复制操作在不同的线程,它只是听起来好像需要从主UI线程独立运行。也许我在这个复杂干脆......有人可以把我在正确的方向?谢谢!

I've looked through the examples but am not really following them. I don't have a percentage complete value, I just want to update .Text changes each iteration and have it display. I don't even think I necessarily need to put the actual copying action in different threads, it just sounds like that needs to be run separately from the main UI thread. Maybe I'm over complicating this altogether...can someone push me in the right direction? Thanks!

推荐答案

如果您使用的是后台工作人员可以使用ReportProgress方法返回的任意整数,如记录数处理。它并不必须是一个百分比。然后,在ProgressChanged处理程序,您可以更新您的文本框。 。例如

If you use a background worker you can use the ReportProgress method to return any integer, such as the number of records processed. It doesn't have to be a percentage. Then, in the ProgressChanged handler you can update your textbox. E.g.

int count = e.ProgressPercentage;
textBox1.Text = string.Format("{0} images processed.", count);

如果你不想使用背景工人可以调用Application.DoEvents()内的循环。这会给用户界面有机会刷新自己并响应用户的动作。但要注意 - 它会减慢你的程序很多,所以您可能需要调用它仅在每次迭代100

If you don't want to use a background worker you can call Application.DoEvents() inside your loop. This will give the UI an opportunity to refresh itself and respond to user actions. But beware - it will slow your program a lot so you may want to call it only on every 100th iteration.

这篇关于C#更新和使用BackgroundWorker的过程中添加文本框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 05:38