BackgroundWorker with anonymous methods ?
Hi,
I'm gonna create a BackgroundWorker with an anonymous method.
I've written the following code :
PHP Code:
BackgroundWorker bgw = new BackgroundWorker();
bgw.DoWork += new DoWorkEventHandler(
() =>
{
int i = 0;
foreach (var item in query2)
{
....
....
}
}
);
But Delegate 'System.ComponentModel.DoWorkEventHandler' does not take '0' arguments and I have to pass two objects to the anonymous method : object sender, DoWorkEventArgs e
Could you please guide me, how I can do it ?
Thanks.
Re: BackgroundWorker with anonymous methods ?
you should do exacly what the compiler is asking you to do:
PHP Code:
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += delegate(Object sender, DoWorkEventArgs e)
{
MessageBox.Show("Hi M-Dayyan!");
};
bw.RunWorkerAsync();
there are hundrets of tutorials on anonymus methods and delegates. just google ;)
Re: BackgroundWorker with anonymous methods ?
Thanks my friend,
we could do that like this
PHP Code:
bgw.DoWork += new DoWorkEventHandler(
(s, e1) =>
{
int i = 0;
foreach (var item in query2)
{
....
....
}
}
);
Re: BackgroundWorker with anonymous methods ?
of course :) this time I chose the lighter method ;)
but even this is possible:
PHP Code:
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += (sender, e) =>
{
MessageBox.Show("Hi!");
};
bw.RunWorkerAsync();