winform中有个timer触发事件,在事件中调用this.Controls.add直接了退出事件,而且控件没有添加成功,求救!!
--------------------编程问答--------------------using System;--------------------编程问答-------------------- this.SuspendLayout();
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.Diagnostics;
using System.Timers;
namespace SimMonitor
{
public partial class Form1 : Form
{
private System.Timers.Timer m_refreshTimer;
private LPView[] m_LPviews; //210,110
private int m_interval;
private int m_exitCount;
LPView lp;
public Form1()
{
InitializeComponent();
//System.Diagnostics.Process[] proc = System.Diagnostics.Process.GetProcessesByName("QQMusic");
/*如果在构造函数中使用this.controls.add是对的*/ //lp = new LPView(proc[0], this, 100);
//lp.Location = new System.Drawing.Point(200, 10);
//lp.Size = new System.Drawing.Size(300, 300);
//lp.Show();
//this.Controls.Add(lp);
}
private void buttonOK_Click(object sender, EventArgs e)
{
try
{
m_interval = int.Parse(textBoxRate.Text);
if (m_interval <= 0)
{
throw new ArgumentNullException();
}
this.textBoxProcName.ReadOnly = true;
this.textBoxRate.ReadOnly = true;
m_refreshTimer = new System.Timers.Timer(m_interval);
m_refreshTimer.AutoReset = true;
m_refreshTimer.Elapsed += this.CheckProcessStart;
m_refreshTimer.Enabled = true;
m_refreshTimer.Start();
}
catch (System.Exception ex)
{
MessageBox.Show("采样周期(秒)必须是正整数");
}
}
private void CheckProcessStart(object sender, System.Timers.ElapsedEventArgs e)
{
Process[] processes = Process.GetProcessesByName(textBoxProcName.Text);
if (processes.Length == 0)
{
MessageBox.Show("进程 " + textBoxProcName + " 未开始运行");
return;
}
else
{
m_refreshTimer.Stop();
/* create new lpview */
for (int i = 0; i < m_LPviews.Length; i++)
{
m_LPviews[i] = new LPView(processes[i], this, i+1);
m_LPviews[i].Location = new System.Drawing.Point(195, 10 + i * 110);
m_LPviews[i].Size = new System.Drawing.Size(210, 110);
m_LPviews[i].Show();
// MessageBox.Show("create success!");
this.Controls.Add(m_LPviews[i]);
[b]/*运行到这句就直接退出了*/ }
/* delegate setting to refresh process information */
m_refreshTimer.Elapsed -= this.CheckProcessStart;
m_exitCount = 0;
textBoxStartTime.Text = processes[0].StartTime.ToString();
foreach (LPView lpview in m_LPviews)
{
m_refreshTimer.Elapsed += lpview.Refresh;
}
m_refreshTimer.Start();
buttonRefresh.Enabled = true;
MessageBox.Show("DSDDDDD");
}
}
public void ProcessExit(object sender, EventArgs e)
{
m_refreshTimer.Elapsed -= ((LPView)sender).Refresh;
m_exitCount++;
if (m_exitCount == m_LPviews.Length)
{
m_refreshTimer.Stop();
buttonRefresh.Enabled = false;
textBoxEndTime.Text = System.DateTime.Now.ToString();
}
}
}
}
this.Controls.Add(m_LPviews[i[b]]);
this.ResumeLayout(false);
加上看看 --------------------编程问答-------------------- 我自己解决了,,,某一线程不能添加其他线程创建的control --------------------编程问答-------------------- 但是具体应该怎样在timer触发的事件中动态创建control并添加到form里面呢? --------------------编程问答-------------------- 真的解决了,,使用System.Windows.Forms.Timer就可以了,,,这样就是一个线程了,,, --------------------编程问答-------------------- 问题是自己最好的老师,恭喜。 --------------------编程问答--------------------
我也碰到过同样的问题,也自己解决了。我用的是另外的一种方法,就是把添加控件的那句代码另外建一个函数,然后把函数放到timer里面,也可以。 --------------------编程问答-------------------- System.Windows.Forms.Timer才是和窗体一个线程的,正解
补充:.NET技术 , C#