小编dev*_*101的帖子

是否有一种干净的方法来检查BackgroundWorker中的取消请求,而无需一遍又一遍地重新键入相同的代码?

如果我想定期检查是否有取消请求,我会在DoWork事件处理程序中不断使用下面的代码:

    if(w.CancellationPending == true)
    {
        e.Cancel = true;
        return;
    }  
Run Code Online (Sandbox Code Playgroud)

有没有一种干净的方法来检查BackgroundWorkerC#中的取消请求而无需一遍又一遍地重新键入相同的代码?

请参考以下代码:

void worker_DoWork(object sender, DoWorkEventArgs e)
{
    ...

    BackgroundWorker w = sender as BackgroundWorker;

    if(w.CancellationPending == true)
    {
        e.Cancel = true;
        return;
    }

    some_time_consuming_task...

    if(w.CancellationPending == true)
    {
        e.Cancel = true;
        return;
    }

    another_time_consuming_task...

    if(w.CancellationPending == true)
    {
        e.Cancel = true;
        return;
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

c#

5
推荐指数
1
解决办法
162
查看次数

如何在Winforms中将对象集合绑定到DataGridView

如果我有两个对象,即Fruit' andColor`及其定义如下:

public class Fruit  
{  
  public int FruitId { get; set; }  
  public string Name { get; set; }  
  public Color Color { get; set; }  
}  

public class Color  
{  
  public int ColorId { get; set; }  
  public string Name { get; set; }  
}  
Run Code Online (Sandbox Code Playgroud)

如何将的集合绑定Fruit (e.g. List<Fruit>)到DataGridView?结果输出将类似于以下内容:

+-----+--------+----------+  
| Id  | Name   | Color    |  
+-----+--------+----------+  
| 10  | Apple  | Red      |  
| 20  | Orange | Orange   |  
| 30  | …
Run Code Online (Sandbox Code Playgroud)

c# mvp architectural-patterns winforms

3
推荐指数
2
解决办法
1万
查看次数

标签 统计

c# ×2

architectural-patterns ×1

mvp ×1

winforms ×1