Javascript JS如何从同一个JS文件执行stdio操作和Ui操作?

Javascript JS如何从同一个JS文件执行stdio操作和Ui操作?,javascript,google-chrome,asynchronous,electron,Javascript,Google Chrome,Asynchronous,Electron,我正在尝试在Electron应用程序的main.js中执行两项任务 1) 监听标准io端口,并在应用程序收到消息时回复。 2) 加载UI(index.js) 第(1)部分或第(2)部分单独运行良好,但如果我试图从main.js一次执行它们,它们就不起作用 请让我知道如何一起执行它们 const electron = require('electron') // Module to control application life. const app = electron.app // Modu

我正在尝试在Electron应用程序的main.js中执行两项任务 1) 监听标准io端口,并在应用程序收到消息时回复。 2) 加载UI(index.js)

第(1)部分或第(2)部分单独运行良好,但如果我试图从main.js一次执行它们,它们就不起作用

请让我知道如何一起执行它们

const electron = require('electron')
// Module to control application life.
const app = electron.app
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow

const path = require('path')
const url = require('url')

let mainWindow

/************ PART 1 *********************************************/

function nativeMessageReply()
{
var nativeMessage = require('chrome-native-messaging');

process.stdin
.pipe(new nativeMessage.Input())
.pipe(new nativeMessage.Transform(function (msg, push, done) {
  var reply = getReplyFor(msg); // Implemented elsewhere by you.
  push(reply);                  // Push as many replies as you like.
  done();                       // Call when done pushing replies.
}))
.pipe(new nativeMessage.Output())
.pipe(process.stdout)
;
}

function getReplyFor(msg)
{
    return "Reply from native";
}

/*********** PART 2 *****************************/

function createWindow () {
  mainWindow = new BrowserWindow({width: 800, height: 600})

  mainWindow.loadURL(url.format({
    pathname: path.join(__dirname, 'index.html'),
    protocol: 'file:',
    slashes: true
  }))

  mainWindow.on('closed', function () {
    mainWindow = null
  })
}

app.on('ready', createWindow)

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', function () {
  if (mainWindow === null) {
    createWindow()

    // How to call this on seperate thread ?
    nativeMessageReply()
  }
})