ricki
ricki

Reputation: 159

Setting RunWorkerCompleted value

If i have a background worker that does some tasks in its do work

    String val= getVal("Val");
    byte[] b = (byte[])e.Argument;

    b = getData.FromPlace(val);

How do i pass the vakue of b to the runworkercompleted method?

Upvotes: 1

Views: 3052

Answers (2)

Razor
Razor

Reputation: 17508

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];
}

Upvotes: 4

Evan Mulawski
Evan Mulawski

Reputation: 55354

You can use an instance variable. Place the declaration above the DoWork event definition.

private byte[] b;

Upvotes: 0

Related Questions