Adam
Adam

Reputation: 632

How do I get a bool from a different thread?

To do cross-thread operations, I use the following:
this.Invoke(new MethodInvoker(() => myMethod());

However, I can't do, for example, the following:

this.Invoke(new MethodInvoker(() => bool myBool = getBool()); 
return myBool;

How would I do this? I can't just do bool myBool = getBool();
because I get a cross-threading operation error.

Thanks in advance.

Upvotes: 2

Views: 501

Answers (2)

ProgramFOX
ProgramFOX

Reputation: 6390

Try this:

delegate T MyDelegate<out T>();
public bool MethodName()
{
    bool b = (bool)this.Invoke(new MyDelegate<bool>(() => getBool()));
    return b;
}

Upvotes: 4

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73502

Not sure what you mean

But you could do something like this

bool myBool = false;
this.Invoke(new MethodInvoker(() => myBool = getBool()));
return myBool;

If am wrong pls make me clear

Upvotes: 1

Related Questions