1

I am developing an application that supposed to run since the PC is starting (Windows 10 C# Windows Console App)

Therefore I use schtask to address it as follows:

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

This is a Rahman Server which has no UI at all (only console app)

This is my program located on TaskManager Program location

Now, I want this Local server show notification to the user

---------------Attempt1:----------

According to C# How to Make a BalloonToolTip from a Non-Form Application It is possible to do that

However, since my program is located on Background Process, It shows no Balloontooltip

I wrap the code like this

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;
            }
        }

The log file says nothing on exception

---------------Attempt2:----------

I Tried to use Toast as explained here https://learn.microsoft.com/en-us/uwp/api/windows.ui.notifications.toastnotification?view=winrt-20348

This is how I write the code

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);
            }
        }

It shows this error on the log Access denied. (Exception occurred HRESULT: 0x80070005 (E_ACCESSDENIED)


However if I start the program by double click it like this, it works both for Toast/Balloon tooltip

Should be like this

According to the stackoverflow community (Sorry, I can't find the link), I learned that

In order to make it works (Toast/Balloon Tooltip), The code should be running on the UI thread

So my question is, Does it possible?

If not, are there any suggestion?

  • 1
    you have `/ru System` so obviousy it's running as system not logged user ... system account cannot interact with user desktop .. unless you will create user app which can recive some IPC message from System app to show what you need ... eventually you may start your app when user log in with user credentails – Selvin Sep 01 '21 at 08:41
  • Thank you for your comment. I am sorry, this is a stupid question. So I cannot run as System to make it worked? I used system because this program is running since the pc start before the user start to login –  Sep 01 '21 at 08:45
  • 1
    As I wrote ... then you have to have 2 apps (system and user) and some IPC(fx NetNamedPipe WCF) to comunicate between them – Selvin Sep 01 '21 at 08:47
  • Hmm, I see. Thank you for your insight, let me re-design the program then –  Sep 01 '21 at 08:49

1 Answers1

0

That was me who ask the question before my account on StackOverflow was somehow restarted

I solved that problem By:

  1. Make a new Executable that responsible for the balloonTip
  2. Let it run forever on tray (c# is able to do that)
  3. To trigger that Executable, I register that program to Startup Program list
MH Rahman
  • 81
  • 8