내가 내 응용 프로그램에 대한 양식 환금 모듈을 추가하려고하고 있었고, 난이 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()
를 호출한다. 나는 이것이 이것이 어플리케이션에 좋은 것이라고 믿기가 어렵다. 이것에 대한 다른 의견을 듣고 싶습니다. 감사.