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