开发者

Setting RunWorkerCompleted value

开发者 https://www.devze.com 2023-02-20 19:50 出处:网络
If i have a background worker that does some tasks in its do work String val= getVal(\"Val\"); byte[] b = (byte[])e.Argument;

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消