Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6108677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:19:16+00:00 2026-05-23T14:19:16+00:00

Here is what I am trying to do: Thread B will download some images

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:19:17+00:00Added an answer on May 23, 2026 at 2:19 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get around a Cross Thread Exception, Invalid Object Exception. Here
Here's some background on what I'm trying to do: Open a serial port from
I am trying to serve up static content for some web development, with a
I'm trying to write very simple program which will imitate simple DeadLock, where Thread
Programming Student here...trying to work on a project but I'm stuck. The project is
I am going nuts here trying to resolve a cascading update/delete issue :-) I
I know of the ||= operator, but don't think it'll help me here...trying to
Noob dumb question, no doubt -- but here it is: Trying to tutorialize myself
After my last, failed, attempt at asking a question here I'm trying a more
Here's what I am trying to do: typedef enum { ONE, TWO, THREE }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.