首页 \ 问答 \ 在主线程中执行Action <>(perform Action<> in main thread)

在主线程中执行Action <>(perform Action<> in main thread)

我正在编写一个应用程序,它在后台执行很长的请求。 每次请求后我都需要将结果发送到主表单。

所以,这是一个代码:

Form1.cs的

private async void StartButton_Click(object sender, EventArgs e)
{
    await Logic.GenerateStackAsync(stackSettings, delegate(FullOrder transaction)
          {
              lastOrderId.Text = transaction.OrderId;
          }
    );

    MessageBox.Show("Completed!");
}

Logic.cs:

    public static bool GenerateStack(StackSettings stackSettings, Action<FullOrder> onOrderCreated = null)
    {
         for(var i = 0; i < 10; i++) 
         {
              // long, long request, replaced with:
              System.Threading.Thread.Sleep(10000);
              if (onOrderCreated != null)
              {
                  onOrderCreated.Invoke(order);
                  // tried to change it with onOrderCreated(order), no results.
              }
          }

          return true;
    }

    public static Task<bool> GenerateStackAsync(StackSettings stackSettings, Action<FullOrder> onOrderCreated)
    {
        return TaskEx.Run(() => GenerateStack(stackSettings, onOrderCreated));
    }

它抛出一个异常:“控制'lastOrderId'从一个线程访问,而不是它创建的线程。”,可以通过添加CheckForIllegalCrossThreadCalls = false;来修复CheckForIllegalCrossThreadCalls = false; ,但我认为这是一次糟糕的经历。 怎么做对了? 先谢谢你。

PS抱歉英语不好。


I'm writing an app, that performs very long requests at background. After each request I need to send result to main form.

So, here is a code:

Form1.cs

private async void StartButton_Click(object sender, EventArgs e)
{
    await Logic.GenerateStackAsync(stackSettings, delegate(FullOrder transaction)
          {
              lastOrderId.Text = transaction.OrderId;
          }
    );

    MessageBox.Show("Completed!");
}

Logic.cs:

    public static bool GenerateStack(StackSettings stackSettings, Action<FullOrder> onOrderCreated = null)
    {
         for(var i = 0; i < 10; i++) 
         {
              // long, long request, replaced with:
              System.Threading.Thread.Sleep(10000);
              if (onOrderCreated != null)
              {
                  onOrderCreated.Invoke(order);
                  // tried to change it with onOrderCreated(order), no results.
              }
          }

          return true;
    }

    public static Task<bool> GenerateStackAsync(StackSettings stackSettings, Action<FullOrder> onOrderCreated)
    {
        return TaskEx.Run(() => GenerateStack(stackSettings, onOrderCreated));
    }

It throws an exception: "Control 'lastOrderId' accessed from a thread other than the thread it was created on.", which can be fixed by adding CheckForIllegalCrossThreadCalls = false;, but I think that this is a bad experience. How make it right? Thank you in advance.

P.S. Sorry for bad English.


原文:https://stackoverflow.com/questions/21354455
更新时间:2023-08-12 19:08

最满意答案

您将不得不使用ng-grid插件Flexible Height Plugin 。 将此插件添加到网格选项的plugins属性中。

$scope.gridOptions = {
    data: 'nagruzkaData',
    enableColumnResize: true,
    showGroupPanel: true,
    plugins: [new ngGridFlexibleHeightPlugin()]
};

Eventually, as I need to solve that problem as quick as I could, I ended up just triggering empty 'resize' event via jQuery, which made ng-grid rebuild and show all data received from the server.

相关问答

更多
  • 最好使用ng-options并将模型设置为您想要选择的选项。 并在控制器中,设置: $scope.selectedItem = "age"; // equal to the value of the default option It's better ...
  • 有一个csv导出插件: https : //github.com/angular-ui/ng-grid/tree/master/plugins 可以通过将其添加到网格选项中轻松添加,同时确保显示页脚。 $scope.gridOptions = { data: ..., plugins: [new ngGridCsvExportPlugin()], showFooter: true, ... }; 它从您指定的grid.data导 ...
  • 这里有几件事情要做: 与您的确切问题无关,但您的firstName字段中嵌套了单引号。 你需要做一些转义或者你的ng-click表达式会被打破。 因此,您可以使用ng-click=\"show('F:/cox/main.html')\"来代替ng-click='show('F:/cox/main.html')' 。 也与您的具体问题无关,但如果您想从UI-Grid内部访问控制器范围内的属性,则需要使用appScope 。 关于它的文档在这里: http : //ui-grid.info/docs/#/tut ...
  • 您需要设置filterOptions属性 $scope.filterOptions = { filterText: 'Status:Valid', useExternalFilter: true }; $scope.gridOptions = { data: 'Questions', filterOptions: $scope.filterOptions }; You need to set the filterOptions property of gridOption ...
  • 您将不得不使用ng-grid插件Flexible Height Plugin 。 将此插件添加到网格选项的plugins属性中。 $scope.gridOptions = { data: 'nagruzkaData', enableColumnResize: true, showGroupPanel: true, plugins: [new ngGridFlexibleHeightPlugin()] }; Eventually, as I need to solve tha ...
  • 像往常一样,当你发现自己需要任意数量的对象时,你应该考虑将它们放在一个集合中而不是给它们个别名字。 我已经更新了wayne的jsfiddle以使用数组和ng-repeat: http : //jsfiddle.net/Th47J/ 为每个选项创建一个网格:
    显然,如果这些是动态创建的,您将需要以编程方式更新gridOptions数组,而不是将所有 ...
  • 好吧,它肯定是ng-grid 2.0.7中的某种bug,就像我升级到(尚未发布)的2.0.8( https://github.com/angular-ui/ng- grid / tree / 2.0.8 ),问题就消失了。 仍然不确定它到底是什么,但是到了下一个故事...... Well, it must have been a bug of some sort in ng-grid 2.0.7, as when I upgraded to the (not-yet-released) 2.0.8 (htt ...
  • 以这种方式检查你的状况。 因此,您不必搜索最后选定的行,您可以直接取消选择它。 我已经测试过了,并且正在以这种方式工作。 $scope.yourGridApi.selection.on.rowSelectionChanged($scope,function(row){ if($scope.yourGrid.gridOptions.selectedItems.length>=5){ row.isSelected = false; } }); ...
  • 对不起,我的坏。我忘了在我的网格上添加enableCellSelection,之后,我能够导航我的网格 Sorry,my bad.I forgot to add enableCellSelection on my grid after which, i am able to navigate my grid
  • 我遇到了同样的问题并尝试了一些我发现的解决方案,但是没有使用我的网格版本。 我搜索了网格源并找到了我用于聚合行的模板并将其复制然后调整以包含对我的方法上的函数的调用以显示部分: 在我的gridOptions上,我定义了下面显示的属性 aggregateTemplate: "
    " + "

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。