【问题标题】:Query not executing after exiting method退出方法后查询未执行
【发布时间】:2015-11-21 22:04:24
【问题描述】:

我有一个从我编写的 Web 服务运行的简单插入查询。单击按钮时,我让它运行一个调用 Web 服务并执行插入查询的方法。出于某种原因,在我退出点击事件之前,它实际上并没有插入信息……谁能告诉我为什么?我将在下面发布代码:

private void btnSubmit_Click(object sender, RoutedEventArgs e)
    {
        //inserts new article into DB
        Insert();
    }

 private void InsertNewArticle()
    {
        ServiceReference1.Service1Client service = new ServiceReference1.Service1Client();

        ArticleDetails articleInfo = new ArticleDetails();
        articleInfo.Title = newsTitle;
        articleInfo.Body = newsBody;
        articleInfo.Author = newsAuthor;

        service.InsertArticleDetailsAsync(articleInfo);
    }

我通过调试看到的是,它在退出btnSubmit_Click 事件之前不会执行。这对我来说是个问题的原因是我想对退出点击事件之前刚刚提交的信息做一些事情。

注意:这是一个通用 Windows 应用程序

【问题讨论】:

    标签: c# web-services asynchronous uwp


    【解决方案1】:

    您正在调用一个异步方法,但不等待它执行。将 InsertNewArticle 方法更改为

    private async void InsertNewArticle()
    {
        ServiceReference1.Service1Client service = new ServiceReference1.Service1Client();
        ArticleDetails articleInfo = new ArticleDetails();
        articleInfo.Title = newsTitle;
        articleInfo.Body = newsBody;
        articleInfo.Author = newsAuthor;
    
        await service.InsertArticleDetailsAsync(articleInfo);
    }
    

    【讨论】:

    • 您说的很对,非常感谢您的回答。当它允许我时将其标记为已回答
    猜你喜欢
    • 2014-01-28
    • 1970-01-01
    • 2021-11-17
    • 2014-11-04
    • 2023-03-21
    • 1970-01-01
    • 2014-10-25
    • 1970-01-01
    相关资源
    最近更新 更多