Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am running windows application. In that, based on condition my windows application will go to minimize automatically. For instance, i would like to minimize windows application automatically when current time reach 4P.M. How to minimize it by c# code. Note: only minimize not close my application.
Posted
Comments
[no name] 13-Sep-13 10:23am    
You would set the WindowState to minimized....

Your problem is the cross-thread operation. What happens in the event handler, can happen on other thread then the UI one.

You can use a different timer, for which this is not a problem: System.Windows.Forms.Timer:
http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx[^].

Be warned though: this timer is very bad, it guarantees no reasonable timing precision. For your purpose it could be good enough though, as you hardly require any serious timing precision.

In all other cases (threading and other timers), you should solve this cross-threading problem, which is a pretty simple thing. You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
Share this answer
 
An exemple :

C#
public Form1()
        {
            InitializeComponent();
            Timer CurrentTime = new Timer();
            CurrentTime.Tick += new EventHandler(CurrentTime_Tick);
            CurrentTime.Interval = 1000; //check every second
            CurrentTime.Start();
        }

        void CurrentTime_Tick(object sender, EventArgs e)
        {
            if (DateTime.Now.Hour == 16 /*add here more condition*/)
            {
                this.WindowState = FormWindowState.Minimized;
            }
        }


Force the windows to be minimized if current time is 4pm
 
Share this answer
 
If you want to keep track of time remaining aswell you could do something like this:
C#
private static DateTime endDate,startDate;
public DateTime StartDate
        {
            get
            {
                return startDate;
            }
            set
            {
                startDate = value;
            }
        }
        public DateTime EndDate
        {
            get
            {
                return endDate;
            }
            set
            {
                endDate = value;
            }
        }

public int DaysRemaining
        {
                get
                {
                    if (startDate != null && endDate != null)
                        return endDate.Subtract(startDate).Hours;//or days, you would need to change the rest of the code to use Hours and not Days.
                    else
                        return 0;
                }
                set
                {
                    endDate = startDate.Add(new TimeSpan(value, 0, 0));
                }
        }


Then check if the time remaining property is <= 0 and then do what Viswanathan Ramamoorthy said above with using the Form's WindowState.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900