PurplePeopleEater
PurplePeopleEater
CC#
Created by PurplePeopleEater on 4/18/2024 in #help
NotifyIcon fails to appear sometimes when my app starts at user login
My app: - .NET 7.0 Windows GUI app - uses WPF for some parts of the GUI - uses WinForms for other parts of the GUI - uses the NotifyIcon class that is a part of WinForms (System.Windows.Forms.NotifyIcon) to create a NotifyIcon in the Windows Taskbar on the bottom right of the screen immediately next to the Windows clock. - gets launched automatically when the user logs in (either via a shortcut in the "Start Menu\Programs\Startup" folder, a Task that launches it in the Task Scheduler, etc) Problem: About 5% of the time when the user logs into Windows the application launches as desired (I can see the app in the Task Manager) but the NotifyIcon fails to appear in the Windows Taskbar. No error seems to be thrown in the code, i.e. it seems as though .NET thinks the NotifyIcon was created successfully even though Windows is failing to show it. As a side note I have seen this same behavior happen with other apps that I have not written including the SysInterals Process Explorer app which is a Microsoft app. What I think is happening: It seems like if my app gets launched too quickly when the user is logging in then Windows is not "ready" to create NotifyIcons and so it doesn't show the NotifyIcon for my app. Possible Solutions: 1) Delay the start of my app in the Task Scheduler to be 10 seconds (or 60 seconds, etc) after the user logs in. This works but I don't like this solutions because I have to replicate this setup to any computer I setup my app on including VMs and on my friends' computers. 2) Detect that the NotifyIcon failed to be displayed and try to remove and recreate it. I think this is the ideal solution. How can I detect this situation?
2 replies
CC#
Created by PurplePeopleEater on 2/23/2024 in #help
How to close a console created with AttachConsole() by a GUI app without exiting the GUI app
I created a Windows GUI app (for example, it has a taskbar icon) that also uses a console. (console as in conhost.exe) Yes, I am aware this doesn't work as well on Windows as it does on Linux. On program launch the app does not create an attached console. One of the menu items of the taskbar icon, however, does allow you to create an attached console. When created, this attached console displays information from the program as it runs. I want to have a way to completely get rid of the console without exiting my app when I am done reading that information. My problem is that if I click the Close button (little "X" on the top right of the window) on the console then both the console AND my app terminate. Right now I am hiding the console window using ShowWindow() WinAPI as a work around but this leaves the console running in the background - i.e. it is visible in Task Manager. Potential solutions that I am aware of: 1) Create a separate child program that owns the Console. Then send messages from the main app to the child app when I want to write to the console. That way when I completely close the console only the child app will be terminated. Then if I need the console again I can respawn the child app. The downside seems to be that I would need to write and maintain the code for this separate child app and that debugging would become more complicated. Also, I would want to write the child app to automatically close itself if the parent app gets closed which is additional functionality I would need to implement. Basically, this option adds complexity. 2) Use a "fake" console window that merely approximate the visual appearance and behavour of a console but is not actually a console. Unfortunately the examples of these "fake" consoles that I have found online are not great and I don't want to implement one from scratch.
26 replies
CC#
Created by PurplePeopleEater on 2/23/2024 in #help
The taskbar icon for my app becomes orphaned
Under the following circumstances the taskbar icon for my app becomes orphaned. In other words, my app is no longer running but the taskbar icon is still visible until I hover my mouse pointer over it which causes it to disappear. 1) If my app crashes 2) If I press the Stop button in the Visual Studio Debugger Questions: 1) Is there a way to make sure the taskbar icon gets removed if scenario #1 above takes place? 2) Is there a way to make sure the taskbar icon gets removed if scenario #2 above takes place? Thank you!
21 replies
CC#
Created by PurplePeopleEater on 2/23/2024 in #help
The taskbar icon for my app is missing sometimes when my app auto starts upon user logon
I have a WPF app that I wrote in C# that creates a taskbar icon when it is launched. The only UI for the app that the user will usually see is the menu that appears if they right click on the taskbar icon. In other words, there is no "main window" that appears when launching the app. I have the app setup to automatically launch when my Windows user logs into Windows. Most of the time this works correctly in the sense that my app starts up and the taskbar icon gets successfully created. Once in a while though my app will start (i.e. I will see the .exe if I look using the Task Manager) but the taskbar icon will not be present at all and I do not receive an error message. When this happens there is no way to interact with the app and I have to kill it using Task Manager and then manually re-launch it at which point the taskbar icon will show up correctly. This failure of the taskbar icon to appear seems to only happen when the app is being launched automatically when the user logs in. In other words, if I instead log into my Windows user, then wait a minute or so, and then manually launch my app then the taskbar icon always successfully appears. Right now I am working around this problem by using the Task Scheduler to launch my app with a few minute delay, but I do not want to solve the problem this way. Solving the problem this way causes a few annoyances... #1 is that I have to intentionally over-estimate the amount to time to wait before launching my app so that I make sure to wait long enough but then I can't interact with my app right away... #2 is related in that if I run my app in a VM I have no realistic way to know how long it will take for Windows to become "ready" to create taskbar icons so I have to over-estimate the amount of time to wait before launching my app by even longer... #3 is that I have to remember to set this delay time for each computer/user I setup my app on.
3 replies
CC#
Created by PurplePeopleEater on 2/10/2024 in #help
Best logging library that supports working within "fire and forget" Task code
Does anyone have a recommendation for a logging library that they have personally used and like? Here is what I am looking for: - Needs to work with WPF desktop apps - Needs to be able to log to the filesystem - Needs to work with both typical application code AND code that is running as a Task in a "fire and forget" scenario. In other words, I have typical application code that needs to write to a "log.txt" file and then I have several places in that application code that "fire and forget" Tasks and those Tasks also need to write to that same "log.txt" file. By a "fire and forget" Task I mean that I am initiating several different Tasks throughout the code but not "awaiting" their completion. In other words, the Tasks fire asynchronously, there may be several of them running at the same time, and I don't wait for them to complete. - Easy to use My current situation: I have written my application but there are some final nagging bugs. Unfortunately those bugs manifest and cause the application to silently crash only after several days of running. This makes it very inconvenient to debug and I don't want to have an instance of VS running for days at a time just to debug this app. I currently have my own simple bespoke logging code but I think it doesn't handle the scenario where multiple threads/Tasks/etc are trying to log something at the same time even though I have tried to handle this scenario using a lock as follows:
36 replies
CC#
Created by PurplePeopleEater on 1/20/2024 in #help
Automatically logging all errors without using try-catch
Is there a way to automatically log all errors without using try-catch's. For example, I could write the following pseudo-code: try { func-that-could-cause-an-error-to-be-thrown(); } catch { logfunc(...); } by using try-catch, but I would like the logging to happen WITHOUT the try-catch and WITHOUT the logfunc(...) call. The reason I want this is because I don't want to add try-catches to the entire large code base - it would require many, many edits. The errors only seem to be thrown once every few hours to once every few days depending on the situation so it is problematic to always run the app via Visual Studio to see the errors because the errors happen so infrequently. When running my app in Visual Studio the debugger captures the errors for me and shows them... which is great except that I don't want to run my app in Visual Studio. That said, I want any error that would be shown in the debugger to be the errors that get logged. I am open to logging via file or via the Windows Events though I would prefer via file. I am open to using some built in logging feature of C#, or a logging library, etc. Thank you in advance for any help.
34 replies
CC#
Created by PurplePeopleEater on 1/28/2023 in #help
❔ Converting between various kinds of Bitmap objects without using Async code
Hi all, I need help doing the following: #1) Convert from an System.Drawing.Bitmap object to an Windows.Graphics.Imaging.SoftwareBitmap object without using asynchronous code (no "await" code, no "async" code, etc) and without needing to temporarily store the data on the filesystem as part of the process. The image data always originates as an System.Drawing.Bitmap object that is created by taking a screenshot users' screen or a rectangular portion of a monitor. I need to take that System.Drawing.Bitmap object and convert it to a Windows.Graphics.Imaging.SoftwareBitmap because I need to pass the image to a library that expects the image data to be a Windows.Graphics.Imaging.SoftwareBitmap. I do not have control over how that library works, I need to use it as-is. #2) Convert from an Windows.Graphics.Imaging.SoftwareBitmap object to an System.Drawing.Bitmap object without using asynchronous code (no "await" code, no "async" code, etc) and without needing to temporarily store the data on the filesystem as part of the process. I assume that I will need to be able to convert from an Windows.Graphics.Imaging.SoftwareBitmap object to an System.Drawing.Bitmap object because the System.Drawing.Bitmap object allows me to save it to the filesystem very easily while the Windows.Graphics.Imaging.SoftwareBitmap object does not allow saving to the filesystem easily. I want to store the Windows.Graphics.Imaging.SoftwareBitmap object on the filesystem as part of #3 below.
98 replies