c# - passing parameter to dowork? -
i calling zip_threading class in class. string = zip_threading(?,?)
but problem how can pass parameter values when calling class : string [] files, bool isoriginal. have used in class background worker threading, real problem passing value class , return value when processing finished in make_zip_file class.
public class zip_threading { public string[] files { get; set; } // recieved zip method zip file names. public int number; public string return_path; public bool isoriginal { get; set; } // recieved zip method boolean true or fales public static backgroundworker bgw1 = new backgroundworker(); // make background worker object. public void bgw1_runworkercompleted(object sender, runworkercompletedeventargs e) { make_zip_file mzf1 = e.result 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) (int = 0; < files.length; i++) ***filenames[i] = httpcontext.current.request.physicalapplicationpath + files[i].remove(0, 10).tostring();*** else (int = 0; < 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 rethrow exception our purposes handled. } return_path = "images/thumbnails/zipfiles/" + directoryname + ".zip"; return return_path; }}
now calling method in other class: this
string path=zipa.run_async(filecollection, isoriginal);
i error in make_zip_file, , mark : object reference not set instance of object* filenames[i] = httpcontext.current.request.physicalapplicationpath + files[i].remove(0, 10).tostring();
*
you can use lambda functions nicer syntax. when registering handler dowork
, can create lambda calls function , assigns result result
property:
make_zip_file mzf2 = new make_zip_file(); bg.dowork += (sender, e) => e.result = dowork(files, isoriginal, mzf2); bgw1.runworkerasync();
then, dowork
method computation , isn't tightly coupled internals of backgroundworker
type:
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; }
Comments
Post a Comment