調用.Net Dll
const edge = require('electron-edge-js');
var testDll = edge.func({
assemblyFile: "./dll/electronedge.dll",
typeName: "electronedge.MQ",
methodName: "Invoke"
});
window.setInterval(heartbeat, 100);
var indexOption=0;
function heartbeat(){
testDll("測試消息 "+indexOption,(err,value)=>{
var selectObj=document.getElementById("list");
selectObj.options[indexOption] = new Option(value, indexOption);
selectObj.selectedIndex = indexOption;
indexOption++;
var num = parseInt(value.substr(value.indexOf("測試消息 ")).replace(/測試消息 /,""));
//alert(num);
if(num != 0 && num % 5 == 0 && num % 7 == 0 && num<106)
{
//document.getElementById("Open1").click();
var OpenWindow = new BrowserWindow ({frame:true,fullscreen:false,x:0,y:0,webPreferences: {nodeIntegration: true}})
OpenWindow.webContents.openDevTools();
OpenWindow.loadURL('file://' + __dirname + '/MQ.html');
OpenWindow.on("close", function(){
OpenWindow = null;
})
var webContents = OpenWindow.webContents;
webContents.on('dom-ready',function(){
//MQjs是MQ.html中的js函數
webContents.executeJavaScript('MQjs('+num+');');
});
}
});
}
子進程及通信
const spawn = require('child_process').spawn;
var childProcess = spawn('./dll/MQConsole.exe', ['參數1',1234]);
var OpenWindowA = new BrowserWindow ({frame:true,fullscreen:false,x:0,y:0,webPreferences: {nodeIntegration: true}})
//OpenWindowA.webContents.openDevTools();
OpenWindowA.loadURL('file://' + __dirname + '/MQ.html');
OpenWindowA.on("close", function(){
OpenWindowA = null;
});
var webContentsA = OpenWindowA.webContents;
childProcess.stdout.on('data', (data) => {
var msgStr;
if(data.toString().indexOf("WinA:")){
msgStr = "中國:"+data.toString();
webContentsA.executeJavaScript("MQjs('"+ msgStr.replace(/[\r\n]/g,"") + "');");
}
else if(data.toString().indexOf("WinB:")){
msgStr = "美國:"+data.toString();
webContentsB.executeJavaScript("MQjs('"+ msgStr.replace(/[\r\n]/g,"") + "');");
}
else{
msgStr = "日本:"+data.toString();
}
selectObj.options[indexOption] = new Option(msgStr, indexOption);
selectObj.selectedIndex = indexOption;
indexOption++;
});
MQConsole.exe代碼
namespace MQConsole { class Program { static void Main(string[] args) { long num = 0; Console.OutputEncoding = Encoding.UTF8; while (true) { if(num % 2==0) Console.WriteLine($"WinA:{num}"); else Console.WriteLine($"WinB:{num}"); num++; Thread.Sleep(500); } } } }
electronedge.dll代碼
namespace electronedge { public class MQ { public async Task<object> Invoke(object input) { return Helper.Send(input.ToString()); } public async Task<object> InvokeScreen(object id) { return Helper.SendScreen(id.ToString()); } //public async Task<object> InitMQ(object input) //{ // return Helper.InitMQ(); //} //public async Task<object> GetMQMsg(string id) //{ // return Helper.GetMQMsg(id); //} } static class Helper { static string RealData = "0000000"; static Mutex mutex = new Mutex(false); public static string Send(string msg) { return $"來自dll : {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} {msg}"; } public static string SendScreen(string id) { return $"來自{id} : {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}"; } //public static object InitMQ() //{ // Thread threadMQ = new Thread(new ThreadStart(RunMQ)); // threadMQ.Start(); // return null; //} //private static void RunMQ() //{ // mutex.WaitOne(); // RealData = DateTime.Now.Ticks.ToString(); // mutex.ReleaseMutex(); //} //public static string GetMQMsg(string id) //{ // return RealData + "來自ID:" + id; //} } }
完整代碼
