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:
public void UpdateGGImagesManager(int GateID, PictureBox pbox)
{
Thread a = new Thread(() => UpdateGGImages(GateID, pbox));
a.IsBackground = true;
a.Start();
}
public void UpdateGGImages(int GateID, PictureBox pbox)
{
try
{
WebRequest request = WebRequest.Create("http://" + server + ".darkorbit.bigpoint.com/jumpgate.php?userID=" + uid + "&gateID=" + GateID + "&type=full");
WebResponse webresponse = request.GetResponse();
Stream responseStream = webresponse.GetResponseStream();
UpdatePictureBox(responseStream, pbox);
}
catch (Exception)
{
}
}
Thats the main code. It is then called later like this:
CSharp Code:
....
case "4":
{
if (root.Element("item").Attribute("duplicate") != null)
{
output = output + Convert.ToChar(948).ToString() + " gate (Multiplier Received)";
}
else
{
output = output + Convert.ToChar(948).ToString() + " gate";
ggcomplete[3][0] = Convert.ToInt32(root.Element("item").Attribute("current").Value);
UpdateGGImagesManager(4, pictureBox4);
}
break;
}
....
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
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.