c# - Is a Semaphore the right tool for this video sequence capture/save job? -


i working on wpf project c# (.net 4.0) capture sequence of 300 video frames high-speed camera need saved disk (bmp format). video frames need captured in near-exact time intervals, can't save frames disk they're being captured -- disk i/o unpredictable , throws off time intervals between frames. capture card has 60 frame buffers available.

i'm not sure best approach implementing solution problem. initial thoughts create "buffertodisk" thread saves images frame buffers become available. in scenario, main thread captures frame buffer , signals thread indicate ok save frame. problem frames being captured quicker thread can save files, there needs kind of synchronization deal this. thinking semaphore tool job. have never used semaphore in way, though, i'm not sure how proceed.

is reasonable approach problem? if so, can post code me started?

any appreciated.

edit: after looking on linked "threading in c# - part 2" book excerpt, decided implement solution adapting "producerconsumerqueue" class example. here adapted code:

class producerconsumerqueue : idisposable {     eventwaithandle _wh = new autoresetevent(false);     thread _worker;     readonly object _locker = new object();     queue<string> _tasks = new queue<string>();      public producerconsumerqueue()     {         _worker = new thread(work);         _worker.start();     }      public void enqueuetask(string task)     {         lock (_locker) _tasks.enqueue(task);         _wh.set();     }      public void dispose()     {         enqueuetask(null);     // signal consumer exit.         _worker.join();         // wait consumer's thread finish.         _wh.close();            // release os resources.     }      void work()     {         while (true)         {             string task = null;             lock (_locker)                 if (_tasks.count > 0)                 {                     task = _tasks.dequeue();                     if (task == null)                     {                         return;                     }                 }             if (task != null)             {                 // parse parameters input queue item                 string[] indexvals = task.split(',');                 int framenum = convert.toint32(indexvals[0]);                 int filenum = convert.toint32(indexvals[1]);                 string path = indexvals[2];                 // build file name                 string newfilename = string.format("img{0:d3}.bmp", filenum);                 string fqfn = system.io.path.combine(path, newfilename);                 // save captured image disk                 int ret = pxd_savebmp(1, fqfn, framenum, 0, 0, -1, -1, 0, 0);             }             else             {                 _wh.waitone();         // no more tasks - wait signal             }         }     } } 

using class in main routine:

// capture bitmap images , save them disk using (producerconsumerqueue q = new producerconsumerqueue()) {      (int = 0; < 300; i++)      {          if (curfrmbuf > numfrmbufs)          {               curfrmbuf = 1;  // wrap around first frame buffer          }           // snap image image buffer          int ret = pxd_dosnap(1, curfrmbuf, 0);           // build parameters saving frame image file (for queue)          string filesaveparams = curfrmbuf + "," + (i + 1) + "," + newpath;          q.enqueuetask(filesaveparams);           curfrmbuf++;     } } 

pretty slick class -- small amount of code functionality.

thanks suggestions, guys.

sure, sounds reasonable. can use semaphores or other thread synchronization primitives. sounds standard producer/consumer problem. take here pseudo-code .


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -