如果我想定期检查是否有取消请求,我会在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) 如果我有两个对象,即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)