What are you expecting from running this code as a task? It does not provide any advantage here. The possibly long running license validation is already being called with await
making it non-UI blocking (unless its implementation is messed up).
To make the message box blocking, i.e. modal, simply write
bool valid = await checkValidLicenseAsync(hardwareid); // non-blocking
if (!valid) {
MessageBox.Show("no License."); // blocking
}
Assuming that this is the System.Windows.MessageBox
and not its winforms pendant.
A possible approach is to use a factory method to return the object. Another one is to use an AsyncLazy<T>
as described here by Stephen Cleary
See also:
- Svick's answer to Can constructors be async?.