473,471 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Trouble with Threads blocking execution

I have a UI with a gallery of thumbnail image previews. The
PictureBoxes are created and their Images are generated by a method
which quickly retrieves the icon for the filetype. Worker threads then
asynchronously retrieve and resize the full image previews and update
the PictureBox's Images when the new images are ready. The way I have
it set up now, the method which generates the filetype icons creates
and starts the new worker Threads. The problem is, the threads block
execution of the method. Here is the relevant code:
// This method is called to create a PictureBox's Image
private Image GetThumbnail(
string uri, // the filepath to the image
PictureBox pictureBox, // the PictureBox control - we need to know this so the worker thread can replace the right PictureBox's Image
int width, // the thumbnail width
int heigh // and the height
)
{
Image thumbnail = GetIcon(uri); // GetIcon returns an Image based on the filetype of the specified URI
ThumbnailThread thumbnailThread = new ThumbnailThread(uri, pictureBox, width, height, ThumbnailCallback); // See below for more on the ThumbnailThread class
Thread thread = new Thread(thumbnailThread.ThreadProc);
thread.Start();
return thumbnail; // we return the temporary thumbnail and the thread will update the PictureBox with the full image preview later
}

// This callback actually swaps the PictureBox's Image
private void ThumbnailCallback(Image image, PictureBox pictureBox)
{
pictureBox.Image = image;
}
and the ThumbnailThread class is thusly defined:
public class ThumbnailThread
{
private string _uri; // the filepath to the image
private PictureBox _pictureBox; // the PictureBox control
private int _width; // the width of the thumbnail
private int _height; // and the height
private ThumbnailThreadCallback _callback; // the callback method

public ThumbnailThread(string uri, PictureBox pictureBox, int width, int height, ThumbnailThreadCallback callback)
{
_uri = uri;
_pictureBox = pictureBox;
_width = width;
_height = height;
_callback = callback;
}

public void ThreadProc()
{
Image thumbnail = Image.FromFile(_uri); // get the whole image
thumbnail = MyClass.FixedSize(thumbnail, _width, _height); // FixedSize is a static method of MyClass that handles fixed proportion resizing
_callback(thumbnail, _pictureBox); // callback to the main thread
}
}
and the ThumbnailThreadCallback delegate looks like this:
public delegate void ThumbnailThreadCallback(Image image, PictureBox pictureBox);


I thought perhaps that scope of the Thread within GetThumbnail was
blocking execution, but I tried making a class-wide Thread[] array,
within which the worker threads were created, but execution was still
being blocked. Any clues?

Jun 25 '06 #1
4 1555
"Lunchtimemama" <lu***********@gmail.com> wrote in
news:11*********************@c74g2000cwc.googlegro ups.com:
I have a UI with a gallery of thumbnail image previews. The
PictureBoxes are created and their Images are generated by a method
which quickly retrieves the icon for the filetype. Worker threads then
asynchronously retrieve and resize the full image previews and update
the PictureBox's Images when the new images are ready. The way I have
it set up now, the method which generates the filetype icons creates
and starts the new worker Threads. The problem is, the threads block
execution of the method. Here is the relevant code:

[snip]

Your main problem I can see is that you are not marshalling the
"pictureBox.Image= image" final method call to the PictureBox control's
thread-- your worker thread is the thread trying to update the picturebox
when the image object creation is complete.

Change the "_callback(thumbnail, _pictureBox)" line with this:

_pictureBox.Invoke(_callback,new object[]{thumbnail,
_pictureBox)});

this will cause the callback invokation to be performed on the GUI thread
that owns the PictureBox's window handle. The Invoke() method will block
until the Gui thread has completed executing the given delegate.

See
http://weblogs.asp.net/justin_rogers...es/126345.aspx
for instance for more about using threading with Gui objects.

Jun 25 '06 #2
Take a look at:
http://channel9.msdn.com/ShowPost.as...=171594#171594

--
William Stacey [MVP]
Jun 25 '06 #3
Neither of those solutions resolve the blocking problem. Note: I've
only tried them with local scope Threads. Is that an issue?

Jun 25 '06 #4
I think the problem lies with Image.FromFile(). Does that function
syncronize with the main thread for some reason?

Jun 25 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bart Nessux | last post by:
Could someone explain the concept of threads and how I might use them in Python? I was a math major, not a CS major (theory instead of practice). Most of my programming knowledge has grown out of...
29
by: Jeffrey Maitland | last post by:
Hello all, I am in the process of writing a multithreading program and what I was wondering is a sleep command in an executing function will affect the threads below it? Here is a basic example...
35
by: Carl J. Van Arsdall | last post by:
Alright, based a on discussion on this mailing list, I've started to wonder, why use threads vs processes. So, If I have a system that has a large area of shared memory, which would be better? ...
18
by: Jon Slaughter | last post by:
"Instead of just waiting for its time slice to expire, a thread can block each time it initiates a time-consuming activity in another thread until the activity finishes. This is better than...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.