任务并且不要关闭,直到基本任务有孩子

时间:2014-11-11 13:43:20

标签: c# winforms task-parallel-library console-application

我的类(ConsoleApplication)名称ClassA带有字段:

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();
        }
}

(关于ChartForm在帖子末尾的描述) 好。当我在Program.cs中创建ClassA时:

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

终端显示: 它显示为与windowsform(ChartForm)平行,但未显示ChartForm。我想为ClassA创建descructor或其他方式。如果父母(ClassA)有孩子(ChartForm),直到它有一个正在运行的孩子,我怎么能这样做 - 不要关闭应用程序。 我试图在析构函数ClassA中添加:

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

但它没有帮助。

ChartForm来自其他项目,它继承了WindowsForms(Form)并且只有一个对象:Chart。

请看下面:

   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;
        }

   }

0 个答案:

没有答案