Results 1 to 2 of 2

Thread: PictureBox.FromStream has some lag?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    121

    PictureBox.FromStream has some lag?

    Hey guys,

    I have a program that downloads a picture from a website and stores it as a stream.
    CSharp Code:
    1. public void UpdateGGImagesManager(int GateID, PictureBox pbox)
    2.         {
    3.             Thread a = new Thread(() => UpdateGGImages(GateID, pbox));
    4.             a.IsBackground = true;
    5.             a.Start();
    6.         }
    7.  
    8. public void UpdateGGImages(int GateID, PictureBox pbox)
    9.         {
    10.             try
    11.             {
    12.                     WebRequest request = WebRequest.Create("http://" + server + ".darkorbit.bigpoint.com/jumpgate.php?userID=" + uid + "&gateID=" + GateID + "&type=full");
    13.                     WebResponse webresponse = request.GetResponse();
    14.                     Stream responseStream = webresponse.GetResponseStream();
    15.                     UpdatePictureBox(responseStream, pbox);
    16.             }
    17.             catch (Exception)
    18.             {
    19.             }
    20.         }

    Thats the main code. It is then called later like this:
    CSharp Code:
    1. ....
    2. case "4":
    3.             {
    4.                        if (root.Element("item").Attribute("duplicate") != null)
    5.                        {
    6.                                output = output + Convert.ToChar(948).ToString() + " gate (Multiplier Received)";
    7.                        }
    8.                        else
    9.                        {
    10.                               output = output + Convert.ToChar(948).ToString() + " gate";
    11.                               ggcomplete[3][0] = Convert.ToInt32(root.Element("item").Attribute("current").Value);
    12.                               UpdateGGImagesManager(4, pictureBox4);
    13.                        }
    14.                        break;
    15.             }
    16. ....


    When the thread runs, even though it is running on another thread, the GUI locks up for about a second. This means I can narrow it down to the Invoke on the main thread, meaning that the PictureBox.FromStream method generates a bit of lag??

    Is this right or what the heck is goin on?

    Thanks,
    Josh

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: PictureBox.FromStream has some lag?

    All you're doing is opening the stream. You're not reading any data from it until you actually create the Image. Given that the PictureBox control already supports asynchronous loading of images from the web itself, why not just use that? It's one method call on the UI thread instead of all that messing around.

    If you specifically want to preload the image so that the PictureBox can be populated instantly later then you should be creating the Image object on the secondary thread and then you can simply assign that to the Image property of the PictrueBox on the UI thread later.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width