C#后台工作者参数问题?

发布于 2024-10-16 18:35:42 字数 3395 浏览 3 评论 0原文

我在另一个类中调用这个 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 中遇到错误,并用以下标记标记:对象引用未设置为对象的实例* filenames[i] = HttpContext.Current.Request.PhysicalApplicationPath +文件[i].Remove(0, 10).ToString();*

I am calling this zip_threading class in another class. string a = zip_threading(?,?)but the problem is that how can i pass the parameter values when i am calling this class which are : String [] files, bool IsOriginal. i have used in this class background worker threading, so the real problem is that passing the value to this class and then return a value when processing is finished in make_zip_file class.

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;
}}

now i am calling this method in other class: like this

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

I get error in make_Zip_File, and i mark that with : Object reference not set to an Instance of an object* filenames[i] = HttpContext.Current.Request.PhysicalApplicationPath + files[i].Remove(0, 10).ToString();*

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

_蜘蛛 2024-10-23 18:35:42

通过将其带到不同的线程,您将在 http 上下文之外运行,这很可能在您的 zip 操作完成之前很久就完成(拆除所有诸如入站流缓冲区之类的东西) - 但您是与HttpContext.Current交谈。

你有几个选择;我突然想到...

  • 在请求线程上运行它;这需要一段时间,但是嗯...
  • 缓冲内存中所需的所有数据,并将其传递给 zip 操作,
  • 将文件从请求线程写入临时区域(而不是主应用程序文件夹)中的磁盘,然后生成一个单独的线程来从临时区域处理它,

但要重复:您无法从另一个线程访问该请求 - 或者至少,您不应该这样做。

另外,请考虑:

  • 请求启动
  • 一个线程来执行
  • 从原始请求返回的
  • zip (工作线程继续运行),

您需要考虑将如何处理 zip 文件名;你不能直接把它交给客户——他们不再听你的了。

By taking this to a different thread, you are running outside of the http-context, which may well finish long before your zip operation does (tearing down all the things like inbound stream buffers) - yet you are talking to HttpContext.Current.

You have a few options; thinking off the top of my head...

  • run it on the request thread; it'll take a while, but meh...
  • buffer all the data you need in memory, and pass that to the zip operation
  • write the file to disk in a temp area (not the main app folder) from the request thread, then spawn a separate thread to process it from the temp area

but to re-iterate: you can't access the request from another thread - or at least, you shouldn't.

Also, consider:

  • a request starts
  • you spin up a thread to do the zip
  • you return from the original request
  • (worker thread keeps on going)

you need to think about what you are going to do with the zip filename; you can't just give it to the client - they are no longer listening to you.

动次打次papapa 2024-10-23 18:35:42

检查 files[i] 是否已初始化,因为它来自某个函数,

Makezipfile(string[] files, bool IsOriginal)
{

}

我认为它没有任何价值。

Check files[i] is intialized or not since it is coming from somewhere to the function

Makezipfile(string[] files, bool IsOriginal)
{

}

i think there will be no value in it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文