Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# execute code after application.run()

Tags:

c#

winforms

I have a problem similar to this one: How can I execute code after my form starts?

But the solution there won't work for me because I am not running a form, I'm running a single custom control, which is a tray icon that monitors things. (Similar to the Icon Dropbox has, where that is the only interface the user has with the program)

What should I do to run code when the control is created? (which has to be after the message pump starts)

like image 369
Drew Avatar asked Mar 06 '11 09:03

Drew


2 Answers

I think you're looking for the Application.Idle event.

Occurs when the application finishes processing and is about to enter the idle state.

E.g.

Application.Idle += delegate { Console.WriteLine(Application.MessageLoop); };

// Output: true 
Application.Run();
like image 195
Ani Avatar answered Sep 24 '22 19:09

Ani


First option would be to post a windows message to yourself. That way it will not be dispatched until your thread starts pumping messages. A second option is to hook into the Application.Idle event which is fired when the message queue is empty. Your third option would be to set and run a Timer for a small duration and hook into the Tick event for when it expires. Fourth and the last for now is to fire a delegate asynchronously as they use the message queue as the mechanism for being fired.

like image 23
Phil Wright Avatar answered Sep 23 '22 19:09

Phil Wright