Click here to Skip to main content
15,886,791 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello,

I created a SplasScreen class to show "processing..."(or any other) gif on a little form. (In this site, I read an article about this but i can't remember who wrote it) When i call the ShowSplash method, it shows a splash on the screen. And i use the CloseSplash method to close it. Usually, the CloseSplash method works, but sometimes it doesn't. I don't understand what can be the reason of this. And those are the codes:

C#
public class SplashScreen : Form
   {
       public Bitmap SImage {get; set;}
       Thread t;

       public void Flash()
       {
           PictureBox pb = new PictureBox();
           pb.SizeMode = PictureBoxSizeMode.AutoSize;
           pb.Image = SImage;
           this.Width = pb.Width;
           this.Height = pb.Height;

           this.Controls.Add(pb);
           this.TopMost = true;
           this.StartPosition = FormStartPosition.CenterScreen;
           this.FormBorderStyle = FormBorderStyle.None;
           Application.Run(this);
       }


       public void sClose()
       {
           this.Close();
       }
       public void ShowSplash()
       {
           t = new Thread(new ThreadStart(Flash));
           t.IsBackground = true;
           t.SetApartmentState(ApartmentState.STA);
           t.Start();
       }
       public void CloseSplash()
       {
           if (this.InvokeRequired)
           {
               this.Invoke(new MethodInvoker(sClose));
           }

       }

   }


And an example for using it:
C#
SplashScreen sc = new SplashScreen();
           sc.SImage = (Bitmap)Image.FromFile("processing.gif");
           sc.ShowSplash();

           //... codes codes codes...

           sc.CloseSplash();


Regards
Posted

1 solution

You have to keep a reference to your running thread, the local variable t have to be declared at the class level (maybe with a better name) then when you want to close the thread you have to invoke:

C#
if(t!= null && t.IsAlive()) //To make shore that the thread is running!
       t.Abort();
 
Share this answer
 
v2
Comments
Çağrı Daşkın 26-Mar-14 4:57am    
Thank you very much Raul, this was really fast. I solved this problem with your way, but with a little change. I made a little search about .Abort and i saw some programmers say "never use abort". so i wrote the code:
if (t != null && t.IsAlive)
{
t.Interrupt();
this.Close();
}
Raul Iloc 26-Mar-14 5:05am    
OK, as you want, but in my projects when I relay want to kill immediately (abort) a thread I am using Abort. So If my solution helped you, you could accept it!
Çağrı Daşkın 26-Mar-14 5:13am    
I already accepted your solution Raul. And I said your solution really worked. I just confused about the difference between interrupt and abort methods. And what can be dangerous about "abort". I hoped you could say something about it.
Raul Iloc 26-Mar-14 5:21am    
Abort is not dangerous, it has only the problem that it generate an ThreadAbortException exception, but sometime is what that we relay need in our code. But also Interrupt() generate also exception and from my point of view is not better then Abort because it works only if your thread is not in a wait, sleep, or join state!
Çağrı Daşkın 26-Mar-14 5:36am    
OK, thank you for your answers. You really helped me and solved my problem. Regards.

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