如图所示,自己做的小程序,点击开始传输时打开多个窗体,预计每个窗体为1个线程,可是程序写好运行后失败,目前为1个新窗体运行后第二个新窗体才运行,我希望可以他们同时运行。
源码如下:private void button1_Click(object sender, EventArgs e)
{
  string s = textBox1.Text;
  string [] a = s.Split('\r');
  int l = a.Length;
  int i;
  for (i = 0; i < l; i++ )
  {
    wt = Computer + "~" + ComputerFenlei + "~" + a[i] + "~" + ComputerAct;
    Thread t1 = new Thread(new ParameterizedThreadStart(Thread1));
    t1.Start(wt);
  }
}public void Thread1(object obj)
{
  string str = obj as string;
  Monitor.Enter(this);
  this.Invoke((MethodInvoker)delegate
  {
    if (checkBox6.Checked)
    {
      upload child = new upload();
      child.MdiParent = this;
      child.Show();
      child.Text = str;
    }
    if (checkBox5.Checked)
    {
      delete child2 = new delete();
      child2.MdiParent = this;
      child2.Show();
      child2.Text = str;
    }
  });
  Monitor.Exit(this);
  Thread.Sleep(10);
}窗体都能打开,打开后也都是自动运行,我就是希望新打开的多个窗体能够同时运行,多线程运行,现在我的资源管理器中cpu使用才10%左右,希望多线程后提高效率。如何看一个进程的线程数量?

解决方案 »

  1.   

    我在 windows资源管理器-进程中,删除我刚才启动的程序,桌面上启动的程序关闭了,但是资源管理器中又出现个进程,但是pid不一样,请问如何完全关闭?我的关闭方法就是点击右上角的x
      

  2.   

    使用了Monitor.Enter(this);就必须一个线程执行完Monitor.Exit(this);中包含的代码后,另一个线程才能进入执行该段代码,删掉Monitor.Enter(this);和Monitor.Exit(this);就可以解决目前为1个新窗体运行后第二个新窗体才运行问题第二个加上这个看行不行
    private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
    {
            System.Environment.Exit(System.Environment.ExitCode);
            this.Dispose();
    }
      

  3.   

    你虽然在button1_Click开了一个线程,但是在方法中又使用了this.Invoke(),这样一来就又回到主线程了,所以相当于没开线程。正确的做法因该这样:            Thread th = new Thread(() => { Application.Run(new RankingFrom()); });
                th.Start();
    //或者
                Thread th2 = new Thread(() => { new RankingFrom().ShowDialog(); });
                th2.Start();
      

  4.   

    关闭时,是先把里面的窗体关了,后关的主窗体,但是资源管理其中该进程的pid号变了(或者说先关后开了)
      

  5.   

    把Monitor.Enter(this);
      this.Invoke((MethodInvoker)delegate
      {});
      Monitor.Exit(this);
      Thread.Sleep(10);统统干掉
      

  6.   


            public void Thread1(object obj)
            {
                string str = obj as string;
                //Monitor.Enter(this);
                if ((bool)this.Invoke(new Func<bool>(() => { return checkBox6.Checked; })))
                {
                    upload child = new upload();
                    child.MdiParent = this;
                    child.Text = str;
                    Application.Run(new child());
                }
                if ((bool)this.Invoke(new Func<bool>(() => { return checkBox5.Checked; })))
                {
                    delete child2 = new delete();
                    child2.MdiParent = this;
                    child2.Text = str;
                    Application.Run(new child2());
                }
                //Monitor.Exit(this);
                Thread.Sleep(10);
            }
      

  7.   

    未能找到类型或空间命名child
      

  8.   


            public void Thread1(object obj)
            {
                string str = obj as string;
                //Monitor.Enter(this);
                if ((bool)this.Invoke(new Func<bool>(() => { return checkBox6.Checked; })))
                {
                    upload child = new upload();
                    child.MdiParent = this;
                    child.Text = str;
                    Application.Run(child);
                }
                if ((bool)this.Invoke(new Func<bool>(() => { return checkBox5.Checked; })))
                {
                    delete child2 = new delete();
                    child2.MdiParent = this;
                    child2.Text = str;
                    Application.Run(child2);
                }
                //Monitor.Exit(this);
                Thread.Sleep(10);
            }这一的,不用new了,一时大意
      

  9.   

    加入:
    Form.CheckForIllegalCrossThreadCalls = false;
    Control.CheckForIllegalCrossThreadCalls = false;
    提示:
      

  10.   


    child.MdiParent = (Form)this.Invoke(new Func<bool>(() => { return this; }))
    不建议设置checkforlllegalcrossthreadcal=false
      

  11.   

    http://www.2cto.com/kf/201110/108916.html
    谁能帮我看看啊
      

  12.   

    这种问题应该挺常见吧,确定的方法是怎么做的?
    难道是:在mdi创建的新窗体都是同一个进程吗?然后在新窗体里用多线程吗?
    现在只是不能同时处理,新开的窗口得一个接着一个的处理,把这个解决就可以了。
      

  13.   

    http://firechun.blog.163.com/blog/static/31804522200991703317373/
    http://firechun.blog.163.com/blog/static/3180452220106543254740/
      

  14.   

    去掉锁提示在线程里不能用this
      

  15.   


    特别感谢haukwong,问题已经解决。