0

I have class (ConsoleApplication) names ClassA with a field:

public class ClassA
{
        List<Task> Tasks;
        (...)
        public void PlotGrid()
        {
                Action<object> action = new Action<object>(ShowChart);
                Task task = new Task(action, intervalX);
                task.Start();
                Tasks.Add(task);
        }
        (...)
        private void ShowChart(object intervalX)
        {
                int interval = Convert.ToInt32(intervalX);
                ChartForm chart = new ChartForm(GetValuesForPlotting(), interval);
                chart.ShowDialog();
        }
}

(Description about ChartForm's at the end of post) Ok. When I have created the ClassA in Program.cs:

class Program
{
    static void Main(string[] args)
    {
            ClassA class = new ClassA();
            class.PlotGrid();
            Console.WriteLine("it was shown as parallel with windowsform(the ChartForm)");
    }
}

In terminal had shown: it was shown as parallel with windowsform(the ChartForm) but a ChartForm not shown. I want to create a descructor or other way for ClassA. How i can do it if a parent (ClassA) has children (ChartForm) and until it has a children whom are running - don't close app. I tried to add in destructor ClassA:

~ClassA()
{
    Task.WaitAll(Tasks.ToArray());
}

but it didn't help.

Class ChartForm is from other project, which inherit WindowsForms (Form) and has only one object: Chart.

Please look below:

   public partial class ChartForm : Form
   {
        private List<Complex> valuesForPlotting;
        int intervalX;
        public ChartForm(List<Complex> valuesForPlotting, int intervalX)
        {
            InitializeComponent();
            this.valuesForPlotting = valuesForPlotting;
            this.intervalX = intervalX;
        }
        private void Form1_Load(object sender, EventArgs e)
        {   
            chart1.ChartAreas[0].AxisX.Interval = intervalX;
            chart1.ChartAreas[0].AxisX.Minimum = 0;
            chart1.ChartAreas[0].AxisX.Maximum = valuesForPlotting.Max(p=> p.Real)+intervalX;

            for (int i = 0; i < valuesForPlotting.Count; i++)
            {
                chart1.Series["ser1"].Points.AddXY
                                (valuesForPlotting[i].Real, valuesForPlotting[i].Imaginary);
            }

            chart1.Series["ser1"].ChartType = SeriesChartType.FastLine;
            chart1.Series["ser1"].Color = Color.Red;
        }

   }
Marek Woźniak
  • 1,766
  • 16
  • 34
  • 1
    Maybe this question will be relevant? [Show WinForm from C# console application and wait to close](http://stackoverflow.com/q/25925680/945456). This question may also be helpful: [Show Form from hidden console application](http://stackoverflow.com/q/22605988/945456). – Jeff B Nov 11 '14 at 13:59
  • 4
    There are so many things wrong: you are trying to show the dialog [from a background thread](http://stackoverflow.com/questions/10905844/creating-a-form-and-using-form-showdialog-on-a-background-thread), you [block the finalizer](http://msdn.microsoft.com/en-us/library/system.object.finalize%28v=vs.110%29.aspx) on waiting for tasks, you don't define the [STAThread](http://stackoverflow.com/questions/1361033/what-does-stathread-do)... Where to start? – galenus Nov 11 '14 at 14:03
  • Thanks you for answers. It will help me, but I'm a green in threading. I have to read more about parallel. – Marek Woźniak Nov 11 '14 at 14:21

0 Answers0