我创建了一个名为ProcessingRequest的子表单,并在其上放置了一些文本和一个动画gif。
我有我的主要形式的产权是我的计算子表单应该在的位置。
private Point ProcessingLocation { get { return new Point(this.Location.X + this.Width/2 - new ProcessingRequest().Width/2, this.Location.Y + this.Height/2 - new ProcessingRequest().Height/2); } }
我有一个类,使一个新的线程来显示其子形式。
public class ShowProgress
{
static private System.Drawing.Point point;
static private ProcessingRequest p;
static public void ShowProgressForm(System.Drawing.Point myPoint)
{
point = myPoint;
Thread t = new Thread(new ThreadStart(ShowProgress.ShowForm));
t.IsBackground = true;
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
static private void ShowForm()
{
p = new ProcessingRequest();
p.StartPosition = FormStartPosition.Manual;
p.Location = point;
p.TopMost = true;
Application.Run(p);
}
static public void CloseForm()
{
p.Invoke(new CloseDelegate(ShowProgress.CloseFormInternal));
}
static private void CloseFormInternal()
{
p.Close();
}
}
public delegate void CloseDelegate();
然后在我的主要形式,我只是把
ShowProgress.ShowProgressForm(ProcessingLocation);
//heavy processing code goes here or whatever
ShowProgress.CloseForm();
:)
日Thnx,但我表现出一个线程的第二种形式。当我尝试form.Show(this)时,我得到这个错误:'跨线程操作无效:控制'frmProcessBuilder'从一个线程访问,而不是它创建的线程。' – Martijn 2009-12-22 08:33:42
只适用于'ShowDialog()'。 – Joey 2009-12-22 08:33:52
我无法使用ShowDialog(),因为直到此对话框关闭,我的代码才会停止。这不是我需要的。表单必须在加载完成之前一直显示。 – Martijn 2009-12-22 08:37:05