1
using System;

public class ImageConverter
{
    public void button1_Click(Object sender, RoutedEventArgs e)
    {
       string filename=null;
       SendImageToPlayer send = new SendImageToPlayer();
       //send.ReadImageFile(filename);
       Thread t = new Thread(new send.ReadImageFile);
       uint ret=send.ErrorCode;
    }
}

public class SendImageToPlayer
{
    ...
    public void ReadImageFile(string PfileName)
    {
       //something
    }
    ...
}

The code above shown will not work. I want to run ReadImageFie in separate thread. How can I do that?

Philip Fourie
  • 111,587
  • 10
  • 63
  • 83
SHRI
  • 2,406
  • 6
  • 32
  • 48

2 Answers2

2

You should start your thread after creating it: t.Start() ;

You should also consider using the Task Parallel Library instead.

Oh, oh, I just noticed you want to pass a parameter. You can set a property in your SendImageToPlayer instance prior to starting the thread, or pass an object to the ReadImagefile function. But really, use the TPL, it's better.

zmbq
  • 38,013
  • 14
  • 101
  • 171
1

Introduce a property FileName on your SendImageToPlayer class and set it before you start the thread.

using System;

public class ImageConverter
{
    public void button1_Click(Object sender, RoutedEventArgs e)
    {
      string filename = "c:\myfile.bmp";
      SendImageToPlayer send = new SendImageToPlayer();
      send.Filename = filename;
      Thread t = new Thread(send.ReadImageFile);
      t.Start();
}

Consider using the BackgroundWorker thread class. It provides events when the thread completes.

You check the ErrorCode when the RunWorkerCompleted event is fired.

Philip Fourie
  • 111,587
  • 10
  • 63
  • 83