2013-06-03 38 views
1

我有,我不能用我自己解決一些奇怪的問題......我一直在使用線程創建的啓動畫面(Form3〜閃屏),應用程序獲取到部分閃屏不能正常關閉

後不知何故

thread.Abort();

(實際上殺死線程)啓動畫面停留在屏幕上,直到我移動上面的鼠標,或點擊它的地方,對其他形式(如Form1中)......我已經變成,因爲,這更混亂當我運行應用程序時,不會發生在VS中。在啓動畫面是正常關閉......,它只是發生在已編譯的.exe Program.cs的

 namespace ICAMReports 
{ 
    static class Program 
    { 

     [STAThread] 
     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      Application.Run(new Form1()); 
     } 
    } 
} 

SplashScreen.cs

namespace ICAMReports 
{ 
    public partial class SplashScreen : Form 
    { 
     public SplashScreen() 
     { 
      InitializeComponent(); 
     } 

     private void timer1_Tick(object sender, EventArgs e) 
     { 
      progressBar1.Increment(1); 
      if (progressBar1.Value == 100) 
      { 
       timer1.Stop(); 
      } 
     } 
    } 
} 

Form1.cs的

namespace ICAMReports 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      Thread th = new Thread(new ThreadStart(splashScreen)); 
      th.Start(); 
      Thread.Sleep(3000); 
      th.Abort(); 
     } 
     public void splashScreen() 
     { 
      Application.Run(new SplashScreen()); 
     } 
     //this where the rest of code is placed.... 
    } 
} 

任何線索,爲什麼會發生這種情況或如何解決這個問題?

截圖:Splashscreen not closing after Form1 is loaded...

+0

我不知道你怎麼能有'thread.Abort',並在同一個問題 – Sayse

+0

Thread.Abort的其實是th.Abort「正常關閉」();在Form1.cs – dovla091

+0

據我瞭解; Thread.sleep代碼(3000); - >停止當前線程,th.Abort(); - >殺死線程(因此關閉SplashScreen窗體...)糾正我,如果我錯了... – dovla091

回答

1

MSDN說有關Thread.Abort的, 「調用此方法通常會終止線程」

有無數種方法可以在不使用thread.Abort的情況下關閉Splash Screen。

下面是一個這樣的方式來完成它看起來你試圖做的事情。

SplashScreen.cs

namespace ICAMReports 
{ 
public partial class SplashScreen : Form 
{ 
    ManualResetEventSlim splashDone; 
    public SplashScreen(ManualResetEventSlim SplashDone) 
    { 
    splashDone=SplashDone; 
     InitializeComponent(); 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     progressBar1.Increment(1); 
     if (progressBar1.Value == 100) 
     { 
      splashDone.Set(); 
      this.Close(); 
     } } } } 

Form1.cs的

 namespace ICAMReports 
{ 
public partial class Form1 : Form 
{ 
    ManualResetEventSlim splashDone = new ManualResetEventSlim(false); 
    public Form1() 
    { 
     InitializeComponent(); 
     Thread th = new Thread(new ThreadStart(splashScreen)); 
     th.Start(); 
     splashDone.Wait(); 
    } 
    public void splashScreen() 
    { 
     Application.Run(new SplashScreen(splashDone)); 
    } 
    //this where the rest of code is placed.... 
    } 
    } 

splashDone.Wait()將完成你試圖用睡眠(做),而是你應該使用同樣的事情你在Splash Screen中加載Bar以告訴你何時結束線程。實際上,在這種情況下,啓動屏幕在單獨的線程上確實沒有任何意義,因爲睡眠/等待會暫停主窗體加載任何內容,直到啓動屏幕完成。假設你的Form1中有資源密集型的東西,你想在Splash Screen分散注意力的時候加載。你會做這樣的事情,而不只是暫停在Form1完全(因爲使用一個單獨的線程的整個的一點是,他們都同時運行。

SplashScreen.cs

namespace ICAMReports 
{ 
public partial class SplashScreen : Form 
{ 
    Form parent; 
    delegate void show(); 
    public SplashScreen(Form Parent) 
    { 
    parent=Parent; 
     InitializeComponent(); 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     progressBar1.Increment(1); 
     if (progressBar1.Value == 100) 
     { 
      parent.Invoke(new show(()=>{parent.Opacity=100;})); 
      this.Close(); 
     } } } } 

Form1.cs的

 namespace ICAMReports 
{ 
public partial class Form1 : Form 
{ 

    public Form1() 
    { 
     InitializeComponent(); 
     Thread th = new Thread(new ThreadStart(splashScreen)); 
     th.Start(); 
     this.Opacity=0; 
    } 
    public void splashScreen() 
    { 
     Application.Run(new SplashScreen(this)); 
    } 
    //this where the rest of code is placed.... 
    } 
    } 

編輯:應對加載條動畫

加載條將移動,並根據贏的版本和設置看起來不同你正在使用的是dows。您不能使用Thread.Sleep來讓加載欄跟上,因爲它會暫停加載欄1動畫。你需要給你的加載欄約10%趕上(根據需要進行調整),這應該可以解決加載欄動畫的問題。

int i = 0; 
    private void timer1_Tick(object sender, EventArgs e) 
    { 
     if(i++<100)progressBar1.Value++; 
     if (i == 110) 
     { 
     splashDone.Set(); 
     this.Close(); 
     } 
    } 
+0

第一個代碼工作,它固定關閉初始屏幕(表單),但由於某種原因,progressbar1不會到100,它會在大約75%的時間關閉窗體... – dovla091

+0

您需要讓定時器超過幾個刻度以允許動畫完成更新ING。我編輯我的帖子上面給你一個可能的解決方案。只需創建一個單獨的變量,該變量可以高於最大progressBar1值並在該值等於110或您希望確保加載欄動畫結束的任何時間間隔時退出。我測試了上面的110以上的設置,它給動畫足夠的時間來完成。如果你需要它持續更長的時間,如果你想讓它快一點105,那麼只是勉強達到目的。 – CodeCamper

+0

感謝CodeCamper,即使我不明白爲什麼以前的代碼不工作...,爲什麼我需要設置值超過100 ... – dovla091