下面的代碼在單獨的線程上啓動一個「啓動畫面」,而您的應用程序(在我的示例中,它被稱爲MainForm())加載或初始化。首先在你的「main()」方法中(在你的program.cs文件中,除非你重命名了它),你應該顯示你的啓動畫面。這將是一個WinForm或WPF表單,您希望在啓動時向用戶顯示。這是主要推出()如下:
[STAThread]
static void Main()
{
// Splash screen, which is terminated in MainForm.
SplashForm.ShowSplash();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Run UserCost.
Application.Run(new MainForm());
}
在你的閃屏的代碼,你需要像下面這樣:
public partial class SplashForm : Form
{
// Thredding.
private static Thread _splashThread;
private static SplashForm _splashForm;
public SplashForm()
{
InitializeComponent();
}
// Show the Splash Screen (Loading...)
public static void ShowSplash()
{
if (_splashThread == null)
{
// show the form in a new thread
_splashThread = new Thread(new ThreadStart(DoShowSplash));
_splashThread.IsBackground = true;
_splashThread.Start();
}
}
// Called by the thread
private static void DoShowSplash()
{
if (_splashForm == null)
_splashForm = new SplashForm();
// create a new message pump on this thread (started from ShowSplash)
Application.Run(_splashForm);
}
// Close the splash (Loading...) screen
public static void CloseSplash()
{
// Need to call on the thread that launched this splash
if (_splashForm.InvokeRequired)
_splashForm.Invoke(new MethodInvoker(CloseSplash));
else
Application.ExitThread();
}
}
這將啓動飛濺的形式在一個單獨的後臺線程允許你繼續同時呈現您的主應用程序。要顯示關於加載的消息,您必須從主UI線程獲取信息,或者以純粹的審美性質工作。爲了完成並關閉閃屏下來的時候你的應用程序已被初始化你把默認的構造函數中以下(你可以重載的構造函數,如果你想):
上面的代碼應該是比較容易跟隨。
我希望這會有所幫助。
你爲什麼不只是刪除的睡眠?這是什麼阻止用戶界面。 – usr 2012-08-04 10:32:38
我已經嘗試通過刪除睡眠,循環然後阻止用戶界面 – greatmajestics 2012-08-04 10:37:32
好吧,當然也刪除循環;-)解除屏蔽UI線程需要從您的事件處理程序返回。 – usr 2012-08-04 10:38:44