2013-06-03 40 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