Question about threading

rkmore
Posts: 53
Joined: Mon Dec 07, 2015 1:53 pm

Question about threading

Postby rkmore » Thu Apr 07, 2016 4:35 am

Given that in most browsers javascript runs in a single thread (and even in chrome it is sort of a wonky hybrid) what is the situation and future plans for threading support in CSHTML5 ?

I see that it (understandably) pops a not yet implemented if I try to spin up a thread now. Are there some sort of plans for a dispatcher that can to some type of rudimentary threading? Is there something I am missing (very likely)?

Just wondering as I have some very long operations (mostly uploads to the azure blob service) that are blocking now but I would love to be able to run them in a way that does not impact the user's ability to continue entering data etc.

RKM

JS-Support
Site Admin
Posts: 789
Joined: Tue Apr 08, 2014 3:42 pm

Re: Question about threading

Postby JS-Support » Thu Apr 07, 2016 9:50 am

Dear RKM,

To perform long web operations without blocking the UI, such as uploading to the Azure Blob service, you can use the "async" version of the http calls, which are executed by the browser on a background thread and give a callback when they are complete. If you need to have a loop in order to make several calls in sequence without blocking the UI, you can use the async/await pattern, which makes it easy to call async methods in a loop while waiting for the callback before continuing the loop. When the execution is waiting for the result of an async call, the UI is not blocked and other operations can be performed.

Alternatively, to perform client-side tasks such as long calculations that cannot be done with async/await, you only have the following options:
- Block the UI but also force the UI to refresh by calling "Dispatcher.BeginInvoke(...)", as discussed in the following thread: http://forums.cshtml5.com/viewtopic.php?f=4&t=1452 - You can for example display a "Please wait..." message and update the percentage of the progress.
- Create a "WebWorker" in JavaScript and make it do the background tasks that you want to do. You can do so by using the new CSHTML5.Interop.ExecuteJavaScript method introduced in Beta 7.2. You will find more information at the following URL: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers

Thanks.
Regards,
JS-Support


Return to “General Discussion and Other”

Who is online

Users browsing this forum: No registered users and 1 guest

 

 

cron