一个连接主窗体与Splash窗体的Splash类

初步实现了2个窗体的连接。写为泛型,方便替换Splash窗体。当前的Splash类没有保证跨线程方法调用的线程安全,需要使用者在Splash窗体的代码中保证线程安全的调用,这也是我正在想办法解决的问题。

放出包含注释的代码供批判

Splash.cs

///   <summary>
///  A bridge class that connect a main form with a splash form
///   </summary>
///   <typeparam name="TForm"> Type of the splash form </typeparam>
///   <example>
///  In the entry method of the application:
///   <code>
///      Splash <<c> SplashForm </c> >.Show();
///      MainForm form = new MainForm();
///      Application.Run(form);
///   </code>
///  And terminate the splash form in the code of the main form:
///   <code>
///      Splash <<c> SplashForm </c> >.Close();
///   </code>
///   </example>
///   <remarks> TO-DO: Ensure thread-safety of cross-thread calls in this
class </ remarks >
public   class  Splash < TForm >   where  TForm : Form,  new ()
{
    
#region  Fields

    
///   <summary>
    
///  The splash form
    
///   </summary>
     private   static  TForm _splashForm  =   null ;

    
///   <summary>
    
///  The splash working thread
    
///   </summary>
     private   static  Thread _splashThread  =   null ;

    
#endregion

    
#region  Properties

    
///   <value>
    
///  Get the reference of the splash form
    
///   </value>
    
///   <remarks> Programmers must warrant the thread-safety of cross-thread calls </ remarks >
    
public   static  TForm SplashForm
    {
        
get  {  return  _splashForm; }
    }

    
#endregion

    
#region  Public Methods

    
///   <summary>
    
///  Show the splash
    
///   </summary>
     public   static   void  Show()
    {
        
if  (_splashThread  !=   null )
            
return ;

        _splashThread 
=   new  Thread(
            
new  ThreadStart(()  =>
            {
                _splashForm 
=   new  TForm();
                Application.Run(_splashForm);
            }));

        _splashThread.IsBackground 
=   true ;
        _splashThread.SetApartmentState(ApartmentState.STA);
        _splashThread.Start();
    }

    
///   <summary>
    
///  Close the splash form
    
///   </summary>
    
///   <remarks> This method is called from the main form </remarks>
     public   static   void  Close()
    {
        
if  (_splashThread  ==   null return ;
        
if  (_splashForm  ==   null return ;

        
try
        {
            _splashForm.Invoke(
new  MethodInvoker(_splashForm.Close));
        }
        
catch  (Exception ex) { }

        _splashThread 
=   null ;
        _splashForm 
=   null ;
    }

    
#endregion
}


新建一个主窗体Form1,Splash窗体Form2,在Form2中添加一个Property供Form1修改,如下:

     public   string  Status
    {
        
set
        {
            ChangeStatus(value);
        }
    }

    
public   void  ChangeStatus( string  status)
    {
        
if  ( this .InvokeRequired)
            
this .Invoke( new  Kwan.Delegates.Action < string > ( this .ChangeStatus)
                                ,
new   object [] { status });
        
else
            
this .Text  =  status;
    }


其中使用了一个我自行定义的委托类型,该委托定义如下:

     public   delegate   void  Action < T > (T val);


最后在Form1的Load处理方法中写上:

     string  str  =   " Loading " ;
    Splash
< Form2 > .SplashForm.Status  =  str;
    System.Threading.Thread.Sleep(
2000 );
    Splash
< Form2 > .Close();


通过睡眠线程来模拟程序的初始化载入。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值