private void SetIconSafe(string iconText) { if (this.Dispatcher.CheckAccess() == false) { this.Dispatcher.Invoke(new Action <string>(SetIconSafe), iconText); return; } var bitmapTemp = IconDrawing.DrawBitmap(iconText); var imageSource = bitmapTemp.ToImageSource(); Icon = imageSource; bitmapTemp.Dispose(); }
public MainWindow() { InitializeComponent(); logger.Info("WhatsappTray2 started"); // https://www.codeproject.com/Articles/36468/WPF-NotifyIcon-2 tbi = new TaskbarIcon(); tbi.Icon = IconDrawing.DrawIcon("0"); //tbi.ToolTipText = "hello world"; tbi.MenuActivation = PopupActivationMode.RightClick; tbi.ContextMenu = CreateContextMenue(); tbi.TrayLeftMouseUp += (sender, e) => RestoreWindow(); Icon = IconDrawing.DrawBitmap("0").ToImageSource(); // Start peridic timer to update the messages count in the tray-symbol. var cancellationToken = new CancellationToken(); Task.Run(async() => { WhatsAppApi.Browser = Browser; logger.Info("Waiting for Whatsapp to initialize."); await PeriodicWaitForWhatsappInitialized(TimeSpan.FromSeconds(1), cancellationToken); WhatsAppApi.Initialize(); // = Start the message-count-update-function = await PeriodicMessageCountUpdate(TimeSpan.FromSeconds(1), cancellationToken); }); if (Properties.Settings.Default.StartMinimized == false) { this.Show(); } else { // If the Browser is not shown we need to initialize it, otherwise it will not load the webpage. Browser.CreateBrowser(null, new Size(20, 20)); } Browser.DownloadHandler = new DownloadHandler(); }
public async Task PeriodicMessageCountUpdate(TimeSpan interval, CancellationToken cancellationToken) { var lastCount = -1; var count = -1; while (true) { logger.Info(nameof(PeriodicMessageCountUpdate)); lastCount = count; count = WhatsAppApi.GetAllChatsWithNewMsgCount(); if (count != lastCount) { var tempIcon = tbi.Icon; tbi.Icon = IconDrawing.DrawIcon(count.ToString()); tempIcon.Dispose(); SetIconSafe(count.ToString()); } await Task.Delay(interval, cancellationToken); } }