GR Kamath
GR Kamath

Reputation: 65

Running two loops in Parallel

I am working on a Video streaming algorithm. I have divided whole video into an array of images. Now each image will be streamed based on the time (Image.TimeInMs represents the time at which image to be streamed or sent). To maintain the time I have System.Timer.Timer object.

_timer = new System.Timers.Timer(250);
_timer.Elapsed += new ElapsedEventHandler(timeCounterIncrementer);
_timer.Enabled = true;

for( i =0; i<imageArray.length;i++)
{
    while (timeCounter < imageArray[i].TimeInMs)
    {                          
        StreamImage();
    }
}

void timeCounterIncrementer(object sender, ElapsedEventArgs e)
{
    timeCounter+=250;            
}

In the above code when the while loop is running timer method timeCounterIncrementer will not execute. I want timer method to run on one hand and While loop on the other. In other words How to run the while loop and timer method parallely ?

Upvotes: 2

Views: 4656

Answers (1)

eMi
eMi

Reputation: 5618

Recently I had a similiar problem. At least I fixed it using Task Parallelism (Task Parallel Library) since Threading is 2 hard for me :)

If you accomplish to part your two loops into methods, you could call these two methods parallely, like this:

Parallel.Invoke(() => DoSomeWork(), () => DoSomeOtherWork());

Upvotes: 4

Related Questions