locked
MDI的應用問題 RRS feed

  • 問題

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

    public void ThreadMethod()
    {
    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();
    Thread StartThread1 = new Thread(new ThreadStart(ThreadMethod));
    //StartThread1.Priority = ThreadPriority.Lowest;
    StartThread1.IsBackground = true;
    StartThread1.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
    
     answer = 1;
                decimal
    
     preanswer = 1;
                for
    
     (decimal
    
     a = first; preanswer <= number; a++)
                {
                    answer = a;
                    preanswer = a * a;
                }
                return
    
     answer - 1;
            }
    
            private
    
     static
    
     Mutex wait = new
    
     Mutex();
            public
    
     void
    
     ThreadMethod()
            {
                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
    
     ChildForm_Load(object
    
     sender, EventArgs e)
            {
                SaveTimer.Start();
                Thread StartThread1 = new
    
     Thread(new
    
     ThreadStart(ThreadMethod));
                StartThread1.IsBackground = true
    
    ;
                StartThread1.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" ;
            }
    類似上面一樣的東西,可是一次跑多一點的時候,UI介面就沒反應(不理我了)(按按鈕都沒有用),於是我就用了Thread.Sleep(5),可是這樣一來每算一個質數是不是質數,就要停5毫秒,效率不就會變差嗎?請問有沒有一個高效率,Form又有反應的方法?PS我算質數的程式碼應該沒有問題(用SDI時跑得好好的)
    2009年7月13日 下午 02:48

所有回覆

  • 等你把程式碼整理到 "可以看了" 的時候我再解鎖。
    小人物一枚。
    2009年7月13日 下午 02:52
    版主