首页 > 解决方案 > 为什么在发送 nativemessage 时 Chrome 关闭之前,本机应用程序不显示?

问题描述

我有一个在 Chrome 中发送本机应用程序的扩展程序。当我发送消息时,在关闭 chrome 之前,本机应用程序不会显示。这里有什么问题?

chrome.downloads.onDeterminingFilename.addListener(function(item, suggest) {

valpairs = item.finalUrl + "e7dfa9a6-f9d8-4146-89f5-ca441f0f5b23" + valpairs;

var port = chrome.runtime.connectNative('com.alto.downloadmanager');
port.onMessage.addListener(function(msg) {
  console.log("Received" + msg);
});
port.onDisconnect.addListener(function() {
  console.log("Disconnected");
});
port.postMessage({ text: valpairs });


chrome.downloads.cancel(item.id);
});

程序.cs:

static void Main(string[] args)
        {
            JObject data;
            var processed = "";
            while ((data = Read()) != null)
            {
                processed = ProcessMessage(data);
                Write(processed);
                if (processed == "exit")
                {
                    break;
                }
            }


            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1(processed));

        }

标签: c#google-chrome-extensionchrome-native-messaging

解决方案


您必须刷新该数据才能立即将其发送到您的本地消息传递主机。从您的帖子中不清楚,您使用什么 API 来写入标准输入。如果您正在使用,FileStream则使用FileStream.Flush,或者如果您正在使用,StreamWriter则使用StreamWriter.FlushC++,正如我在此线程中提到std::flush那样


推荐阅读