我试图添加一个形式兑现模块为我的应用程序,我实现了这个MSDN示例:Form Stacking and CachingApplication.DoEvents()在应用程序运行时不断调用?
对于那些谁不能下载的样本,这是包含了一段代码,我指的类:
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Threading;
namespace GUIFramework {
public class FormStack : CollectionBase {
private System.Collections.ArrayList stack = new ArrayList();
public void Run() {
do {
System.Windows.Forms.Application.DoEvents();
} while(List.Count > 0);
}
public void Stop() {
// nicely destroy each Form
foreach(StackForm sf in List) {
sf.Dispose();
}
// clear the list to kill the message pump in Run()
List.Clear();
}
public void Push(Type FormType) {
// only allow 1 Push at a time to maintain cache and stack itegrity
Monitor.Enter(this);
foreach(StackForm sf in List) {
if(sf.GetType().Name.Equals(FormType.Name)) {
// form is cached so display cached version
sf.Visible = true;
// add it to the stack
stack.Add(FormType.Name);
return;
}
}
// the Form wasn't cached, so create it
StackForm form = Preload(FormType);
// display it
form.Visible = true;
// add a close event handler
form.FormClosed += new FormClose(form_FormClosed);
// add it to the stack
stack.Add(FormType.Name);
Monitor.Exit(this);
}
public StackForm Preload(Type FormType) {
StackForm form = (StackForm)Activator.CreateInstance(FormType);
// get data on a separate thread
form.LoadData();
// build the form
form.InitializeComponent();
// wait for the data thread to finish
form.threadRunning.WaitOne();
// now populate the controls with any retrieved data
form.Populate();
form.MinimizeBox = false; // required to get close event on PPC!
// add it to the cache
List.Add(form);
return form;
}
public void Pop(uint FormsToPop) {
if(stack.Count <= FormsToPop) {
throw new Exception("You cannot Pop the entire stack!");
}
else {
// remove from stack but not cache
for(int i = 0 ; i < FormsToPop ; i++) {
stack.RemoveAt(stack.Count - 1);
}
foreach(StackForm sf in List) {
// find the last form in the stack
if(sf.GetType().Name.Equals(stack[stack.Count - 1])) {
// make it visible
sf.Visible = true;
}
}
}
}
private void form_FormClosed() {
Pop(1);
}
public override string ToString() {
string message = "There are " + List.Count.ToString() + " forms cached\r\n";
message += "Stack contents:";
for(int i = stack.Count - 1 ; i >= 0 ; i--) {
message += "\r\n" + stack[i].ToString();
}
return message;
}
}
}
的Run()
方法是不断调用Application.DoEvents()
而应用捉迷藏。我很难相信这对于应用程序来说是件好事。我很乐意听到关于此的其他意见。谢谢。