在C#中使用异步Memorystream方法获取数据异步 [英] async Memorystream approach in c# to get data async

查看:140
本文介绍了在C#中使用异步Memorystream方法获取数据异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的仪表板需要太多时间从数据库中获取数据,因此我必须使用 async 方法来处理此问题,这是我的代码:

My dashboard needs too much time to get data from database so I have to use async approach to handle this problem here is my code :

public async Task < Stream > LoadDashboard() {
   Stream s = new MemoryStream(Encoding.Default.GetBytes(Resource.Dashboard));
   s.Position = 0;
   return s;
}

private async void frmMaterialDashboard_Load(object sender, EventArgs e) {
   Stream dashboardData = await LoadDashboard();
   dashboardViewer1.LoadDashboard(dashboardData);

  //show UI components for user interact
}

我的代码不起作用,我必须等待数据从数据库中获取.我还应该添加其他内容吗?

My code doesn't work and I have to wait for data to come from the database. Should I add anything else ?

这部分代码需要很长时间才能加载数据

This part of code takes long time to load data

 Stream s = new MemoryStream(Encoding.Default.GetBytes(Resource.Dashboard));
 s.Position = 0;

我要异步执行此部分.加载表单后,我想调用 LoadDashboard 作为后台任务从数据库中获取数据,并且主线程显示我的用户界面表单.

I want to execute this part async. When my form is loaded I want to call LoadDashboard as a background task to get the data from database ,and the main thread show my user interface form .

我正在使用的组件链接:

The component link that I am using :

https://documentation.devexpress.com/#Dashboard/CustomDocument113927

推荐答案

从看起来您没有可以执行的实际异步工作来看,您正在将资源读取到内存流中.将 async 关键字放在某物上本身不会执行任何操作,该功能仍会像以前一样运行.如果您希望工作在后台进行,则必须使用新线程来告诉它在后台工作.

From what it looks like you have no actual async work you can do, you are reading a resource in to a memory stream. Putting the async keyword on somthing does nothing by itself, the function still runs just like it used to. If you want the work to happen in the background you have to tell it to work in the background by using a new thread.

//Get rid of this async stuff here.
public Stream LoadDashboard()
{

    Stream s = new MemoryStream(Encoding.Default.GetBytes(Resource.Dashboard));
    s.Position = 0;
    return s;

}

private async  void frmMaterialDashboard_Load(object sender, EventArgs e)
{
    //Start LoadDashboad in a background thread and await it.
    Stream dashboardData = await Task.Run(() => LoadDashboard());
    dashboardViewer1.LoadDashboard(dashboardData);

    //show UI components for user interact
}


另一种选择是不将字符串复制到内存流中,而是直接获取该流


Another option is to not copy the string to a memory stream and instead get the stream directly

private void frmMaterialDashboard_Load(object sender, EventArgs e)
{
    using (var dashboardStream = Resources.ResourceManager.GetStream("Dashboard"))
    {
        dashboardViewer1.LoadDashboard(dashboardStream);
    }

    //show UI components for user interact
}

我摆脱了异步,因为DashboadViewer并未提供一种从背景尽我所知的方式调用LoadDashboard的方法.您将不得不等到加载完成或弄清楚如何获取较小的数据.

I got rid of the async because DashboadViewer does not provide a way to call LoadDashboard from the background to the best of my knowledge. You will have to wait till it finishes loading or figure out how to get smaller data.

这篇关于在C#中使用异步Memorystream方法获取数据异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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