I have a program I wrote, everything works fine, it connects to a database, and to keep that database connection alive/secure, I have a timer that, after 60 minutes, goes back to the login screen for the program.<\/p>\n
This was all working fine except one thing: I couldn’t get the timer to properly reset to 0, it would reset to 0 right away, even if I logged in or not, so if the timer hit 60, logged me out, and then I logged back in 57 minutes later, it would only have 3 minutes left on the timer.<\/p>\n
So to fix this, I made the timer its own class instead of having it run right on one of the forms itself. I think this works as far as properly resetting the timer, because I have the timer reset to 0 when I click the login button, so all is well there.<\/p>\n
HOWEVER, now my form hiding/showing is not working correctly.<\/p>\n
I have tried hiding/showing the form from the Timer class, it keeps Form2 open and then opens Form1 on top of it. Then when I log back in, I have 2 Form2’s open.<\/p>\n
Then I made a public void in Form2 that does this:<\/p>\n
public void hideForm()\n {\n \n MessageBox.Show(\"Your session has timed out, please log in again\");\n Form1 frm1 = new Form1();\n frm1.Show();\n this.Close();\n }\n<\/code><\/pre>\nWhen the timer hits 60 minutes, it calls the hideForm() and it pops up the messagebox just fine, and it loads Form1 just fine.<\/p>\n
But it still will not close the current form.<\/p>\n
Does anyone know why this is happening? It worked fine before I separated he timer into its own class.<\/p>\n
Any help is much appreciated, thanks!<\/p>","upvoteCount":3,"answerCount":4,"datePublished":"2016-03-24T19:45:08.000Z","author":{"@type":"Person","name":"kylewisdom2","url":"https://community.spiceworks.com/u/kylewisdom2"},"acceptedAnswer":{"@type":"Answer","text":"
Okay, I ended up getting it. I took it out of the class and put it back in the form I was having everything work out of.<\/p>\n
public void timer_Tick(object sender, EventArgs e)\n {\n logintimerCount += 1;\n intLabel.Text = logintimerCount.ToString();\n\n if (logintimerCount == 60)\n {\n\n MessageBox.Show(\"Your session has timed out, please log in again\");\n myTimer.Stop();\n this.Hide();\n Form1 frm1 = new Form1();\n frm1.Show();\n \n \n \n\n }\n }\n<\/code><\/pre>\nI did that for the timer and its ticks. Had it stop, then hide the form and load the login screen back up.<\/p>\n
And then, during form load, it resets the counter to 0.<\/p>\n
Played around with it enough to get my head on straight and get it all organized and put in the right places for it to work. Thanks everyone.<\/p>","upvoteCount":0,"datePublished":"2016-03-25T21:12:06.000Z","url":"https://community.spiceworks.com/t/c-winform-timer-class-need-to-hide-one-form-load-another/483677/4","author":{"@type":"Person","name":"kylewisdom2","url":"https://community.spiceworks.com/u/kylewisdom2"}},"suggestedAnswer":[{"@type":"Answer","text":"
I have a program I wrote, everything works fine, it connects to a database, and to keep that database connection alive/secure, I have a timer that, after 60 minutes, goes back to the login screen for the program.<\/p>\n
This was all working fine except one thing: I couldn’t get the timer to properly reset to 0, it would reset to 0 right away, even if I logged in or not, so if the timer hit 60, logged me out, and then I logged back in 57 minutes later, it would only have 3 minutes left on the timer.<\/p>\n
So to fix this, I made the timer its own class instead of having it run right on one of the forms itself. I think this works as far as properly resetting the timer, because I have the timer reset to 0 when I click the login button, so all is well there.<\/p>\n
HOWEVER, now my form hiding/showing is not working correctly.<\/p>\n
I have tried hiding/showing the form from the Timer class, it keeps Form2 open and then opens Form1 on top of it. Then when I log back in, I have 2 Form2’s open.<\/p>\n
Then I made a public void in Form2 that does this:<\/p>\n
public void hideForm()\n {\n \n MessageBox.Show(\"Your session has timed out, please log in again\");\n Form1 frm1 = new Form1();\n frm1.Show();\n this.Close();\n }\n<\/code><\/pre>\nWhen the timer hits 60 minutes, it calls the hideForm() and it pops up the messagebox just fine, and it loads Form1 just fine.<\/p>\n
But it still will not close the current form.<\/p>\n
Does anyone know why this is happening? It worked fine before I separated he timer into its own class.<\/p>\n
Any help is much appreciated, thanks!<\/p>","upvoteCount":3,"datePublished":"2016-03-24T19:45:08.000Z","url":"https://community.spiceworks.com/t/c-winform-timer-class-need-to-hide-one-form-load-another/483677/1","author":{"@type":"Person","name":"kylewisdom2","url":"https://community.spiceworks.com/u/kylewisdom2"}},{"@type":"Answer","text":"
A timer is running in a thread other than main event thread (GUI thread), all manipulation of UI components should be done in main event thread. check out things like BeginInvoke to get familiar with C# GUI programing , which is very similar to Java Swing or other similar UI frame work.<\/p>","upvoteCount":0,"datePublished":"2016-03-24T20:10:13.000Z","url":"https://community.spiceworks.com/t/c-winform-timer-class-need-to-hide-one-form-load-another/483677/2","author":{"@type":"Person","name":"lesleixu3531","url":"https://community.spiceworks.com/u/lesleixu3531"}},{"@type":"Answer","text":"
I made a C# project that does what you want, but Spiceworks will not allow posting a ZIP file. PM me your email address if you want it.<\/p>\n
Here is the gist of it:<\/p>\n
static void Main()\n {\n Application.EnableVisualStyles();\n Application.SetCompatibleTextRenderingDefault(false);\n GlobalVariables.Timer = new Timer();\n GlobalVariables.Timer.Interval = 60 * 1000; //Minute\n GlobalVariables.Timer.Interval = 60;//test one second\n GlobalVariables.Timer.Tick += new EventHandler(Timer_Tick);\n Application.Run(new frmLogin());\n }\n\n static void Timer_Tick(object sender, EventArgs e)\n {\n \n GlobalVariables.OpenTime++;\n if (GlobalVariables.OpenTime > 2)\n {\n ToggleForm();\n }\n }\n\n public static void ToggleForm()\n {\n //Form main = Application.OpenForms[\"Form1\"];\n Form main = GlobalVariables.MainForm;\n if (main != null)\n {\n main.Hide();\n }\n Form login = Application.OpenForms[\"frmLogin\"];\n if (login != null)\n {\n login.Show();\n }\n else\n {\n throw new Exception(\"Cant find login form\");\n }\n GlobalVariables.Timer.Stop();\n }\n }\n<\/code><\/pre>\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Text;\n\nnamespace WinFormsTimerTest\n{\n public static class GlobalVariables\n {\n public static System.Windows.Forms.Timer Timer\n {\n get;\n set;\n }\n\n public static int OpenTime\n {\n get;\n set;\n }\n\n public static System.Windows.Forms.Form MainForm\n {\n get;\n set;\n }\n\n \n }\n}\n\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2016-03-24T23:49:07.000Z","url":"https://community.spiceworks.com/t/c-winform-timer-class-need-to-hide-one-form-load-another/483677/3","author":{"@type":"Person","name":"williamsurritte3886","url":"https://community.spiceworks.com/u/williamsurritte3886"}}]}}