探しているものを実現する簡単な方法は、OpenNETCFフレームワークの一部であるBackgroundWorkerクラスを使用することです。
これは基本的に、完全な.NETFrameworkからのSystem.ComponentModel.BackgroundWorkerクラスの機能的なコピーです。
次のように実行します。
- フォーム読み込みイベントでアニメーションを開始します
- backgroundworkerを開始します
- backgroundworkerが完了イベントを発生させるのを待ちます。
以下は、MSDNのbackgroundworkerクラスのドキュメントを基にした例で、正しい方向を示している可能性があります。
public Form1()
{
InitializeComponent();
InitializeBackgroundWorker();
}
// Set up the BackgroundWorker object by
// attaching event handlers.
private void InitializeBackgroundWorker()
{
backgroundWorker1.DoWork +=
new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(
backgroundWorker1_RunWorkerCompleted);
backgroundWorker1.ProgressChanged +=
new ProgressChangedEventHandler(
backgroundWorker1_ProgressChanged);
}
protected override void OnLoad(EventArgs e)
{
if (backgroundWorker1.IsBusy != true)
{
// Start the asynchronous operation.
// start your animation here!
backgroundWorker1.RunWorkerAsync();
}
}
// This event handler is where the time-consuming work is done.
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
//this is taken from the msdn example, but you would fill
//it in with whatever code is being executed on your bg thread.
for (int i = 1; i <= 10; i++)
{
if (worker.CancellationPending == true)
{
e.Cancel = true;
break;
}
else
{
// Perform a time consuming operation and report progress.
System.Threading.Thread.Sleep(500);
worker.ReportProgress(i * 10);
}
}
}
// This event handler updates the progress.
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//modify your animation here if you like?
}
// This event handler deals with the results of the background operation.
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//your background process is done. tear down your form here..
}