2013-06-12 44 views
0

我在c#中编写代码来完成图像处理。我想使用线程,我想在WPF应用程序线程是有点不同。我试图运行线程,但它只在函数为void()时才起作用,即不带任何参数。WPF C#线程

然而,我的功能正在3个arguemnts这样

frame_extract.Frame_Processing(_colorFrame_, widht, height); 

因此,因此以下不工作

depth_Threads = new System.Threading.Thread(**) since ** takes on void() type. 

也许我失去了一些东西,但我的问题是如何一起工作对带参数的函数进行线程化。

回答

2

也许你可以使用TPL。

应该再是这样的:

Task.Factory.StartNew(() => frame_extract.Frame_Processing(_colorFrame_, widht, height)); 

但要注意,你可能有元帅到UI线程。

如果你想创建在UI线程的线程,并希望新的线程与上述UI线程交互,类似下面应该工作:

var task = new Task(() => frame_extract.Frame_Processing(_colorFrame_, widht, height)); 
task.Start(TaskScheduler.FromCurrentSynchronizationContext()); 

这应该工作。

+0

它工作正常,但我看到你说元帅向ui线程。你能解释一下这是什么意思吗?对不起,因为我是新手,我仍然在学习。 –

0

这取决于您传递的值。有时,如果您使用的是对象,则它们会锁定到给定的线程,在这种情况下,您需要先创建重复项,然后将重复项传递到新线程中。

1

我不是100%肯定,如果这就是你想要的,但我认为你需要做的是:

depth_Threads = new System.Threading.Thread(()=>frame_extract.Frame_Processing(_colorFrame_, widht, height)); 
0

做到以下几点。你的方法应该得到像这样的单个对象参数void SomeVoid(object obj)。创建一个对象数组,其中包含想要传递给方法SomeVoid的所有变量,如object[] objArr = { arg1, arg2, arg3 };,然后使用objArr参数调用线程对象的Start方法,因为Start()方法接收一个对象参数。现在回到你的方法,演员和OBJ从Start方法接收一个对象数组像这样object arr = obj as object[];然后就可以像这样访问arr[0] arr[1]arr[2]

0

的3个参数,你可以使用ParameterizedThreadStart类。

1)创建一个类谁都会握着你的三个参数

public class FrameProcessingArguments 
{ 
    public object ColorFrame { get; set; } 
    public int Width { get; set; } 
    public int Height { get; set; } 
} 

2)修改Frame_Processing方法采取的参数Object和里面的情况,施放该实例作为FrameProcessingArguments

if (arguments == null) throw new NullArgumentException(); 
if(arguments.GetType() != typeof(FrameProcessingArguments)) throw new InvalidTypeException(); 
FrameProcessingArguments _arguments = (FrameProcessingArguments) arguments; 

3)创建并启动你的线程

FrameProcessingArguments arguments = new FrameProcessingArguments() 
{ 
    ColorFrame = null, 
    Width = 800, 
    Height = 600 
} 

Thread thread = new Thread (new ParameterizedThreadStart(frame_extract.Frame_Processing)); 
// You can also let the compiler infers the appropriate delegate creation syntax: 
// and use the short form : Thread thread = new Thread(frame_extract.Frame_Processing); 
thread.Start (arguments);