C#进度栏背景工作者 [英] C# Progress Bar Background Worker

查看:59
本文介绍了C#进度栏背景工作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我一直在尝试最后几个小时,以了解和应用进度栏"和后台工作者".

我必须要有功能.

第一个接受一个参数的是combobox1.text.
另一个包含两个:(combobox1.text,combobox2.text).


第一个功能是:

Hi,
I''ve been trying the last couple hours to understand and apply the "Progress Bar" and the "background worker".

I have to functions wich take..

The first one takes one argument wich is the combobox1.text.
The other one takes two : (combobox1.text,combobox2.text).


The first Function is :

public void CopyDirectory(string strSource, string strDestination)
        {
            string strDestinationFile = string.Empty;
            DirectoryInfo Dirinfo = new DirectoryInfo(strSource);
            if (!Directory.Exists(strDestination))
            {
                Directory.CreateDirectory(strDestination);
            }
            foreach (FileSystemInfo filesInfo in Dirinfo.GetFileSystemInfos())
            {
                strDestinationFile = Path.Combine(strDestination, filesInfo.Name);

                //Check If it''s a file
                if (filesInfo is FileInfo)
                {
                    File.Copy(filesInfo.FullName, strDestinationFile, true);
                }
                else
                    // Recall The same Function
                    CopyDirectory(filesInfo.FullName, strDestinationFile);
					}
					}



第二个是:



The second one is:

public static long DirSize(DirectoryInfo d)
{
    long size = 0;
    FileInfo[] fis = d.GetFiles();
    foreach (FileInfo fi in fis)
    {
        size += fi.Length;
    }
    DirectoryInfo[] dis = d.GetDirectories();
    foreach (DirectoryInfo di in dis)
    {
        size += DirSize(di);
    }
    return size; //Returns the size of the whole directory for later usage on progress Bar




我没有在代码块中包含进度条值更新的代码,因为它暂时不重要..

由于后台工作人员无法访问表单的控件,因此我应该如何实现其中获得的这两个功能?

在此先感谢Stelios K.




I didn''t included the code for the progressbar value update in the code block as it''s unimportant for now..

Since background worker can''t access the control of the forms how am i supposed to implement these two functions i got in it ??

Thanks in advance, Stelios K.

推荐答案

请参阅以下链接.

http://www.dotneat.net/2009/02/10/BackgroundworkerExample.aspx [ ^ ]

[从WW:您只是缺少新窗口链接的插入符号和右括号]
See the following link.

http://www.dotneat.net/2009/02/10/BackgroundworkerExample.aspx[^]

[From WW: you were just missing the caret and right bracket for the new window link]


首先,这两个函数都不需要访问表单中的任何内容.它们是完全独立的.

对于DirectoryWorker,CopyDirectory是一个非常基本的例程.唯一棘手的部分是将两个参数传递给DoWork例程.有很多方法可以做到这一点.一种方法是创建一个简单的结构,该结构具有要传递的参数,然后将该结构传递给RunWorkerAsync方法.然后,在DoWork中,您只需从struct中提取参数并调用copy函数.

在另一个例程中,只需确保将e.Result设置为等于DirSize函数的结果.

然后,在RunWorkerCompleted事件中,您可以报告结果.
First, neither of those functions has to access anything on the form. They''re wholly self-contained.

CopyDirectory is a pretty basic routine for a BackgroundWorker. The only tricky part is getting two arguments through to the DoWork routine. There are a lot of ways to do it. One way is to create a simple struct that has the arguments you''ll be passing and then pass that struct to the RunWorkerAsync method. Then, in the DoWork, you just pull out the arguments from the struct and call the copy function.

In the other routine, you just have to make sure that you set e.Result equal to the result of the DirSize function.

Then, in the RunWorkerCompleted event, you can report the result.


这篇关于C#进度栏背景工作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆