C#后台工作者参数问题?
我在另一个类中调用这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过将其带到不同的线程,您将在 http 上下文之外运行,这很可能在您的 zip 操作完成之前很久就完成(拆除所有诸如入站流缓冲区之类的东西) - 但您是与
HttpContext.Current
交谈。你有几个选择;我突然想到...
但要重复:您无法从另一个线程访问该请求 - 或者至少,您不应该这样做。
另外,请考虑:
您需要考虑将如何处理 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...
but to re-iterate: you can't access the request from another thread - or at least, you shouldn't.
Also, consider:
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.
检查 files[i] 是否已初始化,因为它来自某个函数,
我认为它没有任何价值。
Check files[i] is intialized or not since it is coming from somewhere to the function
i think there will be no value in it.