如图所示,自己做的小程序,点击开始传输时打开多个窗体,预计每个窗体为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%左右,希望多线程后提高效率。如何看一个进程的线程数量?
private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
{
System.Environment.Exit(System.Environment.ExitCode);
this.Dispose();
}
th.Start();
//或者
Thread th2 = new Thread(() => { new RankingFrom().ShowDialog(); });
th2.Start();
this.Invoke((MethodInvoker)delegate
{});
Monitor.Exit(this);
Thread.Sleep(10);统统干掉
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);
}
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了,一时大意
Form.CheckForIllegalCrossThreadCalls = false;
Control.CheckForIllegalCrossThreadCalls = false;
提示:
child.MdiParent = (Form)this.Invoke(new Func<bool>(() => { return this; }))
不建议设置checkforlllegalcrossthreadcal=false
谁能帮我看看啊
难道是:在mdi创建的新窗体都是同一个进程吗?然后在新窗体里用多线程吗?
现在只是不能同时处理,新开的窗口得一个接着一个的处理,把这个解决就可以了。
http://firechun.blog.163.com/blog/static/3180452220106543254740/
特别感谢haukwong,问题已经解决。