2012-09-11 127 views
0

好吧,我有这个类显示一个“正在加载...”初始屏幕。当我在Initialize()上调用它时效果很好,但在Form_Load上不会。而不是在Form_Load的开始处显示,它显示在所有表格被填充后,然后挂在那里(没有锁定)。C#Winforms - 飞溅屏幕挂在Form_Load

class innerLoad 
{ 
    //Delegate for cross thread call to close 
    private delegate void CloseDelegate(); 


    //The type of form to be displayed as the splash screen. 
    private static frmLoading splashForm; 

    static public void ShowSplashScreen() 
    { 
     // Make sure it is only launched once. 
     if (splashForm != null) 
      return; 

     Thread thread = new Thread(new ThreadStart(innerLoad.ShowForm)); 
     thread.IsBackground = true; 
     //Thread.Sleep(100); 
     thread.SetApartmentState(ApartmentState.STA); 
     thread.Start(); 


    } 
    //volatile static public bool isOpen = false; 
    static private void ShowForm() 
    { 

     splashForm = new frmLoading(); 

     splashForm.ShowDialog(); 
     splashForm.Dispose(); 
    } 

    static public void CloseForm() 
    { 
     try 
     { 
      if (splashForm == null) 
       return; 
      splashForm.Invoke(new CloseDelegate(innerLoad.CloseFormInternal)); 
     } 
     catch 
     { 

     } 

    } 

    static private void CloseFormInternal() 
    { 
     splashForm.Close(); 
     splashForm = null; 
    } 


} 

这里是在Form_Load代码:

private void frmPayGen_Load(object sender, EventArgs e) 
    { 
     //th1 = new Thread(LoadingForm); 
     //th1.Start(); 
     //Thread.Sleep(500); 
     innerLoad.ShowSplashScreen(); 
     fill(); 
     innerLoad.CloseForm(); 

     //Thread.Sleep(500); 
    } 

我感谢您的帮助,我喜欢这个网站...帮助我很多:d

+1

[VS2010在64位版本的Windows上的WinForms应用程序中未显示未处理的异常消息](http://stackoverflow.com/questions/4933958/vs2010-does-not-show-unhandled-异常消息在winforms应用程序上) –

回答

0

如果在设置断点开始您的Form Load事件,并使用F11单步执行,您最终会看到此异常:

Error

表单加载事件中的异常基本上被忽略。如果抛出异常,则抛出异常的行之后没有任何东西运行,但Windows窗体也不会崩溃。拿走这行代码应该让事情按照你的意愿工作。

+0

我没有删除那个......“DISPOSE”只是一个测试,我忘了在这里删除...这并不能解决问题.. 。所以我只是在负载上显示表单本身,而不是使用类和线程来使其工作。感谢您的帮助! – user799920

+0

你只是想显示一个标准的初始屏幕? – JMK

+0

如果按照标准来看,在申请开始之前,答案是否定的。我正在尝试显示“加载表单”或“保存屏幕”。我知道我应该用一个Background Worker来代替,但是自从Thread.Abort()在各处引发异常之后,我开始采用不同的方式:D – user799920