I'm new at c# development. My goal is to send a toast notification in background (by using Windows Services) alter some petiod of time. Program.cs file:
static class Program {
static void Main() {
try {
ServiceBase[] servicesToRun = {
new TestService()
};
ServiceBase.Run(servicesToRun);
Notifications notifications = new Notifications();
notifications.Start();
var notive = new ToastContentBuilder();
notive.AddText("Notification");
notive.Show();
}
catch (Exception e) {
Logger.WriteLine("Unhandled exception: " + e.Message);
}
}
}
and Notification file:
public class Notifications
{
private readonly Timer _timer;
public Notifications()
{
_timer = new Timer(1000 * 10) {
AutoReset = true};
_timer.Elapsed += TimerElapsed;
}
private void TimerElapsed(object sender, ElapsedEventArgs e)
{
var notive = new ToastContentBuilder();
notive.AddText("Notification2");
notive.Show();
}
public void Start()
{
_timer.Start();
}
public void Stop()
{
_timer?.Stop();
}
}
When I use cmd command 'TestService.exe start', I can see 'Notification', but 'Notification2' that should be sended every 10 sec, does not appear. Also adding code:
string[] lines = new string[] { DateTime.Now.ToString() };
File.AppendAllLines(@"C:\Visual Studio WorkPlace\License.txt", lines);
before creating toast 'Notification2' will work. I suppose that can be because toast 'Notification2' doesn't appear in Main Thread.