In my winform programme I use Postsharp interceptor class on each control event to avoid try/catch block repetition.
The custom postsharp method:
[Serializable]
public class OnErrorShowMessageBox : MethodInterceptionAspect
{
public override void OnInvoke(MethodInterceptionArgs args)
{
try
{
args.Proceed();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
args.ReturnValue = null;
}
}
}
Usage this the attributs:
[OnErrorShowMessageBox]
private void txtComments_TextChanged(object sender, EventArgs e)
{
//blabla
}
This works like a charm BUT know I would like to use async on the event. So txtComments_textChanged become :
[OnErrorShowMessageBox]
private async void txtComments_TextChanged(object sender, EventArgs e)
{
await //blabla
}
And here comes the problem. Try/catch bloc in the interceptor method don't catch anything when is async... How can I do ? thanks