在WinForm按钮上的单独线程中运行EF6查询单击事件 [英] Run EF6 Query in separate Thread on WinForm Button Click Event

查看:167
本文介绍了在WinForm按钮上的单独线程中运行EF6查询单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对于实体框架和多线程非常新鲜,并发现在做出大型EF查询时如何不锁定我的winforms UI有点混乱。



所以,假设我想按一下按钮,查询我的数据库并返回一个大的数据集,而不锁定UI,并能够向我的用户发送消息,查询正在进行中,我不知道如何我试图(显然不正确地)实现Jon Skeet的建议,从这里,所以我创建了以下代码,但是我收到错误,知道我做错了。我只是不明白如何做到正确...请任何链接/建议/示例代码将是**非常感谢!



我当前的代码(给出错误):

 私有Async Sub Button1_Click(发件人作为对象,e作为EventArgs)句柄Button1.Click 

作为新列表(DB_Data)

Val1 =Val1
Val2 =Val2

Dim qry As task = Task.Run(Function()GetDBInfo(Val1,Val2))
y = Await qry
End Sub

我想要异步运行的方法(我不知道这应该是 Async 还是不) strong>

 私有异步函数GetDBInfo(Val1 As String,Val2 As string)As Task(Of List(Of DB_Data))

Dim retval As List(Of DB_Data)

使用x作为新的DB_Context
retval =等待(从rw在x.DB_Data
其中rw.Val1 = Val1和
rw.Val2 = Val2
选择rw).ToListAsync
结束使用

返回retval
结束函数






再次,正如我所说,这不工作,可能是死了错误 - 我只是不知道更好的如何做到正确...任何帮助将不胜感激! - 即使这是在VB中,我也同样喜欢VB / C#解决方案。



谢谢!!

解决方案

您正处于正确的轨道,但您不需要使用EF 6的 Task.Run 方法。EF 6来使用实际异步方法,例如 ToListAsync 。这些方法在场景背后使用新的ADO.NET异步方法,并且根本不消耗或创建任何线程。 Task.Run 旨在调用CPU绑定方法,更多在这里。此外,它启动线程池上的任务进行计算,更多在这里。但是,实际异步方法根本不使用任何线程, more here 。所以只需使用

  var result = await(from b in db.Blogs orderby b.Name select b).ToListAsync()

足以释放GUI线程。也不要在这里使用阻止方法和属性,如wait或result,更多信息


I am very new to both Entity Framework AND multi-threading and finding it a bit confusing how to not lock up my winforms UI while making a large EF query.

So, suppose I want to, on a button click, query my database and return a large dataset while not locking up the UI and being able to send messages to my user that the query is in progress, I don't know how to do this AT ALL, but am trying!!

I tried to (obviously incorrectly) implement Jon Skeet's advice from here, and so I've created the following code, but I'm getting errors and know I'm doing it wrong. I just don't understand how to do it right... PLEASE any links / advice / sample code would be **VERY* much appreciated!!

My current code (giving errors):

Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

   Dim y As New List(Of DB_Data)

    Val1 = "Val1"
    Val2 = "Val2"

    Dim qry As task = Task.Run(Function() GetDBInfo(Val1, Val2))
    y = Await qry
End Sub

The method I want to run Asynchronously (I have no clue if this should be Async or not) :

Private Async Function GetDBInfo(Val1 As String, Val2 As string) As Task(Of List(Of DB_Data))

    Dim retval As List(Of DB_Data)

    Using x As New DB_Context
        retval = Await (From rw In x.DB_Data
                         Where rw.Val1 = Val1 And
                               rw.Val2 = Val2
                         Select rw).ToListAsync
    End Using

    Return retval
End Function


Again, as I said, this is not working and could be dead wrong - I just don't know any better how to do it right... Any help would be greatly appreciated! - And even though this is in VB, I'm equally comfortable with VB / C# solutions.

Thanks!!

解决方案

You are on the right track but you don't need the Task.Run method with EF 6. EF 6 comes with real async methods such as ToListAsync. These methods are using new ADO.NET's async methods behind the scene and they don't consume or create any threads at all. Task.Run is designed to call CPU-bound methods, more here. Also it starts a task on the thread pool to do the calculations, more here. But real async methods do not use any threads at all, more here. So just using

var result = await (from b in db.Blogs orderby b.Name select b).ToListAsync()

is enough to free the GUI thread. Also don't use blocking methods and properties here such as wait or result, more info.

这篇关于在WinForm按钮上的单独线程中运行EF6查询单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆