讓我們假設你有以下的事情做形式負載:
void LoadStartupItemsAndDataBlahBlah(object sender, DoWorkEventArgs e)
{
int s = 0;
for (int i = 0; i < 1000; i++)
for (int j = 0; j < 1000; j++)
for (int k = 0; k < 1000; k++)
s++;
MessageBox.Show(s.ToString());
}
,如果你調用形式負載這種方法
private void Form5_Load(object sender, EventArgs e)
{
Stopwatch t = new Stopwatch();
t.Start();
LoadStartupItemsAndDataBlahBlah(null,null);
t.Stop();
this.Text = string.Format("[{0}] mili-seconds", t.ElapsedMilliseconds);
}
表單約5秒鐘後運行(5000毫秒)以及消息框1000000000
但如果您在後臺運行該方法
private void Form5_Load(object sender, EventArgs e)
{
BackgroundWorker bg = new BackgroundWorker();
bg.DoWork += new DoWorkEventHandler(LoadStartupItemsAndDataBlahBlah);
Stopwatch t = new Stopwatch();
t.Start();
bg.RunWorkerAsync();
t.Stop();
this.Text = string.Format("[{0}] mili-seconds", t.ElapsedMilliseconds);
}
您的形式加載速度非常快,大約0-1毫秒,在5秒內消息框將到達並顯示它的價值1000000000
你打開這個表格每個應用程序運行一次?或者你想多次打開它? –