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
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
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?