Perl-Sensitive Sunglasses PerlMonks

by jerre_111 (Sexton)
 on Apr 23, 2013 at 07:53 UTC Need Help??
jerre_111 has asked for the wisdom of the Perl Monks concerning the following question:

Hey everybody,

I'm facing some difficulties when I'm trying to implement a thread in my program. I don't know if I am thinking wrong about the usage of threads or if I'm facing some other problems?

First I'll try to explain the basic of the program which I'm trying to implement thread in.

I'm working in a Unix environment and I'm using Tk and Imager modules

The program basicly reads in an image and then draws it pixels by pixels on a canvas. Because it is taking some time to draw on the canvas if it is a big image, I want to add a progressbar while drawing the image.

Butt when I add the progress bar in the same sub routine as the drawing subroutine, it becomes even more slower. So I thought to use threads, by doing this I can work parallel on drawing on the progress bar at the same time. Right?

Because I never used threads before, I have read some articles about it at the perlthrtut pages in the hope of learning how threads works.

After reading this all, I managed to write this piece of code:

```# Calculate the pixels and divide by 100

if (\$first_start == "1"){
\$tot_pix = \$x * \$y;
\$one_hunderd = \$tot_pix / 100;
}

# Loop for drawing all

for (\$width = 0; \$width < \$x; \$width++) {
for (\$height = 0; \$height < \$y; \$height++) {

# Define the exact coordinates to place the pixel
\$width_pos = \$width + 1;
\$height_pos = \$height + 1;
\$width_pos2 = \$width + 2;
\$height_pos2 = \$height + 2;
\$canvas->createRectangle(\$width_pos * \$multipli ,\$height_pos * \$multip
+li , \$width_pos2 *\$multipli , \$height_pos2 * \$multipli , -fill => 'bl
+ack');
}
}
}
# If it is not the first time in this subroutine, start the thread
if (\$first_start == "1"){
my \$thr1 = threads->create(\&progress_calc, \$tot_pix, \$one_hun
+derd, \$passed_pix);
}

\$passed_pix += 1;
# If it is not the first time, Join the thread.
if (\$first_start == "1"){
\$thr1->join();
}
}
}

#call the subroutine
hide_progress();

#set the variables when exiting the subroutine
\$first_start = "1";
\$passed_pix = 0;

}

#subroutine for the progress bar
sub progress_calc {
# Calculations for progress bar

\$var1 = \$percent_done;
\$var2 = \$passed_pix;
\$var3 = \$one_hunderd;
while (\$var1 <= 100) {
#do some math to define the percent done and update the progress bar.
\$var1= \$var2 / \$var3;
\$win_prog->update();
}
}

#sub to show the progress bar
sub show_progress {
\$win_prog->deiconify();
\$win_prog->raise();
\$win_prog->update();
}

#sub to hide the progress bar
sub hide_progress {
\$win_prog->withdraw();
}

So basicly what this code does is first do some calculatios to get the percent of the total pixels (I need this to draw a realistic progress bar). Then I'm drawing the image pixel by pixel. While drawing the image, I want to start a thread that is updating the progress bar and doing the calculation for knowing the percent that is drawed already.

So that it the very basic of what I'm trying to do.

Butt, I'm not having any luck in creating a thread that does update the progress bar and so on.

I don't know what I'm doing wrong and where I'm doing it wrong. So could someone please help me in implementing the thread?1

Or maybe somebody has a better idea then using a thread?

Replies are listed 'Best First'.
by kcott (Chancellor) on Apr 23, 2013 at 08:34 UTC

G'day jerre_111,

You might take a look at "Progress Bar in Perl script. This has many examples of progress indicators, threaded and otherwise.

Also, what GUI module are you using? If it's Perl/Tk, take a look at Tk::ProgressBar; if it's not, see if there's an equivalent for whatever you're using.

-- Ken

Thanks, I'll take a look at the node you have send.

Yes, I'm using Perl/Tk. Drawing the Progress bar is already done, it is the updating in the thread that doesn't work out for me.

## Update

Hmm I don't seem to get any wiser from the node you have send me sorry. I'm sure it is me but, I could only find one answer about using threads ine that node, and I didn't understand it sorry.

All the rest was about using a spinner as a progress indicator. Butt, I use the Tk::ProgressBar because I want to show a percentage of the work that is done already.

Sorry for being such a retard, Threading is not my thing is seems

"... I could only find one answer about using threads ine that node, and I didn't understand it sorry."

Perhaps a look at the Perl Threads Tutorial might help.

Here's a technique for creating image data and updating a progress bar without using threads. I've made no attempt to integrate canvasses, rectangles, etc. This is a complete working script — you should be able to run it as is.

```#!/usr/bin/env perl

use strict;
use warnings;

use Tk;
use Tk::Photo;
use Tk::ProgressBar;

my \$mw = MainWindow->new();
\$mw->geometry("200x200");

my \$action_F = \$mw->Frame()->pack(-side => 'bottom');
my \$exit_B = \$action_F->Button(-text => 'Exit', -command => sub { exit
+ })->pack;

my \$image_F = \$mw->Frame(
)->pack(-padx => 10, -pady => 10, -expand => 1, -fill => 'both');
my \$progress_F = \$mw->Frame(
)->pack(-padx => 10, -pady => 10, -expand => 0, -fill => 'x');

my (\$image_height, \$image_width) = (100, 100);
my \$image = \$image_F->Photo(-height => \$image_height, -width => \$image
+_width);
\$image_F->Label(-image => \$image)->pack;

my \$pc_image_drawn = 0;
my \$progress_PB = \$progress_F->ProgressBar(-variable => \\$pc_image_dra
+wn
)->pack(-fill => 'x');

for my \$height (0 .. \$image_height - 1) {
for my \$width (0 .. \$image_width - 1) {
\$image->put('#ffcc33', -to => \$width, \$height);
\$image->update; # called in inner loop for slow demonstration
+display
}

\$pc_image_drawn = ((\$height + 1) / (\$image_height)) * 100;
}

MainLoop;

-- Ken

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1030062]
Approved by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2018-06-22 04:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should cpanminus be part of the standard Perl release?

Results (121 votes). Check out past polls.

Notices?