Re: Threading confusion!
- From: Rudy <Rudy@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 17 Feb 2007 19:11:00 -0800
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
- Follow-Ups:
- Re: Threading confusion!
- From: Rudy
- Re: Threading confusion!
- References:
- Re: Threading confusion!
- From: Markus
- Re: Threading confusion!
- From: Markus
- Re: Threading confusion!
- From: Markus
- Re: Threading confusion!
- Prev by Date: Re: Threading confusion!
- Next by Date: Re: Threading confusion!
- Previous by thread: Re: Threading confusion!
- Next by thread: Re: Threading confusion!
- Index(es):
Relevant Pages
|
Loading