两个表单和线程之间的交互

时间:2015-08-14 10:32:40

标签: c# multithreading forms

我需要什么: 我需要一个单独的表格,显示我的主程序/班级的一些信息,但程序必须继续。所以我决定在第二个线程中启动第二个窗口。在创建之后,我想发送应该显示的数据。

问题在哪里:我创建了一个帖子并生成了表单,但我无法在此新表单中填写日期。

我的代码到目前为止:

    public partial class Form1 : Form
        {
            int number = 0;
            Thread t1;
            Form2 fm2 = new Form2();

            //open the second Form
            private void add_window_Click(object sender, EventArgs e)
            {
                t1 = new Thread(test);
                if (!t1.IsAlive )
                {
                        t1.Start();    
                }
                else { MessageBox.Show("Thread allready runing"); }
            }
            public void test()
            {

                fm2.Gen();
            }

            // start the generitng of new data
            private void sec_serein_Click(object sender, EventArgs e)
            {
                fm2.add_ser();
            }
        }
 class Form2 
    {
        Form FormX = new Form();
        int number = 0;
        Charting.ChartArea chartArea1 = new Charting.ChartArea();
        Charting.Legend legend1 = new Charting.Legend();
        Charting.Series series1 = new Charting.Series();
        Charting.Chart chart1 = new Charting.Chart();
        public void Gen()
        {

            // create an Form with a Charting Area
            FormX.ShowDialog();
        }
        public void add_ser()
        {
            // For each Row add a new series
            string seriesName = "Series_" + number;
            number++;
            chart1.Series.Add(seriesName);
            chart1.Series[seriesName].ChartType = SeriesChartType.Line;
            chart1.Series[seriesName].BorderWidth = 2;
            Random rnd = new Random();
            for (int i = 0; i < 20; i++)
            {
                string columnName = i.ToString();
                int YVal = rnd.Next(0, 100);
                chart1.Series[seriesName].Points.AddXY(columnName, YVal);
            }
        }

    }

0 个答案:

没有答案