如何为获取动态参数的方法创建一个线程 [英] How to Create a thread for method which takes dynamic parameters

查看:79
本文介绍了如何为获取动态参数的方法创建一个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在为获取动态参数的方法创建线程时遇到问题。

我正在使用c#3.5



这是我的方法

  public   void  ToggleFunction( params   int  [] values)
{
做点什么
}





我尝试了很多方法。但我在尝试的所有方面都面临着问题。



线程prvw =  new 线程(()= >  ToggleFunction(myInts)); 



创建线程时抛出错误字段初始化程序无法引用非静态字段,方法或属性



请帮助...

解决方案





我正在撰写一篇关于这个主题的文章,名为:如何:简化WPF中线程的使用


这是一个可以帮助你的预览:



  public   void  RunByNewThread(操作操作)
{
action.BeginInvoke(ar = > ActionCompleted(ar,res = > action.EndInvoke(res)),);
}





只需将您的匿名方法作为参数发送到此方法:)


< blockquote> PS:这是我承诺的文章:



如何:简化WPF中线程的使用 [ ^ ]


Hi
I am facing a problem when creating a thread for method which takes dynamic arguments.
I am using c# 3.5

This is my method

public void ToggleFunction(params int[] values)
{
  Do something
}



I tried many possible ways. But I am facing problem in all the ways I tried.

Thread prvw = new Thread(() => ToggleFunction(myInts));


While creating thread it throws error "A field initializer cannot reference the non-static field, method or propery"

Please help...

解决方案

Hi,

I'm writing an article about this subject called: "How To: Simplify the use of threads in WPF"

Here is a preview that can help you:

public void RunByNewThread(Action action)
{
  action.BeginInvoke(ar => ActionCompleted(ar, res => action.EndInvoke(res)), null);
}



Just send your anonymous method as a parameter to this method :)


P.S.: Here is the article I've promised:

How To: Simplify the Use of Threads in WPF[^]


这篇关于如何为获取动态参数的方法创建一个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆