I have a layered application. The GUI layer clicking a button, calls my business layer that calls a method from the data layer to access the database and bring a list of users.
我有一个分层的应用程序。GUI层单击一个按钮,调用我的业务层,业务层从数据层调用一个方法来访问数据库并带来一个用户列表。
How to call the data layer methods without locking the GUI with the User.
如何在不与用户锁定GUI的情况下调用数据层方法。
The methods of the data layer must receive parameters and return parameters.
数据层的方法必须接收参数和返回参数。
For example: List all active users in the database.
例如:列出数据库中的所有活动用户。
IEnumerable <users> lstUsr = db.ListUsers(bool isActive);
2 个解决方案
#1
1
Rather than incurring the cost of thread management, let the task parallel library do this for you:
不要产生线程管理的成本,让任务并行库为您做以下工作:
protected void SomeButton_OnClick(Object sender, EventArgs e)
{
// A new task is not necessarily a new thread!
Task.Factory.StartNew(() => PopulateSomeControl());
}
private async Task RequestDataAndPopulateTheControl()
{
Statusbar.Text = "Loading from DB...";
// this blocks only this method, not the UI thread.
var data = await RequestData();
// data is now available
foreach(var whatever in data.ToList())
SomeControl.Items.Add(whatever);
Statusbar.Text = "Ready.";
}
private async Task<DataTable> RequestData()
{
var db = new YourDbContext();
return db.SomeDbSet().ToDataTable();
}
The method RequestDataAndPopulateTheControl()
now becomes a readable union of your asynchronous threaded request for data as well as the callback to use the data. The UI thread is not blocked.
RequestDataAndPopulateTheControl()方法现在成为数据的异步线程请求和使用数据的回调的可读联合。UI线程没有被阻塞。
#2
1
As your tag indicates, spin up a thread! You just need to be mindful of needing Dispatcher.BeginInvoke to put the UI piece back on the UI thread when you are done.
当你的标签指示,旋转一个线程!您只需要注意需要调度程序。开始调用,在完成之后将UI块放回UI线程。
new Thread(() =>
{
IEnumerable<User> lstUsr = db.ListUsers(isActive);
Distpatcher.BeginInvoke(() =>
{
myUiVariable = lstUsr;
}
}
If you have access to an async Data Access Layer and are in .NET 4.0+, you can take advantage of async/await
如果您访问了一个异步数据访问层,并且在。net 4.0+中,您可以利用异步/等待。
myUiVariable = await db.ListUsersAsync(isActive);
#1
1
Rather than incurring the cost of thread management, let the task parallel library do this for you:
不要产生线程管理的成本,让任务并行库为您做以下工作:
protected void SomeButton_OnClick(Object sender, EventArgs e)
{
// A new task is not necessarily a new thread!
Task.Factory.StartNew(() => PopulateSomeControl());
}
private async Task RequestDataAndPopulateTheControl()
{
Statusbar.Text = "Loading from DB...";
// this blocks only this method, not the UI thread.
var data = await RequestData();
// data is now available
foreach(var whatever in data.ToList())
SomeControl.Items.Add(whatever);
Statusbar.Text = "Ready.";
}
private async Task<DataTable> RequestData()
{
var db = new YourDbContext();
return db.SomeDbSet().ToDataTable();
}
The method RequestDataAndPopulateTheControl()
now becomes a readable union of your asynchronous threaded request for data as well as the callback to use the data. The UI thread is not blocked.
RequestDataAndPopulateTheControl()方法现在成为数据的异步线程请求和使用数据的回调的可读联合。UI线程没有被阻塞。
#2
1
As your tag indicates, spin up a thread! You just need to be mindful of needing Dispatcher.BeginInvoke to put the UI piece back on the UI thread when you are done.
当你的标签指示,旋转一个线程!您只需要注意需要调度程序。开始调用,在完成之后将UI块放回UI线程。
new Thread(() =>
{
IEnumerable<User> lstUsr = db.ListUsers(isActive);
Distpatcher.BeginInvoke(() =>
{
myUiVariable = lstUsr;
}
}
If you have access to an async Data Access Layer and are in .NET 4.0+, you can take advantage of async/await
如果您访问了一个异步数据访问层,并且在。net 4.0+中,您可以利用异步/等待。
myUiVariable = await db.ListUsersAsync(isActive);