简体   繁体   中英

NotifyIcon's ContextMenu not working sometimes

I'm developing a Windows application to run in the background (for a long running process).

Application shows process status by a NotifyIcon tool tip.

I added a ContextMenu control to the NotifyIcon tool tip control. Whenever the application is idle (not doing any work), the context menu works fine, but during the running of a process the context menu hangs.

All processes are running in different threads. Can anyone tell me how I can make a smooth context menu for a long running background process?

The exact same rules apply to a program that has a NotifyIcon as a program that uses Window or Form to show a user interface. NotifyIcon sends notifications to the main thread of the process, the one on which you called Application.Run(). And if that main thread is busy with other tasks, like "running of a process" then the NotifyIcon goes catatonic. It is trying to send the notification but your main thread isn't listening since it is busy executing that "process".

And you'll have to solve this the exact same way as in a regular gui program, you have to run the processing code on a worker thread. Use the standard .NET solutions, like BackgroundWorker, Task or the async/await keywords. Or spin your own with Thread or ThreadPool.

If you are already doing this, it isn't clear from the question, then watch out for, say, a BackgroundWorker that calls ReportProgress too often and still gets the main thread bogged down.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM