Re: Threading confusion!



Hi Markus!

Thank you for your great suggestions! But, none of them really worked. But
I did discover something interesting. My picture boxes have a background
color set to Transpracy. When I set the background color to a color, they
load up just fine. Why do you think that is?

Thanks again for all your help!

Rudy

"Markus" wrote:

So I'm assuming that it is all one thread on that form. So I figure
if I can load the main image first, this may speed things up.

Yes. A form is single threaded by default. If you want to do something
different, you need to create a new thread and do this in another thread.


I like your idea below, but how would I go about doing that? Loading
the image form the main form would be a non-safe thread, right.
Because the object is part of the form.

Which object is part of the form? The image? Or the Thread object? Maybe
I didn't get you right, but this does not matter...

However, you could do something like this:

1) prerequisites (member vars)
------------------------------
// create a member var for the images, maybe a list:
private IList<images> _myImages;


2) create the thread and handle the thread events
the easiest might be to use the BackgroundWorker instead
of an own thread, because then you don't have to be concerned
about accessing the Form-Thread from your thread to modify
controls.
-----------------------------------------------------------
// somewhere in the load event (or the constructor, but I might
// prefer the load event of the form):
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += delegate(object sender, DoWorkEventArgs e)
{
_myImages = new List<images>();
for (int i=0; i<8; i++)
{
Image img = LoadMyImageFromSomewhere();
_myImages.Add(img);
}
}

worker.RunWorkerCompleted += delegate(object sender,
RunWorkerCompletedEventArgs e)
{
foreach (Image img in _myImages)
{
PictureBox pb = GetPictureBox();
pb.Image = img;
}
};

worker.RunWorkerAsync();



However, you might want to decide to use methods instead of the
inline-delegate statements - but I hope you got the point.

Only remember that you do NOT access (or set) the PictureBox.Image
property in the DoWork-event, as the DoWork event is NOT executed in the
same thread of the form and would throw an exception. But the
RunWorkerCompleted event provides exactly this feature.

I did something similar a while ago and set the image to the picture box
in the DoWork event by returning to the UI event... then you have a
behavior like in Windows Explorer when you load the thumbnail-view - as
soon as something is loaded it is displayed.


If still everything is unclear, feel free to email me (just remove the
REMOVE-THIS in my email address...

Markus

.



Relevant Pages

  • Re: Threading confusion!
    ... if I can load the main image first, ... PictureBox pb = GetPictureBox; ... RunWorkerCompleted event provides exactly this feature. ... in the DoWork event by returning to the UI event... ...
    (microsoft.public.dotnet.general)
  • Re: too many picture controls really slows down my application
    ... I want to load on my form. ... array of picture controls, ... resize for viewing a normal sized jpeg photo. ... PictureBox or an Image Control, or into a stdPicture Object, it loads ...
    (comp.lang.basic.visual.misc)
  • Re: How do I create mouse overs for web documents?
    ... that it will load more quickly, ... > I did tweak the sky and the picture is a LITTLE bit clearer. ... > Many thanks David. ... >> you can see that you have added the alt tags to your images as they load ...
    (microsoft.public.publisher.webdesign)
  • linked images in access and having troubles printing
    ... The graphics I have are JPG's at a size of between 50K and 70K. ... When I import picture into the database, it increases in size by about ... I have read the help file and it mentions filters but how to load them? ...
    (microsoft.public.access.externaldata)
  • Re: Cancelling a for loop
    ... I'd suggest you don't want to load your pictures in a For/Next loop. ... user can scroll the viewer between timer ticks. ... the routine that loads another picture. ... Private Sub Timer1_Timer ...
    (microsoft.public.vb.general.discussion)

Loading