我添加了一个运行存储过程的仪表板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.DashboardInternalException'发生在DevExpress.Dashboard.v15.2.Win.dll
其他信息:内部错误。请与应用程序供应商或系统管理员联系,并提供以下信息。
System.InvalidOperationException:当前的SynchronizationContext可能不会用作TaskScheduler。
at System.Threading.Tasks.SynchronizationContextTaskScheduler..ctor()
在DevExpress.DashboardWin.Native.WinDashboardService.RequestCustomizationServices(RequestCustomizationServicesEventArgs e)
在DevExpress.DashboardCommon.Service.DashboardService.DevExpress.DashboardCommon.Service.IDashboardServiceAdminHandlers.OnRequestCustomizationServices(Object sender,RequestCustomizationServicesEventArgs e)
在DevExpress.DashboardCommon.Server.DashboardSession.CreateDataLoaderParameters(ReloadDataArgs args) 在DevExpress.DashboardCommon.Server.DashboardSession.CreateDataLoader(ReloadDataArgs args)
在DevExpress.DashboardCommon.Server.DashboardSession.LoadData(IEnumerable1 dataSourceComponentNames, ReloadDataArgs args)
1 dataSourceComponentNames,ReloadDataArgs args)
at DevExpress.DashboardCommon.Server.DashboardSession.ReloadData(IEnumerable
在DevExpress.DashboardCommon.Server.DashboardSession.Initialize(DashboardSessionState状态,布尔isDesignMode)
在DevExpress.DashboardCommon.Service.DashboardServiceOperation`1.Execute(DashboardServiceResult result)
已更新
我改变了我的代码,它没有任何错误,数据显示但没有任何异步操作,我不得不等待加载数据
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);
}
答案 0 :(得分:3)
如果没有问题的完整上下文,我无法为您提供准确的解决方案,但总体而言,您无法从其他线程访问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);
}