MDI的應用問題

• 問題

• 大家好：
我現在寫一個找質數的MDI程式，但我遇到了瓶頸，各位高手可否給我一點方向？
主視窗：
```decimal ChildCount = 0;

{
if (ChildForm.ActiveForm.InvokeRequired)
{
ChildForm.ActiveForm.Invoke(new Start1(StartMethod1));
}
else
{
for (decimal i = 0; i < decimal.Parse(richTextBox1.Text); i++)
{
ChildForm newChildForm = new ChildForm();
newChildForm.Text = "Prime Number Generator " + ChildCount.ToString();
newChildForm.MdiParent = this;

newChildForm.TabCtrl = tabControl1;

TabPage tp = new TabPage();
tp.Parent = tabControl1;
tp.Text = newChildForm.Text;
tp.Show();

newChildForm.TabPage = tp;

newChildForm.Show();
ChildCount++;

tabControl1.SelectedTab = tp;
}
this.LayoutMdi(MdiLayout.TileHorizontal);
}
}

delegate void Start1();
private void StartMethod1()
{
for (decimal i = 0; i < decimal.Parse(richTextBox1.Text); i++)
{
ChildForm newChildForm = new ChildForm();
newChildForm.Text = "Prime Number Generator " + ChildCount.ToString();
newChildForm.MdiParent = this;

newChildForm.TabCtrl = tabControl1;

TabPage tp = new TabPage();
tp.Parent = tabControl1;
tp.Text = newChildForm.Text;
tp.Show();

newChildForm.TabPage = tp;

newChildForm.Show();
ChildCount++;

tabControl1.SelectedTab = tp;
}
this.LayoutMdi(MdiLayout.TileHorizontal);
}

private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (ChildForm ChildForm in this.MdiChildren)
{
if (ChildForm.TabPage.Equals(tabControl1.SelectedTab))
{
ChildForm.Select();
}
}
}

private void Start_Click(object sender, EventArgs e)
{
//SaveTimer.Start();
//this.Invoke(new MethodInvoker(StartMethod1));
}
```

我覺得主視窗似乎沒有什麼問題，好像是子視窗出槌了

子視窗：
```        private

TabControl tabCtrl;
private

TabPage tabPage;

public

TabPage TabPage
{
get

{
return

tabPage;
}
set

{
tabPage = value;
}
}

public

TabControl TabCtrl
{
set

{
tabCtrl = value;
}
}

public

decimal

Calculate(decimal

first, decimal

number)
{
decimal

decimal

for

(decimal

a = first; preanswer <= number; a++)
{
}
return

}

private

static

Mutex wait = new

Mutex();
public

void

{
for

(number = prenumber; number > 1 && t1start == true

; number++)
{

wait.WaitOne();

一串算質數的程式

wait.ReleaseMutex();
}
}

private

void

ChildForm_Activated(object

sender, EventArgs e)
{
tabCtrl.SelectedTab = tabPage;
if

(!tabCtrl.Visible)
{
tabCtrl.Visible = true

;
}

}

private

void

ChildForm_Closing(object

sender, System.ComponentModel.CancelEventArgs e)
{
this

.tabPage.Dispose();
if

(!tabCtrl.HasChildren)
{
tabCtrl.Visible = false

;
}
}

private

void

sender, EventArgs e)
{
SaveTimer.Start();

;
//this.backgroundWorker1.RunWorkerAsync();

}
}
}
```

我現在用的是Mutex，但是這樣子不就一次只有一個MDI子視窗在跑嗎？這樣子應該就不是MDI了吧！我之前是過不用Mutex的，而且我有用Invoke
```        if

(label3.InvokeRequired)
{
label3.Invoke(new

Stop1(StopMethod1));
}
else

{
label3.Text = "Stopping"

;
}

delegate

void

Stop1();
private

void

StopMethod1()

{
label3.Text = "Stopping"

;

}
```