Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8613239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:49:31+00:00 2026-06-12T04:49:31+00:00

Lets assume I have two forms Form1 and Form2 . I need to show

  • 0

Lets assume I have two forms Form1 and Form2. I need to show Form1 for a while of time (say 5 minutes) and then hide it and show Form2 for another 5 minutes and then hide Form2 and then show Form1 and so on …

How can I do that in C++/CLI or C#?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:49:33+00:00Added an answer on June 12, 2026 at 4:49 am

    Off the top of my head, I’d probably do something like this (C# example provided)

      using System;
      using System.Timers;
      using System.Windows.Forms;
    
    
      public class SwitchForms : IDisposable
      {
        private Form Form1 { get; set; }
    
        private Form Form2 { get; set; }
    
        private System.Timers.Timer VisibilityTimer { get; set; }
    
    
        public SwitchForms(Form form1, Form form2, double hideMinutes)
        {
            Form1 = form1;
            Form2 = form2;
    
            VisibilityTimer = new System.Timers.Timer(hideMinutes * 60.0 * 1000.0);
            VisibilityTimer.Elapsed += VisibilityTimer_Elapsed;
            VisibilityTimer.Enabled = true;
    
            //Could also consider subscribing to the close events of both forms here to disable the Timer
    
            Form1.Invoke(new MethodInvoker(() => Form1.Show()));
            Form2.Invoke(new MethodInvoker(() => Form2.Hide()));
    
        }
    
        private void VisibilityTimer_Elapsed(object source, ElapsedEventArgs e)
        {
          if(Form1 == null || Form2 == null)
          {
            VisibilityTimer.Enabled = false;
            return;
          }
    
          if (Form1.IsDisposed || Form2.IsDisposed)
          {
            VisibilityTimer.Enabled = false;
            Form1 = null;
            Form2 = null;
          }
          else
          {
            Form1.Invoke(new MethodInvoker(() => { if(Form1.Visible) { Form1.Hide(); } else {Form1.Show();} }));
            Form2.Invoke(new MethodInvoker(() => { if (Form2.Visible) { Form2.Hide(); } else { Form2.Show(); } }));
          }
        }
    
        public void Dispose()
        {
          VisibilityTimer.Enabled = false;
          VisibilityTimer.Dispose();
        }
    
    
    
      }
    
    }
    

    In usage, when you create Form1 and Form2, you can hold a reference to this in one of the fields of Form1/Form2 and when you close Form1/Form2, just dispose it.

    e.g. something like:

      public partial class Form1 : Form
      {
        private Form OtherForm { get; set; }
    
        private SwitchForms FormSwitcher { get; set; }
    
        public Form1()
        {
          InitializeComponent();
    
    
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
          OtherForm = new Form();
          OtherForm.Text = "Other Form";
          OtherForm.Show();
          OtherForm.Closed += OtherForm_Closed;
          FormSwitcher = new SwitchForms(this, OtherForm, 5.0);
        }
    
        void OtherForm_Closed(object sender, EventArgs e)
        {
          Show();
          //Application.Exit(); //Could also consider closing the app if the other window is closed
        }
    
      }
    

    There are a couple of things to note:

    1. Use of Form.Invoke
      As the timer is essentially running in a separate thread, it’s important to use the Invoke methods of the forms or you’ll get a cross thread exception.

    2. Potential Race Condition
      If the time interval is really small, the elapsed event can fire multiple times. Although this would be a silly thing to do from a user experience point of view, the critical issue is if the Timer_Elapsed event is called quickly in succession after Form2 has been closed – it’s possible to get a null reference exception. The same could potentially happen if the app is shutting down while the Timer_Elapsed event is called – the best thing to do here is to Dispose the SwitchForms class (or add a Stop method) so you can get rid of the timer before the app closes.

    I could then also put SwitchForms in my startup class for example, although I’d have to be extra careful to then check that Form1 and Form2 haven’t already been disposed and so on.

    You could also embed the code of SwitchForms into either Form1 or Form2 for simplicity. You’ll also need to think about what happens if either Form1 or Form2 are closed independently as otherwise the timed event will throw an exception.

    In my example, I simply reshow Form1 if Form2 is closed, and if Form1 is closed, the app exits.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets assume I have two hashes. One of them contains a set of data
I have a city table that has two columns from_city to_city now lets assume
Let's say we have two objects. Furthermore, let's assume that they really have no
lets say, i have two tables, one for object records and one for activity
Lets assume we have two string arrays string[] array1 = {aa, bb, cc}; string[]
lets assume that we have two operation contracts defined on wcf service, sync and
Lets say I have two databases: one for students and one for classes. I
lets assume we have this table called visits and it has two fields id
Let's assume we have two arrays of the same size - A and B
Let's assume I have two gmdistibution models that i obtained using modeldata1=gmdistribution.fit(data1,1); modeldata2=gmdistribution.fit(data2,1); Now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.