创建一个新的参数化线程
我想在不同的线程中运行我的函数,但问题是该函数需要参数。
如果我用不需要参数的函数(CPUPlay())尝试它,那就可以了:
private void OpenNewThread(bool open)
{
Thread thread = new Thread(new ThreadStart(CPUPlay));
}
但是如果我用需要参数的函数尝试它,它不起作用:
private void OpenNewThread(bool open)
{
Thread thread = new Thread(new ParameterizedThreadStart(CloseOpenAnimation));
thread.Start(open);
}
那么我如何运行带有参数的函数不同的线程?
I want to run my function in a different thread but the problem is that the function needs a paramater.
If I try it with a function that doesn't need paramters(CPUPlay()) its ok:
private void OpenNewThread(bool open)
{
Thread thread = new Thread(new ThreadStart(CPUPlay));
}
But if I try it with a function that needs parameters, it doesn't work:
private void OpenNewThread(bool open)
{
Thread thread = new Thread(new ParameterizedThreadStart(CloseOpenAnimation));
thread.Start(open);
}
So how can I run a function with parameters in a different thread?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的函数与这个签名匹配吗?
Does your function match this signature?
欺骗和利用代表
Cheat and use delegates
尝试BackgroundWorker对象。您可以使用一个对象参数 Argument。
其中 e.Argument 是对象类型。
Try BackgroundWorker object. There is an object parameter Argument you can use.
where e.Argument is of type object.