背景:無限ループで 'task + number'を出力するタスクでアプリケーションを実行します。私は、アプリケーションを閉じると、それがどのように見えるのか、そのタスクで何が起こっているのかを知りたい。アプリケーションを閉じると何が起こっているのですか?C#
私はその実行タスクを参照するために使用私の例、:
//delegate to pring text in label
private delegate void SetTextToControlDelegate(string text, Control control);
private void SetTextToControl(string text, Control control)
{
if (control.InvokeRequired)
{
SetTextToControlDelegate deleg =
new SetTextToControlDelegate(SetTextToControl);
this.Invoke(deleg, new object[] { text, control });
}
else
{
control.Text = text;
}
}
//run a task
private void Run()
{
Task.Factory.StartNew(() =>
{
int i = 0;
while (true)
{
Thread.Sleep(1000);
i++;
string result = "task " + i.ToString();
SetTextToControl(result, label1);
}
});
}
//button to run task
private void button1_Click(object sender, EventArgs e)
{
try
{
Run();
}
catch (Exception ex)
{
SetTextToControl(ex.Message,label1);
}
}
アプリケーション全体を閉じると、タスクが強制終了されると思います。 – Sebi
タスクが強制終了されました。スレッドはプロセスに属します。 –