.net - VC++ Winforms passing member function as argument to BackgroundWorker -
being beginner, struggling syntax here. making generic backgroundworker avoid making separate workers each of many tasks in application. unable figure out how pass member function runworkerasync()
here dowork method's code:
private: system::void backgroundworker2_dowork(system::object^ sender, system::componentmodel::doworkeventargs^ e) { func<int> ^func = (func<int>^)e->argument; e->result = func(); }
let's want bg worker run function named myfunc. want this: runworkerasync(myfunc)
while myfunc member of same class i.e form1
it isn't clear me hangup might be, syntax create func<> isn't different way subscribe dowork event. sample code:
ref class example { backgroundworker^ worker; void ondowork(system::object ^sender, system::componentmodel::doworkeventargs ^e) { auto job = safe_cast<func<int>^>(e->argument); e->result = job(); } int myfunc() { return 42; } public: example() : worker(gcnew backgroundworker) { worker->dowork += gcnew system::componentmodel::doworkeventhandler(this, &example::ondowork); auto job = gcnew func<int>(this, &example::myfunc); worker->runworkerasync(job); } };
Comments
Post a Comment