If i have a background worker that does some tasks in its do work
String val= getVal("Val");
byte[] b = (byte[])e.Argument;
b = 开发者_StackOverflow社区getData.FromPlace(val);
How do i pass the vakue of b to the runworkercompleted method?
You could use closure
void Main()
{
var bw = new BackgroundWorker();
byte[] b;
bw.DoWork += (sender, args) => {
b = DoStuff();
};
}
byte[] DoStuff() {
String val= getVal("Val");
byte[] b = (byte[])e.Argument;
b = getData.FromPlace(val);
return b;
}
You could also use return Result property on the event args object. I think this way gives more flexibility.
void Main()
{
var bw = new BackgroundWorker();
bw.DoWork += (sender, args) => {
args.Result = DoStuff();
};
bw.RunWorkerCompleted += (sender, args) => {
var result = args.Result as byte[];
};
bw.RunWorkerAsync();
}
byte[] DoStuff() {
return new byte[10];
}
You can use an instance variable. Place the declaration above the DoWork
event definition.
private byte[] b;
精彩评论