c# - Pass arguments by reference to BackgroundWorker -


i need invoke c dll background process. execution of dll time-taking , therefore want show progress bar on main gui. in order use reportprogress, need able pass 2 arguments reference , update progress bar whenever values of arguments change. however, runworkerasync seems take values (not references).

how can it?

thanks.

here simplified code:

public void button1_click(object sender, eventargs e)     {        //variable declarations , initializations         list<object> arguments = new list<object>();                 arguments.add(curgen);                 arguments.add(dataindex);          backgroundworker1.runworkerasync(arguments);         backgroundworker1.reportprogress(curgen * 100 / ngen, "gen");         backgroundworker1.reportprogress(dataindex * 100 / (dimension * fitnesscases), "data");      }   private void backgroundworker1_dowork(object sender, doworkeventargs e)     {         list<object> genericlist = e.argument list<object>;         // getting variables object          calldll.gpinnovization(ref curgen, ref dataindex);     } 

i want pass 'curgen' , 'dataindex' reference.

you can use lambda dowork event handler close on variables (note closures close on variables, not values, not copy value). there can either work right in lambda, or pass variable, reference, other methods.

int somevalue = 5;  backgroundworker worker = new backgroundworker(); worker.dowork += (s, args) => {     processvalue(ref somevalue); }; worker.runworkerasync(); 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -