kcc__
kcc__

Reputation: 1648

WPF C# Threading

I am writing a code in c# to do image processing. I want to use threading and i suppose threading in WPF application is little different. I tried to run thread but it only works when the function is void(), i.e does not take any arguments.

However, my function is taking 3 arguemnts like this

frame_extract.Frame_Processing(_colorFrame_, widht, height);

So therefore the following does not work

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

perhaps i am missing something, but my question is how can i work with threading for functions that take arguments.

Upvotes: 0

Views: 166

Answers (5)

b_meyer
b_meyer

Reputation: 604

Maybe you could use the TPL.

It should then be something like:

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

But be aware that you might have to marshal to the ui-thread.

If you want to create the thread in the ui thread and want the new thread to interact with mentioned ui thread, something like the following should work:

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

That should work.

Upvotes: 2

dna
dna

Reputation: 1075

You can use the ParameterizedThreadStart class.

1) Create a class who will holds your three arguments

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

2) Modify your Frame_Processing method to take as parameter an instance of Object and inside it, cast that instance as a FrameProcessingArguments

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

3) Create and start your thread

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);

Upvotes: 0

Dimitri
Dimitri

Reputation: 2878

Do the following. Your method should receive single object argument like this void SomeVoid(object obj). Make an object array with all variables that you want to pass to the method SomeVoid like this object[] objArr = { arg1, arg2, arg3 }; and then call the Start method of thread object with objArr argument since the Start() method receives one object parameter. Now back to your method, cast and obj received from Start method to an object array like this object arr = obj as object[]; and then you can access those 3 arguments like this arr[0] arr[1] and arr[2]

Upvotes: 0

C4stor
C4stor

Reputation: 8036

I'm not 100% sure if that's what you want, but I think you need to do this :

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

Upvotes: 1

Edmund Covington
Edmund Covington

Reputation: 521

It depends on what values you are passing in. Sometimes if you are using objects, they are locked to a given thread in which case you need to create duplicates prior and then pass the duplicates into the new thread.

Upvotes: 0

Related Questions