1

How can I access a value that has been returned by a method that is running in another thread?

Lets say:

public string[,] method1
{
    string a = "blah";
    return a;
}

private void btn_Click(object sender, EventArgs e)
{
    Thread thread = new Thread(method1);
    thread.Start();

    // here I want to use a ...
    Label1.Text = a;
}

Can anyone tell me please?

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
Martin Nemeth
  • 659
  • 3
  • 16
  • 24
  • Hi, I'd like some information about the actual usage here as this may impact the answers you get. Do you really need a thread here? In this case, btn_Click would have to wait for the thread to complete, why not simply call the method directly? – Lasse V. Karlsen Apr 09 '14 at 14:37
  • The way your code is at the minute its very possible that you will have tried to set the text before `a` has been returned, backgroundworkers have a `Result` property that may help here but I agree with Lasse – Sayse Apr 09 '14 at 14:38
  • is `"blah"` a `string[,]`? – Jodrell Apr 09 '14 at 14:39
  • I want to load pretty huge xls file on that button click. This is taking about a minute or so. So I want to avoid freeying of GUI, while the xls is loading. That is why I want to use thread. This is just a example. The method1 should return 2D array where the loaded values should be. – Martin Nemeth Apr 09 '14 at 14:42
  • @MartinNemeth, for several reasons, use a jagged array, `string[][]`, instead of a multidimensional array. Or, perhaps here, an `IEnumerable>` could be appropriate. – Jodrell Apr 09 '14 at 15:00
  • There is nothing intrinsicly wrong with the concept of a multi-dimensional array but, there is with the framework implementation. http://stackoverflow.com/questions/597720/what-are-the-differences-between-a-multidimensional-array-and-an-array-of-arrays – Jodrell Apr 09 '14 at 15:09

2 Answers2

3

why not,

public Task<string> Method1()
{
    return Task.Run(() => "blah");
}

private async void btn_Click(object sender, EventArgs e)
{
    Label1.Text = await Method1();
}

or, if the function takes parameters,

public Task<string> Method1(string someString, int someInt)
{
    return Task.Run(() => string.Format("{0}{1}", someString, someInt));
}

private async void btn_Click(object sender, EventArgs e)
{
    Label1.Text = await Method1("EZ", 1);
}

or even,

private Task<string[][]> ArrayMaker(uint x, uint y)
{
    return Task.Run(() =>
    {
        var result = new string[x][]
        for (var i = 0; i < x; i++)
        {
            result[i] = new string[y];
            for (var j = 0; j < y; j++)
            {
                result[i][j] = ((long)i * j).ToString(
                    CultureInfo.InvariantCulture);
            }
        }

        return result;
    });
}


private async void btn_Click(object sender, EventArgs e)
{
    var bigArray = await ArrayMaker(1000000, 1000000);
}
Jodrell
  • 34,946
  • 5
  • 87
  • 124
0
Label1.InvokeIfRequired(c => { c.Text = a; });

Where "a" is a string or text from another thread.

Bassam Alugili
  • 16,345
  • 7
  • 52
  • 70