Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从多个线程更新DataGrid_C#_Wpf_Multithreading_Datagrid_Timeout - Fatal编程技术网

C# 从多个线程更新DataGrid

C# 从多个线程更新DataGrid,c#,wpf,multithreading,datagrid,timeout,C#,Wpf,Multithreading,Datagrid,Timeout,我想从WPF(c#)中的多线程更新我的数据网格。我使用dataGrid.Dispatcher.BeginInvoke()和dataGrid.Dispatcher.Invoke(),但它们冻结了程序(主线程)。如何在超时的情况下从多个线程更新dataGrid(因为我使用的web服务可能无法访问)。如果不需要在线程中进行dataGrid编辑,可以在主线程中运行它们,如下所示: this.Invoke((Action)delegate { //Edit the DataGrid however

我想从WPF(c#)中的多线程更新我的数据网格。我使用dataGrid.Dispatcher.BeginInvoke()和dataGrid.Dispatcher.Invoke(),但它们冻结了程序(主线程)。如何在超时的情况下从多个线程更新dataGrid(因为我使用的web服务可能无法访问)。

如果不需要在线程中进行dataGrid编辑,可以在主线程中运行它们,如下所示:

this.Invoke((Action)delegate
{
    //Edit the DataGrid however you like in here
});

确保只将需要在主线程中运行的内容放在其中(否则会破坏多线程的功能)。

如果不需要在线程中进行DataGrid编辑,可以在主线程中运行它们,如下所示:

this.Invoke((Action)delegate
{
    //Edit the DataGrid however you like in here
});

确保只将需要在主线程中运行的内容放在其中(否则会破坏多线程的目的)。

使用
任务
异步启动web服务请求。为此,您可能需要将EAP(基于事件的异步模式)样式转换为TAP(基于任务的异步模式)样式。下面是你如何做到这一点

private Task<IEnumerable<YourDataItem>> CallWebServiceAsync()
{
  var tcs = new TaskCompletionSource();
  var service = new YourServiceClient();
  service.SomeOperationCompleted +=
    (sender, args) =>
    {
      if (args.Error == null)
      {
        tcs.SetResult(args.Result);
      }
      else
      {
        tcs.SetException(args.Error);
      }
    };
  service.SomeOperationAsync();
  return tcs.Task;
}
就是这样!没有比这更优雅的了。这将在后台线程上异步执行操作,然后将结果绑定到UI线程上的
DataGrid


如果WCF服务无法访问,则它将抛出异常,并将附加到
任务
,以便它向上传播到
等待
调用。此时,它将被注入到执行中,如有必要,可以使用
try catch
进行包装。

使用
任务
异步启动web服务请求。为此,您可能需要将EAP(基于事件的异步模式)样式转换为TAP(基于任务的异步模式)样式。下面是你如何做到这一点

private Task<IEnumerable<YourDataItem>> CallWebServiceAsync()
{
  var tcs = new TaskCompletionSource();
  var service = new YourServiceClient();
  service.SomeOperationCompleted +=
    (sender, args) =>
    {
      if (args.Error == null)
      {
        tcs.SetResult(args.Result);
      }
      else
      {
        tcs.SetException(args.Error);
      }
    };
  service.SomeOperationAsync();
  return tcs.Task;
}
就是这样!没有比这更优雅的了。这将在后台线程上异步执行操作,然后将结果绑定到UI线程上的
DataGrid


如果WCF服务无法访问,则它将抛出异常,并将附加到
任务
,以便它向上传播到
等待
调用。此时,它将被注入到执行中,如果需要,可以使用
try-catch
进行包装。

我猜您是在Dispatcher调用的方法中进行web服务调用。这当然会冻结用户界面。从后台线程调用web服务(在调用
Invoke
之前),并仅通过
Invoke
将结果传递给UI线程。能给我举个例子吗?我是多线程方面的新手。我猜您是在调度程序调用的方法中进行web服务调用的。这当然会冻结用户界面。从后台线程调用web服务(在调用
Invoke
之前),并仅通过
Invoke
将结果传递给UI线程。能给我举个例子吗?我是多线程新手。->你的答案中没有超时。它还抛出一个异常。不过谢谢你……。@Babak.Abad真的吗?它会抛出什么异常?因为我用过,而且一直都很有效。你会把超时放在调用之后,这样它会在线程中运行,而不是在主线程中运行。你是说这个.Dispatcher.Invoke(…)?我看不出有什么例外。但我看到我的程序被冻结了+谢谢你的帮助。如果有其他解决方案,请帮助我->您的答案不包含超时。它还抛出一个异常。不过谢谢你……。@Babak.Abad真的吗?它会抛出什么异常?因为我用过,而且一直都很有效。你会把超时放在调用之后,这样它会在线程中运行,而不是在主线程中运行。你是说这个.Dispatcher.Invoke(…)?我看不出有什么例外。但我看到我的程序被冻结了+谢谢你的帮助。如果有其他解决方案,请帮助我