2

Here is what I am trying to do: Thread B will download some images and store those images in a shared resource: Static ArrayList IMBuffer; thread A will take an image from IMBuffer and do something with it. The following is what I got:

Thread B:

// do something

                    System.Net.WebClient myWebClient = new System.Net.WebClient();
                   try
                      { myWebClient.DownloadFile(pth, "BufferImg"); }
                   catch
                      {  // some stuff }

                    // add new dled image to IMBuffer
                    fs = new FileStream("BufferImg", FileMode.Open, FileAccess.Read);
                    Image img = Image.FromStream(fs);
                    lock (IMBuffer)
                    { IMBuffer.Add(img); }
                    img.Dispose();

                    lock (IMRequest) { IMRequest.RemoveAt(0); }
                    myWebClient.Dispose();
                    //fs.Dispose();
                    //  File.Delete("BufferImg");

// do something else

Thread A:

// do something
Image nextImg;
                lock (IMBuffer)
                {
                    nextImg = (Image)IMBuffer[0];
                    nextImg.Save(DLedIM);
                }
// do something else

and here is the problem I am running to; since the images in IMBuffer was opened using a filestream, when the stream is disposed, the line: nextImg.Save(DLedIM); is causing "file is been used by another process" error. However if fs.Dispose(); line is commented out, then the program is locking up "BufferImg", as the result, it won't be able to download image to "BufferImg" after the first time. What should I do to fix this problem? Or is there a simpler way to accomplish what I am trying to do?

codeAsIGo
  • 25
  • 1
  • 4

2 Answers2

0

This should work:

byte[] buffer;
using (FileStream fs = new FileStream("BufferImg", FileMode.Open, FileAccess.Read))
{
    buffer = new byte[fs.Length];
    fs.Read(buffer, 0, (int)fs.Length);
}
using(Image img = Image.FromStream(new MemoryStream(buffer))
{
    //...
}

Using a MemoryStream you avoid having to hold on to the FileStream - at this point the image does not have any connection at all to the file and hence you shouldn't have any problem with file locking.

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • Thank you BrokenGlass, your suggestion fixes the problem. Maybe I am having this all wrong, but what I am doing here seems like a lots of code to do a sample task. I can't stop wondering if I am missing something completely. @ dthorpe thanks for the suggestion, I;ll look into that link too. – codeAsIGo Jul 08 '11 at 20:23
0

Instead of implementing your own multithread producer/consumer workflow (and then having to debug it), why not just use an existing threadsafe (nay, concurrent) queue provided by .NET? Could save you a lot of trial and even more errors.

Details here: Thread-safe blocking queue implementation on .NET

Community
  • 1
  • 1
dthorpe
  • 35,318
  • 5
  • 75
  • 119