一个连接主窗体与Splash窗体的Splash类
来源:互联网 发布:佐菲奥特曼act淘宝 编辑:程序博客网 时间:2024/05/17 20:23
初步实现了2个窗体的连接。写为泛型,方便替换Splash窗体。当前的Splash类没有保证跨线程方法调用的线程安全,需要使用者在Splash窗体的代码中保证线程安全的调用,这也是我正在想办法解决的问题。
放出包含注释的代码供批判
Splash.cs
新建一个主窗体Form1,Splash窗体Form2,在Form2中添加一个Property供Form1修改,如下:
其中使用了一个我自行定义的委托类型,该委托定义如下:
最后在Form1的Load处理方法中写上:
通过睡眠线程来模拟程序的初始化载入。
放出包含注释的代码供批判
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
}
/// 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;
}
{
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();
Splash<Form2>.SplashForm.Status = str;
System.Threading.Thread.Sleep(2000);
Splash<Form2>.Close();
通过睡眠线程来模拟程序的初始化载入。
- 一个连接主窗体与Splash窗体的Splash类
- 如何做一个这样的splash窗体?
- Splash窗体的完美实现
- 一个Splash Screen控件 (闪屏窗体 C#)
- 程序启动欢迎窗体(Splash) 制作方法
- 使用Delphi 7实现Splash窗体
- [C#]增强响应性,用加载窗体(Splash)来载入主窗体
- [C#]增强响应性,用加载窗体(Splash)来载入主窗体
- [C#]增强响应性,用加载窗体(Splash)来载入主窗体
- splash
- splash
- Splash
- 与splash详解相关的
- [界面]程序启动欢迎窗体(Splash) 制作方法(附原码)
- [界面]程序启动欢迎窗体(Splash) 制作方法(附原码)
- VB.NET :Splash类与只许一个实例
- 程序启动的Splash screen与跳转
- Splash.cs的改进
- 老生常谈 TDD with RSpec
- WINCE6.0 安装 下载
- c#下cookie使用实例
- web开发的珍藏代码积累
- Calling Web Services Asynchronously
- 一个连接主窗体与Splash窗体的Splash类
- gridview设置DataFormatString
- GPS专业词语缩写
- [整合]eclipse不自动编译的解决办法
- 点石互动--highdiy之:如果没有"NoFollow"
- 样式应用的优先级别
- 该说些什么好呢?
- OllyDBG 入门系列(一)-认识OllyDBG
- 如何使用已有的主题