multithreadingcappuccinoobjective-j

How to multi-thread in Cappuccino


I have a data intensive Cappuccino application that can take up to 30 seconds to run a 'job' An example might be to combine values from a number of arrays to produce a further array for use in a TableView. I would like to include an animated progress bar when this will take more than a second or 2, but I seem to block the primary(main) thread so no activity is visible. i.e. console messages and display updates all wait until the job is complete. Is is possible to run such a job on a background thread so that the main application, including a progress bar, can update and display during the process?


Solution

  • Javascript doesn't have threads or shared memory. You can execute your code in parallel using the webworker API. The general problem with web workers is that the code that is run in the worker must be in a separate file.

    Luckily someone has already created a nice wrapper for it which will create a data blob from a string of code and use that as the file for the worker. You'll then want to pass your data to that new worker, and let it run.

    You can occasionally call postMessage() from your worker which you can use to update a progress indicator.

    https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/basic_usage