c# - Task queue for wp8? -


what correct way queue complex tasks in wp8?

the tasks consists of following:

  1. showing progressindicator through updating model variable
  2. fetching or storing data wcf service (uploadstringasync)
  3. updating potentially data bound model result uploadstringcompleted.
  4. hiding progressindicator through updating model variable

currently i've been working class owning queue of command objects, running single thread started when item added if it's not running.

i have problems waiting tasks or subtasks code stops running.

previously i've used async await, few levels down behaviour becoming more , more unpredictable.

what want main thread being able create , queue command objects. command objects should run 1 @ time, not starting new 1 until previous 1 finished. command objects should able use dispatcher access main thread if neccesary.

if use async/await, there's no need thread (since have no cpu-bound processing).

in case, sounds need queue of asynchronous delegates. natural type of asynchronous delegate func<task> (without return value) or func<task<t>> (with return value). little tip unfortunately not well-known @ point.

so, declare queue of asynchronous delegates:

private readonly queue<func<task>> queue = new queue<func<task>>(); 

then can have single "top-level" task (asynchronously) processes queue:

private task queueprocessor; 

the queueprocessor can null whenever there's no more items. whenever it's not null, it'll represent method:

private async task processqueue() {   try   {     while (queue.count != 0)     {       func<task> command = queue.dequeue();       try       {         await command();       }       catch (exception ex)       {         // exceptions queued tasks end here.         throw;       }     }   }     {     queueprocessor = null;   } } 

your enqueue method this:

private void enqueue(func<task> command) {   queue.enqueue(command);   if (queueprocessor == null)     queueprocessor = processqueue(); } 

right now, have exception handling set this: queued command throws exception cause queue processor stop processing (with same exception). may not best behavior application.

you can use (with either lambda or actual method, of course):

enqueue(async () => {   showprogressindicator = true;   modeldata = await myproxy.downloadstringtaskasync();   showprogressindicator = false; }); 

note use of downloadstringtaskasync. if write tap wrappers eap members, async code more "natural-looking" (i.e., simpler).

this sufficiently complex i'd recommend putting separate class, you'd want decide how handle (and surface) errors first.


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -