开发者

Threading with parameters of type string

开发者 https://www.devze.com 2023-03-31 14:58 出处:网络
Using ParameterizedThreadStart wou开发者_如何学Gold almost work but it requires object as parameter, which feels very wrong. Is it possible to pass a String through ParameterizedThreadStart?

Using ParameterizedThreadStart wou开发者_如何学Gold 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);


You have to box is as an object when you start the thread. Then cast it back to a string and call your method.

That is the way it works since it gives the greatest range of flexibility.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号