在.NET Core/Change视图中创建加载视图而不返回c#
我正在开发一个在.NETCore中创建的网站(使用完整的.NETFramework),该网站使用后台任务获取设备列表 我希望在任务从另一台PC获取数据(使用请求)时显示加载“视图”,然后在任务完成时显示带有设备的表格。我该怎么做 下面是我的一段代码:在.NET Core/Change视图中创建加载视图而不返回c#,c#,asynchronous,asp.net-core,asp.net-core-mvc,loading,C#,Asynchronous,Asp.net Core,Asp.net Core Mvc,Loading,我正在开发一个在.NETCore中创建的网站(使用完整的.NETFramework),该网站使用后台任务获取设备列表 我希望在任务从另一台PC获取数据(使用请求)时显示加载“视图”,然后在任务完成时显示带有设备的表格。我该怎么做 下面是我的一段代码: public class DeviceController : Controller { public IActionResult Index() { if (DataSyncronizer.getDeviceListT
public class DeviceController : Controller {
public IActionResult Index() {
if (DataSyncronizer.getDeviceListTask.Status == TaskStatus.Running) {
// TODO Show the loading screen here.
// return this.View("Loading");
}
if (DataSyncronizer.getDeviceListTask.Status == TaskStatus.Faulted) {
ViewData["ErrorTitle"] = "Errore di sincronizzazione";
ViewData["ErrorText"] = "Cannot get devices";
return this.View("Error");
}
if (DataSyncronizer.getDeviceListTask.Status == TaskStatus.Canceled) {
ViewData["ErrorTitle"] = "Errore di sincronizzazione";
ViewData["ErrorText"] = "";
return this.View("Error");
}
return this.View(DataSyncronizer.Devices);
}
这是获取设备列表的函数:
public static class DataSyncronizer {
public static Task<List<Device>> getDeviceListTask { get; private set; }
public static List<Device> Devices = new List<Device>();
public static Task UpdateDevices() {
getDeviceListTask = new Task<List<Device>>(() =>
Device.GetMyDevicesList(meUser));
getDeviceListTask.ContinueWith((result) => {
DataSyncronizer.Devices = result.Result;
}, TaskScheduler.Current);
getDeviceListTask.Start();
return getDeviceListTask;
}
}
公共静态类数据同步器{
公共静态任务getDeviceListTask{get;private set;}
公共静态列表设备=新列表();
公共静态任务更新设备(){
getDeviceListTask=新任务(()=>
Device.getMyDeviceList(meUser));
getDeviceListTask.ContinueWith((结果)=>{
DataSyncronizer.Devices=result.result;
},TaskScheduler.Current);
getDeviceListTask.Start();
返回getDeviceListTask;
}
}
您可以在调用UpdateDevices()之前显示加载程序。
将此添加到任务末尾
范例
var webTask = Task.Run(() =>
{
try
{
wcf.UploadMotionDynamicRaw(bytes); //my web service
}
catch (Exception ex)
{
//deal with error
}
}).ContinueWith(t => "Function to hide loader");
是的,我可以这样做,但我不想这样做,因为用户可能会在设备列表仍在加载时执行其他操作。如果用户要求提供设备列表,但该列表尚未准备好,我只想显示加载视图。修改帖子以获得可能有用的解决方案。是的,但是我如何在不返回IActionResult的情况下隐藏加载程序(重定向到/devices/list)?您必须返回结果。客户端如何知道服务器是否处理了它的请求?您必须返回一个结果。客户机如何知道服务器是否完成了对其请求的处理?在结果中,您需要告诉客户机隐藏加载程序。
var webTask = Task.Run(() =>
{
try
{
wcf.UploadMotionDynamicRaw(bytes); //my web service
}
catch (Exception ex)
{
//deal with error
}
}).ContinueWith(t => "Function to hide loader");