簡體   English   中英

C# 是否可以從后台進程的控制台應用程序發送 Toast / BalloonTip?

[英]C# Is it possible to send Toast / BalloonTip from Console App that a Background Process?

我正在開發一個應該在 PC 啟動后運行的應用程序(Windows 10 C# Windows 控制台應用程序)

因此,我使用 schtask 來解決它,如下所示:

schtasks /create /sc ONSTART /tn "RamanLocalServer" /tr "$INSTDIR\RamanLocalServer.exe" /ru System
schtasks /run /tn "RamanLocalServer"

這是一個完全沒有 UI 的 Rahman 服務器(只有控制台應用程序)

這是我位於 TaskManager 上的程序節目地點

現在,我希望這個本地服務器向用戶顯示通知

---------------嘗試1:-----------

根據C# 如何從非表單應用程序制作 BalloonToolTip可以這樣做

但是,由於我的程序位於后台進程中,因此沒有顯示Balloontooltip

我像這樣包裝代碼

private static void BalloonTip()
        {
            notifyIcon = new NotifyIcon();
            notifyIcon.Icon = Properties.Resources.new_icon;
            notifyIcon.Visible = true;
            log.Debug("Balloon on server");
            try
            {
                notifyIcon.ShowBalloonTip(600, "Rahman Updater", "New update is available\nGo to Rahman Manager to update", ToolTipIcon.Info);
            }
            catch (Exception e)
            {
                log.Error(e.Message);
                log.Error(e.StackTrace);
                throw;
            }
        }

日志文件對異常沒有任何說明

---------------嘗試2:-----------

我嘗試按照此處的說明使用 Toast https://docs.microsoft.com/en-us/uwp/api/windows.ui.notifications.toastnotification?view=winrt-20348

這就是我寫代碼的方式

private static void CallToast()
        {
            log.Debug("Toast1");
            try
            {
                //// Requires Microsoft.Toolkit.Uwp.Notifications NuGet package version 7.0 or greater
                //new ToastContentBuilder()
                    .AddArgument("action", "viewConversation")
                    .AddArgument("conversationId", 9813)
                    .AddText("Guardian Updater")
                    .AddText("New update is available\nGo to Guardian Manager to update")
                    .Show(); // Not seeing the Show() method? Make sure you have version 7.0, and if you're using .NET 5, your TFM must be net5.0-windows10.0.17763.0 or greater
            }
            catch (Exception e)
            {
                log.Error(e.Message);
                log.Error(e.StackTrace);
            }
        }

它在日志Access denied. (Exception occurred HRESULT: 0x80070005 (E_ACCESSDENIED)顯示此錯誤Access denied. (Exception occurred HRESULT: 0x80070005 (E_ACCESSDENIED) Access denied. (Exception occurred HRESULT: 0x80070005 (E_ACCESSDENIED)


但是,如果我像這樣雙擊它來啟動程序,它既適用於 Toast/Balloon 工具提示

應該是這樣的

根據stackoverflow社區(對不起,我找不到鏈接),我了解到

為了使其工作(Toast/Balloon Tooltip),代碼應該在 UI 線程上運行

所以我的問題是,這可能嗎?

如果沒有,有什么建議嗎?

那是我在我在 StackOverflow 上的帳戶以某種方式重新啟動之前提出的問題

我通過以下方式解決了這個問題:

  1. 創建一個負責balloonTip的新可執行文件
  2. 讓它在托盤上永遠運行(c# 能夠做到這一點)
  3. 為了觸發該可執行文件,我將該程序注冊到啟動程序列表

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM