Start线程返回System.InvalidOperationException:当前SynchronizationC

本文关键字:当前 SynchronizationC InvalidOperationException System 线程 返回 Start | 更新日期: 2023-09-27 18:07:12

我添加了一个运行存储过程的仪表板devexpress,它花费了很长时间。所以我在form_load中创建了一个简单的线程

public void LoadDashboard()
{
    using (Stream s = new MemoryStream(Encoding.Default.GetBytes(Resource.Dashboard.MaterialDashboard1)))
    {
         s.Position = 0;
         dashboardViewer1.LoadDashboard(s);
    }
}
private void frmMaterialDashboard_Load(object sender, EventArgs e)
{
    Thread newth=new Thread(LoadDashboard);
    newth.Start();
    int UserId = int.Parse(Configuration.AccountDetail.UserId.ToString());
    lblUserName.Caption = _userRepository.Get().Where(i => i.Id == UserId).First().FullName;
    alertControl1.Show(this, "Welcome","Welcome to SPMS Mr."+_userRepository.FindById(Configuration.AccountDetail.UserId).First().FullName +"'n Server time:"+DateTime.Now);
}

但是当我运行我的应用程序时,我得到这个错误:

类型为'DevExpress.DashboardCommon '的未处理异常。

DevExpress.Dashboard.v15.2.Win.dll中出现DashboardInternalException

附加信息:内部错误。请与应用程序供应商或您的系统管理员联系,并提供以下信息。

系统。InvalidOperationException:当前SynchronizationContext不能被用作TaskScheduler。

在System.Threading.Tasks.SynchronizationContextTaskScheduler . .男星()
在DevExpress.DashboardWin.Native.WinDashboardService。RequestCustomizationServices (RequestCustomizationServicesEventArgs e)
在DevExpress.DashboardCommon.Service.DashboardService.DevExpress.DashboardCommon.Service.IDashboardServiceAdminHandlers。OnRequestCustomizationServices(对象发送者,RequestCustomizationServicesEventArgs e)
在DevExpress.DashboardCommon.Server.DashboardSession。CreateDataLoaderParameters (ReloadDataArgs args)在DevExpress.DashboardCommon.Server.DashboardSession。CreateDataLoader (ReloadDataArgs args)
在DevExpress.DashboardCommon.Server.DashboardSession。LoadData(IEnumerable 1 dataSourceComponentNames, ReloadDataArgs args)
at DevExpress.DashboardCommon.Server.DashboardSession.ReloadData(IEnumerable
1 dataSourceComponentNames, ReloadDataArgs args)
在DevExpress.DashboardCommon.Server.DashboardSession。Initialize(DashboardSessionState state, Boolean isDesignMode)
在DevExpress.DashboardCommon.Service.DashboardServiceOperation 1。Execute(dashboardserviceresresult)

我像这样改变我的代码,它工作没有任何错误,数据显示,但没有任何异步操作,我必须等待加载数据

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);
    int UserId = int.Parse(Configuration.AccountDetail.UserId.ToString());
    lblUserName.Caption = _userRepository.Get().Where(i => i.Id == UserId).First().FullName;
    alertControl1.Show(this, "Welcome","Welcome to SPMS Mr."+_userRepository.FindById(Configuration.AccountDetail.UserId).First().FullName +"'n Server time:"+DateTime.Now);
}

Start线程返回System.InvalidOperationException:当前SynchronizationC

没有完整的问题背景,我无法给你一个确切的解决方案,但总的来说,你不能从另一个线程访问UI元素。这意味着你需要在另一个线程上完成所有的请求和计算,然后在UI线程上更新UI元素。考虑这样一个简化的解决方案,它不显式地启动一个新线程:

// event on UI thread    
private async void frmMaterialDashboard_Load(object sender, EventArgs e)
{
    var dashboardData = await LoadDashboardDataFromDatabaseAsync();
    dashboardViewer1.Load(dashboardData);
}
public async Task<DashboardData> LoadDashboardDataFromDatabaseAsync()
{
    string query = "...";
    var queryResult = await db.ExucuteQueryAsync(query).ConfigureAwait(false);
    return ConvertQueryRequltToDashboardData(queryResult);
}