-1

I'm working with a camera and want to invoke asynchronously a thread from the delegate that handles a new frame that arrives

I tried:

Task.Factory.StartNew(() => Thread.Sleep(5000))
Thread t = new Thread(new ThreadStart(TestMethod));
t.Start();



camera = new CameraController();

camera.addFramesListener(frameHandler);


public void frameHandler(short[] frame, int indicator)
{

     //handle new frame that arrived from the camera

     //trying to run a new thread from here
}

I expect thread will run asynchronously without waiting for the thread to complete

vik_78
  • 1,107
  • 2
  • 13
  • 20
  • 1
    It's unclear what is the problem from the question. – Dmytro Mukalov Apr 15 '19 at 13:29
  • does your "TestMethod" contain the lines of code : camera = new CameraController(); camera.addFramesListener(frameHandler); ? – Vampirasu Apr 15 '19 at 13:30
  • It might help if you could provide a little more detail around the code you want to call from inside `frameHandler`. Does it specifically need to run on a different thread? What is supposed to happen when that task is finished? – Scott Hannen Apr 15 '19 at 19:13
  • I registered to every new frame that arrives from the camera. when a new frame arrives, I implemented a handler called frameHandler(short[] frame). from that handler (frameHandler) i want to start a thread. however this thread blocks frameHandler instead of running asynchronously – user10183145 Apr 16 '19 at 09:16
  • `Task.Run(() => FireAway());` [From this answer](https://stackoverflow.com/a/1018630/5101046). `FireAway()` will run on another available thread. It's important to note that the method you call must include all of its own exception handling. You can't catch an exception from the method where you started the task. – Scott Hannen Apr 16 '19 at 11:58

1 Answers1

0

Try this:

public void frameHandler(short[] frame)
 {
    Task.Run(() => FireAway())
 }  

 void FireAway()
 {
    Thread.Sleep(5000);
 }
Kzryzstof
  • 7,688
  • 10
  • 61
  • 108