After a long time since this question was posted, I can use edge.js easily to communicate IN and OUT my .NET UI (even control node.js within node-webkit from .NET UI), using node event emitter:
// demo basic code in node.js
var
edge = require('edge'),
Bridge = edge.func('../path/to/compiled.dll'),
callback,
ev = new require('events').EventEmitter();
ev.on('acquire', function(fingerdata){
console.log(fingerdata);
});
ev.on('error', function(){
});
callback = function(event, report){
// report the result of the event emitter back to .NET
// you can even pass the "report" to the event handler, so you can return anything you want back to .NET, not just a boolean
report(null, ev.emit(event.name, event.data));
//ev.emit(event.name, {data: event.data, report: report});
};
var bridge = Bridge(callback, true);
// calling bridge(null, true); "releases" my device, should be called on process.on('exit')
And now you can call in/out from .NET using events, instead of calling native code (that might not be thread safe)
namespace Bridge
{
public class Startup
{
public async Task<object> Invoke(Func<object, Task<object>>callback)
{
Bridge.Base.setCallback(callback);
MainForm mainForm = new Bridge.MainForm();
Task.Run(async () =>
{
Application.Run(mainForm);
});
return (Func<object, Task<object>>)(async (i) => { Bridge.Base.release(); return null; });
}
}
}
// inside Bridge.Base
static public void setCallback(Func<object, Task<object>> cb)
{
if (callback == null)
{
callback = cb;
}
}
static public async void Emit(string name, object data)
{
return await Task.Run(async () =>
{
return await callback(new {
name = name,
data = data
});
});
}
static public Func<object, Task<object>> callback = null;
can now call Emit('error', "My error")
from anywhere my derived classes from Base
, asynchronously. Just notice that I recently started dwelving in C#, and my code presented here might not be the best fit.