I am getting an undefined null pointer exception if I try this. (Removing the thread code solves it). Isn't it possible to spawn a new thread from a setter? If so, why not? I am used to doing it this way from Java. Thank you
EDIT: The actual stripped down code:
public string AuthToken {
set {
this.authToken = value;
var RunOnNewThread = new Thread(() =>
{
Console.WriteLine("Test");
} );
RunOnNewThread.Start();
}
get { return this.authToken; }
}
It does actually run the thread. Note! This is in an Xamarin Android app, and the error only occurs in debug mode. Release works perfectly.
Thread started: #2
Test
Thread finished: #2
Here a screenshot from Xamarin:
I tried surrounding the thread creation in a try/catch, but it still throws the null pointer on RunOnNewThread.Start()
.
However, there must be something else going on. Because it just ran fine 2 times, and the 3rd time I got the null pointer again. Any ideas?