I have a property returns the selected item of a radio box group.
public string P1 { get => CB.SelectedItem as string; }
And it's used in an async function which is called in the constructor of a class.
async Task F() {
var b = P1?.Equals(".........", StringComparison.InvariantCultureIgnoreCase) ?? false;
var t1 = callAsync().ContinueWith(x => {
if (b) { .../* use x*/... }
});
await t2;
await t1; //...
The code works fine. However, b
is used in many places so I create a property for it and removed the local variable var b = P1?Equals(....
.
bool b => P1?.Equals(".........", StringComparison.InvariantCultureIgnoreCase) ?? false;
async Task F() {
var t1 = callAsync().ContinueWith(x => {
if (b) { .../* use x*/... } // Exception if F() is called in constructor
});
await t2;
await t1; //...
Now it got the following error when access CB.SelectedItem
?
Cross-thread operation not valid: Control 'CB' accessed from a thread other than the thread it was created on.
Update: I found all the code works if not called from a constructor.