将参数传递给dowork?

时间:2011-02-09 15:17:46

标签: c# multithreading

我在另一个类中调用此zip_threading类。 string a = zip_threading(?,?)但问题是我在调用这个类时如何传递参数值:String [] files,bool IsOriginal。我在这个类后台工作者线程中使用过,所以真正的问题是将值传递给这个类,然后在make_zip_file类中处理完成后返回一个值。

public class zip_threading
{

    public string[] files { get; set; } // to be recieved by the zip method as zip file names.
    public int number;
    public string return_path;
    public bool IsOriginal { get; set; }  // to be recieved by the zip method as boolean true or fales
    public static BackgroundWorker bgw1 = new BackgroundWorker(); // make a background worker object.
public void bgw1_RunWorkerCompleted(Object sender, RunWorkerCompletedEventArgs e)
    {

        make_zip_file mzf1 = e.Result as make_zip_file;
        return_path = mzf1.return_path;

    }
    public make_zip_file bgw_DoWork(string[] files, bool IsOriginal, make_zip_file argumentest)
    {

        Thread.Sleep(100);
        argumentest.return_path = argumentest.Makezipfile(files,IsOriginal);
        return argumentest;

    }
    public void run_async(string []files,bool IsOriginal)
    {

        make_zip_file mzf2 = new make_zip_file();
       // mzf2.files = files;
        //mzf2.IsOriginal = IsOriginal;

        bgw1.DoWork += (sender, e) => e.Result = bgw_DoWork(files, IsOriginal, mzf2);
        bgw1.RunWorkerAsync();

}
 public  class make_zip_file
    {
        public string return_path ;
        //public string[] files{get;set;}
       // public bool IsOriginal{get;set;}


        public string Makezipfile(string[] files, bool IsOriginal)
        {

            string[] filenames = new string[files.Length];
            if (IsOriginal)
                for (int i = 0; i < files.Length; i++)
                    ***filenames[i] = HttpContext.Current.Request.PhysicalApplicationPath + files[i].Remove(0, 10).ToString();***
            else
                for (int i = 0; i < files.Length; i++)
                    ***filenames[i] = HttpContext.Current.Request.PhysicalApplicationPath + files[i].Replace(HttpContext.Current.Request.UrlReferrer.ToString(), "");***
            string DirectoryName = filenames[0].Remove(filenames[0].LastIndexOf('/'));
            DirectoryName = DirectoryName.Substring(DirectoryName.LastIndexOf('/') + 1).Replace("\\", "");

            try
            {

                string newFile = HttpContext.Current.Request.PhysicalApplicationPath + "images\\Thumbnails\\zipFiles\\" + DirectoryName + ".zip";
                if (File.Exists(newFile))
                    File.Delete(newFile);
                using (ZipFile zip = new ZipFile())
                {

                    foreach (string file in filenames)
                    {

                        string newfileName = file.Replace("\\'", "'");
                        zip.CompressionLevel = 0;
                        zip.AddFile(newfileName, "");
                    }

                    zip.Save(newFile);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception during processing {0}", ex);

                // No need to rethrow the exception as for our purposes its handled.
            }

            return_path = "images/Thumbnails/zipFiles/" + DirectoryName + ".zip";
return return_path;
}}

现在我在其他类中调用此方法:像这样

String path=zipa.run_async(fileCollection, IsOriginal);

我在make_Zip_File中遇到错误,我用 标记:对象引用未设置为对象的实例 * {{1 }} *

4 个答案:

答案 0 :(得分:5)

在调用bgw1.RunWorkerAsync()时传递参数。

EG:

bgw1.RunWorkerAsync(IsOriginal)

OR

bgw1.RunWorkerAsync(new object[] {IsOriginal})

答案 1 :(得分:4)

您可以使用lambda函数来获得更好的语法。注册DoWork的处理程序时,可以创建调用该函数的lambda并将结果赋给Result属性:

make_zip_file mzf2 = new make_zip_file(); 
bg.DoWork += (sender, e) => 
    e.Result = DoWork(files, isOriginal, mzf2);
bgw1.RunWorkerAsync();

然后,DoWork方法只进行计算,并没有与BackgroundWorker类型的内部结构紧密耦合:

public make_zip_file DoWork
   (string[] files, bool isOriginal, make_zip_file argumentest) {
      Thread.Sleep(100);
      argumentest.return_path = argumentest.Makezipfile(files, IsOriginal);
      return argumentest;
}

答案 2 :(得分:3)

在调用RunWorkerAsync时传递数据

BackgroundWorker.RunWorkerAsync(Object)

答案 3 :(得分:1)

如果需要将多个参数传递给BackgroundWorker,那么您可以将所有参数包装在一个对象中并传入该一个对象。

只需创建一个包含一个文件列表以及IsOriginal的新类。