WebWorker integration idea

Beluvius
Posts: 5
Joined: Wed Oct 21, 2015 12:25 am

WebWorker integration idea

Postby Beluvius » Wed Oct 21, 2015 1:26 am

As many other developers, we face quite heavy client based calculations in our application. We would like to port to C#/XAML next year and are investigating our possibilities. (Apologies upfront if the javascript syntax isn't fully up to standards: my javascript is not up to date yet, but I hope you get the idea.)

I am curious if the following idea would work/be doable by the C#/XAML team:
Create a special CsXamlThread base class which compiles to javascript Webworker.

Code example:
main.cs file:

Code: Select all

public class WorkerThread : CsHtmlThread
{
  public void Calculation1(int x1, int x2)
  {
    // some difficult calculation
    Calculation1Result(x1 + x2);
  }
  public event System.Action<int> Calculation1Result;
}

public class MyClass {
  private WorkerThread CalculationWorker;

  public MyClass()
  {
    CalculationWorker = new WorkerThread();
    CalculationWorker.Calculation1Result += HandleCalculation1Result;
    CalculationWorker.Calculation1(1, 2);
  }

  private void HandleCalculation1Result(int result)
  {
    // process result
  }
}


which would translate to:
WorkerThread.js:

Code: Select all

self.addEventListener('message', function(e) {
    if (e.data[0] == 'Calculation1')
    {
      self.postMessage(['Calculation1Result', e.data[1] + e.data[2]]);
    }
  });


main.js:

Code: Select all

function MyClass() {
  this.Worker CalculationWorker = undefined;
  this.CalculationWorker = new Worker('WorkerThread.js');
  this.CalculationWorker.addEventListener('message', function(e)
    {
      if (e.data[0] == 'Calculation1Result')
      {
        HandleCalculation1Result(e.data[1]);
      }
    });
  this.CalculationWorker(1, 2);
}
MyClass.prototype.HandleCalculation1Result = function(result) {
  // process result
}


It is just an idea to discuss the possibilities.
I would not mind having to write more code in the WorkerThread code, e.g:
- call a process function CsHtmlThreadProcess(System.Action) to be able to debug without javascript (CsHtmlThread should behave like a 'normal' Thread)
- add attributes to whichever function needed denoting anything

Code: Select all

public class WorkerThread : CsHtmlThread
{
  [CsHtmlThreadMethod]
  public void Calculation1(int x1, int x2)
  {
    // some difficult calculation
    CsHtmlThreadProcess(() => Calculation1Result(x1 + x2));
  }
  public event System.Action<int> Calculation1Result;
}


As a final note, I do not need the ability to share data between the main thread and the worker thread (as is implemented in the beta of FireFox by using SharedArrayBuffer). I can work around those limitations (I can rewrite our code to only pass objects which can already be passed to WebWorkers), as long as I can create a WebWorker in C#/XAML code by use of just C# code.

Best regards,
Beluvius

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

Re: WebWorker integration idea

Postby JS-Support » Fri Oct 23, 2015 11:37 am

Hello Beluvius and welcome to the forums.

Thank you very much for your interesting message.

We are going to think about this. Our plan was to think about a possible implementation of background threads with WebWorkers after we go out of the Beta phase, sometime in 2016. I will let you know if we have a chance to look into this sooner though.

Regards,
JS-Support


Return to “General Discussion and Other”

Who is online

Users browsing this forum: No registered users and 2 guests