WinFrom多窗体间如何实现for循环切换,在线等啊,急?
现有5个窗体 :main,A,B,C,D,在main窗体中有四个checkbox,分别对应A B C D四个Form对象,点击main页面中的按钮,触发事件,按先后选中顺序,无限循环show出选中的窗体,直到点击取消按钮,如顺序 D,B,A,C ,先后选中这四个checkbox后,点击main中确定按钮, 先显示D,然后 B,A,C,要求之间有时间间隔,如5000毫秒,任何窗体不能死锁,点击mian页面取消按钮,A B C D 四个窗体close掉。
目前小弟完成了一部分,但是貌似在a b c d四个窗体切换时,main死锁了,不能进行任何操作。
部分代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace SwitchWindowApp
{
public partial class Control : Form
{
public delegate void SwitchDelegate(Form form);
SwitchDelegate switchDelegate = null;
public Control()
{
InitializeComponent();
switchDelegate = new SwitchDelegate(DelegateMethod);
}
List<Form> list = new List<Form>();
private void runbtn_Click(object sender, EventArgs e)
{
if (list.Count == 0)
{
MessageBox.Show("您还没选中任何窗体", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
Thread thread = new Thread(new ParameterizedThreadStart(ThreadMethod));
thread.Start(list);
}
}
public void ThreadMethod(Object formlist) {
List<Form> list = (List<Form>)formlist;
while (1 == 1)
{
foreach (Form item in list)
{
switchDelegate(item);
}
}
}
public void DelegateMethod(Form form) {
if (InvokeRequired)
{
Invoke(new SwitchDelegate(DelegateMethod),form);
}
else {
form.Show();
form.Location = new Point(500,500);
Thread.Sleep(5000);
form.Hide();
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
list.Add(new Form1());
}
else
{
list.Remove(new Form1());
}
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked)
{
list.Add(new Form2());
}
else
{
list.Remove(new Form2());
}
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
if (checkBox3.Checked)
{
list.Add(new Form3());
}
else
{
list.Remove(new Form3());
}
}
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
if (checkBox4.Checked)
{
list.Add(new Form4());
}
else
{
list.Remove(new Form4());
}
}
private void checkBox5_CheckedChanged(object sender, EventArgs e)
{
if (checkBox5.Checked)
{
list.Add(new Form5());
}
else
{
list.Remove(new Form5());
}
}
}
}