dimanche 19 avril 2015

Task no longer runs my code

I am new to the concept of multithreading and I wanted to implement it into my existing code to make some nested foreach loops run faster. After putting it in a task and running it, the code no longer does anything.



var task = Task.Factory.StartNew
(() =>
{
foreach (Control groupbox in tabControl.SelectedTab.Controls)
{
if (groupbox is GroupBox)
{
foreach (Control ctrl in groupbox.Controls)
{
if (ctrl is Panel)
{
foreach (Control c in ctrl.Controls)
{
if (c is CheckBox)
(c as CheckBox).Checked = false;
}
}
if ((string)ctrl.Tag == "num")
(ctrl as TextBox).Text = Convert.ToString(0);
else if (ctrl is TextBox)
(ctrl as TextBox).Text = "";

if (ctrl is MaskedTextBox)
(ctrl as MaskedTextBox).Text = "";
if (ctrl is ComboBox)
(ctrl as ComboBox).SelectedIndex = 0;
if (ctrl is CheckBox)
(ctrl as CheckBox).Checked = false;
}
}
}
});


Without the var task = Task.Factory.StartNew (() =>...}); the code runs but is obviously not in a task. If anyone can spot what I am doing incorrectly, it would be greatly appreciated. Thanks.


Aucun commentaire:

Enregistrer un commentaire