I have a C # workflow that saves a camera bitmap package to disk using BlockingCollection. It works well, but I need a method that is called from the main application, which blocks execution until all the bitmap images in the queue are saved (see, for example, the end of the message).
The whole class is as follows:
namespace GrabGUI
{
struct SaveTask
{
public string fname;
public Bitmap bm;
}
class ImageWriter
{
private BlockingCollection<SaveTask> queue = new BlockingCollection<SaveTask>();
public string ErrorsOccurred;
private Thread writerthread;
public ImageWriter()
{
writerthread = new Thread(new ThreadStart(Writer));
writerthread.Start();
}
public void Stop()
{
queue.CompleteAdding();
}
public string WaitForIdleAndGetErrors()
{
return ErrorsOccurred;
}
public void AddImageToQueue(string filename, Bitmap bmap)
{
SaveTask t;
t.bm=bmap;
t.fname=filename;
queue.Add(t);
}
void Writer()
{
while (queue.IsCompleted==false)
{
try
{
SaveTask t = queue.Take();
SaveBitmap(t.fname, t.bm);
}
catch (Exception e)
{
return;
}
}
}
private void SaveBitmap(string filename,Bitmap m_bitmap)
{
}
}
}
And used from the main application, for example:
ImageWriter w=new ImageWriter();
w.AddImageToQueue(fname,bitmap);//repeat many times
...
//wait until whole queue is completed and get possible errors that occurred
string errors=w.WaitForIdleAndGetErrors();
So the question is how to implement waitlocking WaitForIdleAndGetErrors (). Any suggestions?
Terok source
share