falopsy
falopsy

Reputation: 636

Run two codes simultaneously c#

I have this situation in an app I am developing where I need to do two things at exactly the same time and I am kind of struggling with it. I am developing a camera application and I need to trigger the camera at the same time as an LED in order to get a good image. The following are the options I have tried.

  1. Use two threads:

    Thread t1 = new Thread(
                        new ThreadStart(() =>
                        {
                            //trigger LED here
                        }));
    
    Thread t2 = new Thread(
                        new ThreadStart(() =>
                        {
                            //trigger camera
                        }));
    
    t1.Start();
    
    t2.Start();
    t1.Join();
    t2.Join();
    
  2. Trigger the light on the current thread and the camera on a separate thread

    Thread t = new Thread(
                        new ThreadStart(() =>
                        {
                            //trigger camera
                        }));
    
    // trigger LED
    t.Start();
    t.Join();
    

In both cases, I get the back flickers after like 10 images which means the camera and LED are not synchronised. My question is, is there any way to achieve what I am trying to do.

Thank you.

Upvotes: 3

Views: 8004

Answers (4)

usr
usr

Reputation: 171178

If you can't solve this in hardware here is the best approach that I can think of on a non-realtime OS like Windows:

  1. Start one thread for each of the two actions
  2. Raise their priorities to High or Realtime to ensure that they immediately kick anything else off the CPU
  3. Make them wait on a Barrier. This ensures that both threads have started and checked in at the barrier before continuing. This helps to reduce timing jitter. It does not guarantee that both threads execute their hardware action at the same time but it will help.

If that does not help I don't know what would. It is worth a try though. The OS does not guarantee realtime execution but with CPU speeds north of 3 GHZ you can achieve amazing realtime speeds. A context switch might take 3000 cycles which is 0.000001 seconds. So you absolutely can achieve extremely accurate timings.

Upvotes: 4

Lawrence
Lawrence

Reputation: 3297

What you are effectively asking for is a tight coupling between the hardware (camera trigger and LED) and the programming framework to achieve a "real-time" OS. Windows is not a real-time operating system - Microsoft's goal was to develop a platform that is mostly agnostic to the hardware it is running on.

Even for threads - once you ask the CLR to spin up a thread, there is no guarantee that the thread will start running within a specified time interval (reference CLR via C#).

Upvotes: 0

nvoigt
nvoigt

Reputation: 77285

If you have two (or more) actions, you can invoke them in parallel using

Parallel.Invoke( () => DoCamera(), () => DoLighting() );

However, that's a best try. There's no guarantee because after all you might execute this on a machine with only one physical processor or maybe it's under heavy load and only has 10% CPU left anyway.

Upvotes: 4

Bernhard Hofmann
Bernhard Hofmann

Reputation: 10391

How are you controlling the two devices (USB)? You might have two physical CPUs that are able to perform the operations at the same time, but if your interface(s) need the same communication channels, you will find a delay.

Also, you might want to increase the priority of your threads if they are time critical.

If you want the threads synchronised, you should use some form of thread synchronisation such as a mutex, but unless you can guarantee that you (and your customers) have two CPUs, you might be better off performing the two operations in a critical section whilst setting your thread to time critical. See this answer for details on that.

Upvotes: 2

Related Questions