使用字符串类型参数进行线程处理
使用 ParameterizedThreadStart 几乎可以工作,但它需要对象作为参数,这感觉非常错误。是否可以通过 ParameterizedThreadStart 传递字符串?
public void OpenUDirectory(String Directory)
{
Items.Clear();
foreach (FileInfo FI in new DirectoryInfo(Directory).GetFiles())
{
Items.Add(FI.Name);
}
}
我想出了向 ParameterizedThreadStart 添加扩展构造函数的想法,以便我可以将 String 转换为 Object 并调用基本方法,但是有没有更干净的方法呢?
我确定我需要调用 Invoke 方法,以便我有一个委托:
public delegate void OpenDD(String Directory);
Using ParameterizedThreadStart would almost work but it requires object as parameter, which feels very wrong. Is it possible to pass a String through ParameterizedThreadStart?
public void OpenUDirectory(String Directory)
{
Items.Clear();
foreach (FileInfo FI in new DirectoryInfo(Directory).GetFiles())
{
Items.Add(FI.Name);
}
}
I came up with the idea of adding an extension constructor to ParameterizedThreadStart so that I could cast String to Object and call the base method, but is there a cleaner way?
I'm sure I need to call the Invoke method so I have a delegate:
public delegate void OpenDD(String Directory);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您启动线程时,您必须将其包装为
对象
。然后将其转换回string
并调用您的方法。这就是它的工作方式,因为它提供了最大范围的灵活性。
You have to box is as an
object
when you start the thread. Then cast it back to astring
and call your method.That is the way it works since it gives the greatest range of flexibility.