Repeatedly running thread

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mbewers1
    New Member
    • Feb 2009
    • 68

    #1

    Repeatedly running thread

    I have a problem with this piece of code, which loads a splash screen and keeps it open for 5 seconds before loading another form.

    It strangely seems to be repeatedly running a thread once the method has executed and showing an unlimited number of splash screens but no main form. I've tried inserting mb.Suspend() and mb.Abort() after requesting the ShowDialog() method to be run for my main form, Form1.

    How can I stop the thread from repeatedly running?

    Cheers
    Matt

    Code:
    protected override void OnLoad(EventArgs e)
    {
                SplashIntro s = new SplashIntro();
    
                Thread mb = new Thread(new ThreadStart(StartSplash));
                mb.Start();
                Thread.Sleep(5000);
    
                bool authenticated = s.AuthenticateUser();
                if (s.AuthenticateUser() ? true : false)
                {
                    Close();
                    Form1 f1 = new Form1();
                    f1.Size = new Size(1060, 770);
                    f1.ShowDialog();
                }
    
                else
                {
                    Application.ExitThread();
                }
    
                s.Close();
                s.Dispose();
                s = null;
    }
  • balame2004
    New Member
    • Mar 2008
    • 142

    #2
    Post StartSplash method code.

    Comment

    • mbewers1
      New Member
      • Feb 2009
      • 68

      #3
      StartSplash method

      As promised...

      Code:
      public static void StartSplash()
      {
              Application.Run(new SplashIntro());
      }
      In my Program.cs, I call Application.Run (new Form1()), which is what I was told to do in another thread:



      Thanks
      Matt

      Comment

      • mbewers1
        New Member
        • Feb 2009
        • 68

        #4
        Almost solved!

        Hey, it's OK, I've got the screen working the way I want to.

        Only thing is, the splash screen does not close when the main Windows Form appears. How can I change my code below to do this without using Application.Exi t() to shut down the whole thing?

        Code:
        private void Form1_Load(object sender, EventArgs e)
                {
                    SplashIntro s = new SplashIntro();
                    Thread mb = new Thread(new ThreadStart(StartSplash));
                    mb.Start();
                    Thread.Sleep(5000);
        
                    bool authenticated = s.AuthenticateUser();
                    if (authenticated)
                    {
                        s.Close();
                        s.Dispose();
                        Show();
                    }
                    LoadOtherTabs();
                }

        Comment

        • IanWright
          New Member
          • Jan 2008
          • 179

          #5
          Really you want to change you're layout. I don't think the flow you're taking is a good one. The problem you have at the moment is because you're StartSplash method creates a new SplashIntro. So when you call s.Close() that isn't the same SplashIntro.

          I think a better way to do this might be something like:
          Code:
          private void Form1_Load(object sender, EventArgs e)
          {
                Thread splashThread = new Thread(new ThreadStart(StartSplash));
                splashThread.Start();
          
                // Do any work here. E.g. you could authenticate
          
                // Wait for thread to finish
                splashTread.Join();
          
                LoadOtherTabs();
          }
          
          private void StartSplash()
          {
                SplashIntro s = new SplashIntro();
                s.ShowDialog();
          }
          Then on the SplashIntro itself

          Code:
          public class StartSplash
          {
                public StartSplash()
                {
                      // Add a System.Timer Here set to 5s tick.
                      // Add an Event to trigger when the Timer ticks
                }
          
                private void OnTimerTick(object sender, EventArgs e)
                {
                   this.Close();
                }
          }

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Application.Run () should only be used once.
            IanWright's suggestion of correcting your layout is also a good idea.

            Comment

            Working...