Interaction between two Forms and Threads
What do I need: I need a separate Form that shows some information from my Main Program/Class but the program must continue. So I decided to start the second window in a second thread. And after it is created I want to send data which should be shown.
Where is the Problem: I have created a thread and generated the form but I can't fill date in this new Form.
My code so far:
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 already running"); }
}
public void test()
{
fm2.Gen();
}
// start the generating 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);
}
}
}