I am having a problem with setting referenced variable inside of thread.
Error:
“Cannot use ref or out parameter ‘output’ inside an
anonymous method, lambda expression, or query expression”
Is there any way to get this to work or achieve similar effect?
public static void LoadThreaded<T>(string path, ref T output)
{
ThreadStart threadStart = delegate
{
output = Loader<T>(path);
};
new Thread(threadStart).Start();
}
The problem is that the method returns prior to the
outputvariable being set (necessarily), as theoutputis set by a different thread.The best option here would be to use the TPL to rework this, ie:
This allows you to start this asynchronous operation, and get the result when it completes, either by blocking (calling
output.Result) or by adding a task continuation.