Existe-t-il un moyen de mettre en ligne la tâche déléguée au lieu de la séparer dans une autre fonction ?
Code original :
private void ofdAttachment_FileOk(object sender, CancelEventArgs e)
{
System.Threading.ThreadPool.QueueUserWorkItem((o) => Attach());
}
void Attach() // I want to inline this function on FileOk event
{
if (this.InvokeRequired)
{
this.Invoke(new Action(Attach));
}
else
{
// attaching routine here
}
}
Je voulais que ce soit comme ça (pas besoin de créer une fonction séparée) :
private void ofdAttachment_FileOk(object sender, CancelEventArgs e)
{
Action attach = delegate
{
if (this.InvokeRequired)
{
// but it has compilation here
// "Use of unassigned local variable 'attach'"
this.Invoke(new Action(attach));
}
else
{
// attaching routine here
}
};
System.Threading.ThreadPool.QueueUserWorkItem((o) => attach());
}