Winforms,仅可在断点处使用控件

问题描述

我为Winforms应用程序创建了一个自定义的Toast通知,每当返回自定义结果时,我都会使用ShowMessage扩展来显示该通知。

在登录屏幕上,我有一个解锁应用程序按钮,只要密码不正确,该按钮就会显示烤面包机消息。

当我在result.ShowMessage()之前放置一个断点时;通知出现。但是当我删除断点时,它不再出现。我不更改任何调试值。

我该如何解决?我确实尝试实现thread.sleep,但是它没有任何改变,仅当我插入一个断点并继续处理该断点时,该通知才会出现。

值得注意的是,该通知在应用程序中的其他任何地方都可以正常工作。

解锁应用程序的代码:

private void UnlockApplication()
    {
        var result = new Business.Server.User().Get(_loginModel.UserName,_loginModel.Password.Encrypt());
        if (result.IsSuccessful)
        {
            // Perform log in process
        }
        else
        {
            result.ShowMessage(); //Only works when I put breakpoint here 
        }
    }

ShowMessage扩展代码

public static void ShowMessage<T>(this Models.Result<T> result) where T : class
    {
      Helpers.ToasterNotificationHelper.ShowNotification(result.ResultTypeKey.ToDescription(),result.Message,result.ResultImage(),result.ResultColor(),result.ResultTypeKey == Enums.ResultTypeEnum.Warning ? 5000 : 2500);  
    }

public static void ShowNotification(string header,string message,Image icon,Color backgroundColor,int durationInMilliseconds = 1000)
        {
            if (Application.OpenForms[0] is MyApplication)
            {
                var toasterNotification = new ToasterNotificationControl(header,message,icon,backgroundColor);
                (Application.OpenForms[0] as MyApplication).toastNotificationCollectionControl1.AddNotification(toasterNotification,durationInMilliseconds);
            }
        }

我的Toast Notification控件是一个DevExpress弹出面板控件,已添加到Main应用程序表单中

解决方法

您的“ Get(_loginModel.UserName ...”似乎是异步的。请尝试执行下一个操作以使呼叫等待:

private async System.Threading.Tasks.Task UnlockApplicationAsync()
{
    var result = await new Business.Server.User().Get(_loginModel.UserName,_loginModel.Password.Encrypt());
...

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...