c# - wait for static callback complete -


i've scenario:

  1. myapp calls cameracapture
  2. that fires callbackfunction
  3. after callbackfunction (i have photo captured) completes, more stuff.

so have wait callbackfunction complete before executing function. how this?

here code:

private static readonly plustek_camera.pfnck_event staticfncamera = fnpfnck_event;  public static bool fnpfnck_event(int ievent, int iparam, intptr puserdata) {    //capture picture , save folder }  //i implement callback start camera , fire callback staticfncamera  var _status = cameractrl.start(cameractrl.scanmode, cameractrl.resolution, cameractrl.imageformat, cameractrl.alignment, staticfncamera);  //waiting staticfncamera complete make sure image produced    readpassporttext(); 

if understand correctly, have camera control provides asynchronous api start capturing image, want wait synchronously operation complete.

if so, there lots of different ways accomplish you're trying do. 1 such way use taskcompletionsource:

taskcompletionsource<bool> source = new taskcompletionsource<bool>();  var _status = cameractrl.start(cameractrl.scanmode, cameractrl.resolution,     cameractrl.imageformat, cameractrl.alignment,     (ievent, iparam, puserdata) =>     {         staticfncamera(ievent, iparam, puserdata);         source.setresult(true);     });  //waiting staticfncamera complete make sure image produced  await source.task; readpassporttext(); 

note above uses await, valid in async method. haven't provided enough context show how work in code, recommend following above. avoid blocking running thread; async method return @ point, letting thread continue run, , resumed @ readpassporttext(); statement when operation completes.

if reason cannot use await in method, can instead source.task.wait();. will, of course, block executing thread @ statement.

the above requires .net 4.5. there other approaches work earlier versions of .net, need specific requirements make worth trying describe those.


edit:

since using .net 4.0, , presumably visual studio 2010, above won't work "out-of-the-box". 1 option download async ctp visual studio, give c# 5.0 compiler enable above. if that's not feasible you, option compiler on behalf, replacing last 2 lines above following:

source.task.continuewith(task => readpassporttext(),    taskscheduler.fromcurrentsynchronizationcontext()); 

that attach continuation delegate call readpassporttext() task object taskcompletionsource, specifying current synchronization context source of scheduler use run continuation.

the method return after calling continuewith() (just in await version, except here it's written out explicitly instead of compiler doing you). when task object set completed state, previously-registered continuation executed.

note original question isn't clear context. if code running in ui thread, using fromcurrentsynchronizationcontext() important , ensure continuation executed in ui thread well. otherwise, can away without specifying scheduler in call continuewith().


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -