我有以下代码,你可以看到后台工作程序搜索文件,并且在进度中更改了事件文件被添加到listview,但是由于有很多文件被添加到listview,UI变得没有响应,我可以睡在循环中的线程,但我不认为这是一个很好的做法,什么是防止UI冻结的最佳方法?
为了详细说明,listview是表单上的表单控件.
void bg_DoWork(object sender, DoWorkEventArgs e)
{
Stack<string> dirs = new Stack<string>(20);
dirs.Push(e.Argument.ToString());
while (dirs.Count > 0)
{
string currentDir = dirs.Pop();
string[] subDirs;
try { subDirs = System.IO.Directory.GetDirectories(currentDir); }
catch (UnauthorizedAccessException) { continue; }
catch (System.IO.DirectoryNotFoundException) { continue; }
string[] files = null;
try { files = System.IO.Directory.GetFiles(currentDir); }
catch (UnauthorizedAccessException) { continue; }
catch (System.IO.DirectoryNotFoundException) { continue; }
foreach (var file in files) { bg.ReportProgress(0, file); }
foreach (string str in subDirs) { dirs.Push(str); }
}
} …Run Code Online (Sandbox Code Playgroud) 为什么我不能通过它的接口(ItestClass)访问基类(testClass)属性?我创建了接口以避免实际的Control(winforms/wpf)属性在第三个类(newClass)中可见.如果那不可能有更好的方法吗?
public class testClass : Control, ItestClass
{
public int Property1 { set; get; }
public int Property2 { set; get; }
public testClass() { }
}
public interface ItestClass
{
int Property1 { get; set; }
int Property2 { get; set; }
}
public class newClass : ItestClass
{
public newClass()
{
// Why are the following statements are not possible?
Property1 = 1;
// OR
this.Property1 = 1;
}
}
Run Code Online (Sandbox Code Playgroud) 使长话短说.我有以下代码:
class MyList : IEnumerable
{
private List<string> T1 = new List<string>();
private List<string> T2 = new List<string>();
private List<string> T3 = new List<string>();
public List<string> Name { set { T1 = value; } get { return T1; } }
public List<string> DataType { set { T2 = value; } get { return T2; } }
public List<string> Nullable { set { T3 = value; } get { return T3; } }
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)GetEnumerator();
}
public MyList<List<string>, …Run Code Online (Sandbox Code Playgroud)