[Gtk-sharp-list] GUI don't refresh the progress bar

YyYo yossiozani at gmail.com
Sat Oct 11 12:40:26 EDT 2008


I just started using GTK#, and until now i did use gtk using Pytgk and
perl-gtk2.

For learning the GTK# (and C# language), I wrote a simple app which suppose
to show progress bar progress(by separate thread).
The main Idea is that the progress bar will continue to work while the user
cound be able to work with other widget of the  GUI simultaneously. 

when I run the app, I can't see any progress of the progress bar, even
though I increase the "fraction" value. I only see movement only when the
"fraction" value equals 1(at the end of the loop)

Note: When I use threads in pygtk I always put gtk_threads_init method, so
the created threads could work simultaneously with gtk main loop

Code:
using System;
using Gtk;
using System.Threading;

public partial class MainWindow: Gtk.Window
{	
	public MainWindow (): base (Gtk.WindowType.Toplevel)
	{
		Build ();
	}
	
	protected void OnDeleteEvent (object sender, EventArgs a)
	{
		Application.Quit ();
	}
	void updateProgressBar(){
		if(!delay_entry.Text.Equals("") && !loop_entry.Text.Equals("")){
			int loop,delay;
			float fraction,z=0;
			string tmp;
			progressbar.Fraction=0;
			tmp=delay_entry.Text;
			delay=System.Convert.ToInt32(tmp);
			tmp=loop_entry.Text;
			loop=System.Convert.ToInt32(tmp);
			Console.WriteLine("delay={0} loop={1}",delay,loop);
			fraction=(float)1/loop;
			for(int i=0;i<loop;i++){
				z+=fraction;
				if(z>1)
					progressbar.Fraction+=1-progressbar.Fraction;
				else
					progressbar.Fraction=z;
				Console.WriteLine("{0}",progressbar.Fraction);
				//Thread.Sleep(delay);
			}
			Console.WriteLine("{0}",progressbar.Fraction);
				
		}
	}
		
	protected virtual void On_startButton_clicked (object sender,
System.EventArgs e)
	{
		
		Thread T=new Thread(new ThreadStart(this.updateProgressBar));
		T.Start();
	}
		
}

public partial class MainWindow {
    
    private Gtk.VBox vbox2;
    
    private Gtk.Label label3;
    
    private Gtk.Table table2;
    
    private Gtk.Entry delay_entry;
    
    private Gtk.HBox hbox1;
    
    private Gtk.ProgressBar progressbar;
    
    private Gtk.Label label4;
    
    private Gtk.Label label5;
    
    private Gtk.Label label6;
    
    private Gtk.Entry loop_entry;
    
    private Gtk.HSeparator hseparator1;
    
    private Gtk.HButtonBox hbuttonbox2;
    
    private Gtk.Button button7;
    
    private Gtk.HBox hbox3;
    
    private Gtk.Label label7;
    
    private Gtk.Image image1;
    
    private Gtk.Button exit_button;
    
    private Gtk.HBox hbox82;
    
    private Gtk.Label label8;
    
    private Gtk.Image image2;
    
    protected virtual void Build() {
        Stetic.Gui.Initialize(this);
        // Widget MainWindow
        this.WidthRequest = 293;
        this.HeightRequest = 162;
        this.Name = "MainWindow";
        this.Title = Mono.Unix.Catalog.GetString("MainWindow");
        this.WindowPosition = ((Gtk.WindowPosition)(4));
        this.DefaultWidth = 293;
        this.DefaultHeight = 162;
        // Container child MainWindow.Gtk.Container+ContainerChild
        this.vbox2 = new Gtk.VBox();
        this.vbox2.Name = "vbox2";
        this.vbox2.Spacing = 3;
        // Container child vbox2.Gtk.Box+BoxChild
        this.label3 = new Gtk.Label();
        this.label3.Name = "label3";
        this.label3.LabelProp =
Mono.Unix.Catalog.GetString("<u>Progress</u>");
        this.label3.UseMarkup = true;
        this.vbox2.Add(this.label3);
        Gtk.Box.BoxChild w1 = ((Gtk.Box.BoxChild)(this.vbox2[this.label3]));
        w1.Position = 0;
        w1.Expand = false;
        w1.Fill = false;
        // Container child vbox2.Gtk.Box+BoxChild
        this.table2 = new Gtk.Table(((uint)(3)), ((uint)(2)), false);
        this.table2.Name = "table2";
        this.table2.RowSpacing = ((uint)(6));
        this.table2.ColumnSpacing = ((uint)(6));
        // Container child table2.Gtk.Table+TableChild
        this.delay_entry = new Gtk.Entry();
        this.delay_entry.CanFocus = true;
        this.delay_entry.Name = "delay_entry";
        this.delay_entry.IsEditable = true;
        this.delay_entry.InvisibleChar = '●';
        this.table2.Add(this.delay_entry);
        Gtk.Table.TableChild w2 =
((Gtk.Table.TableChild)(this.table2[this.delay_entry]));
        w2.TopAttach = ((uint)(1));
        w2.BottomAttach = ((uint)(2));
        w2.LeftAttach = ((uint)(1));
        w2.RightAttach = ((uint)(2));
        w2.YOptions = ((Gtk.AttachOptions)(4));
        // Container child table2.Gtk.Table+TableChild
        this.hbox1 = new Gtk.HBox();
        this.hbox1.Name = "hbox1";
        this.hbox1.Spacing = 6;
        // Container child hbox1.Gtk.Box+BoxChild
        this.progressbar = new Gtk.ProgressBar();
        this.progressbar.Name = "progressbar";
        this.hbox1.Add(this.progressbar);
        Gtk.Box.BoxChild w3 =
((Gtk.Box.BoxChild)(this.hbox1[this.progressbar]));
        w3.Position = 0;
        w3.Expand = false;
        this.table2.Add(this.hbox1);
        Gtk.Table.TableChild w4 =
((Gtk.Table.TableChild)(this.table2[this.hbox1]));
        w4.TopAttach = ((uint)(2));
        w4.BottomAttach = ((uint)(3));
        w4.LeftAttach = ((uint)(1));
        w4.RightAttach = ((uint)(2));
        w4.YOptions = ((Gtk.AttachOptions)(4));
        // Container child table2.Gtk.Table+TableChild
        this.label4 = new Gtk.Label();
        this.label4.Name = "label4";
        this.label4.LabelProp = Mono.Unix.Catalog.GetString("Loop");
        this.table2.Add(this.label4);
        Gtk.Table.TableChild w5 =
((Gtk.Table.TableChild)(this.table2[this.label4]));
        w5.XOptions = ((Gtk.AttachOptions)(4));
        w5.YOptions = ((Gtk.AttachOptions)(4));
        // Container child table2.Gtk.Table+TableChild
        this.label5 = new Gtk.Label();
        this.label5.Name = "label5";
        this.label5.LabelProp = Mono.Unix.Catalog.GetString("Delay");
        this.table2.Add(this.label5);
        Gtk.Table.TableChild w6 =
((Gtk.Table.TableChild)(this.table2[this.label5]));
        w6.TopAttach = ((uint)(1));
        w6.BottomAttach = ((uint)(2));
        w6.XOptions = ((Gtk.AttachOptions)(4));
        w6.YOptions = ((Gtk.AttachOptions)(4));
        // Container child table2.Gtk.Table+TableChild
        this.label6 = new Gtk.Label();
        this.label6.Name = "label6";
        this.label6.LabelProp = Mono.Unix.Catalog.GetString("Progress");
        this.table2.Add(this.label6);
        Gtk.Table.TableChild w7 =
((Gtk.Table.TableChild)(this.table2[this.label6]));
        w7.TopAttach = ((uint)(2));
        w7.BottomAttach = ((uint)(3));
        w7.XOptions = ((Gtk.AttachOptions)(4));
        w7.YOptions = ((Gtk.AttachOptions)(4));
        // Container child table2.Gtk.Table+TableChild
        this.loop_entry = new Gtk.Entry();
        this.loop_entry.CanFocus = true;
        this.loop_entry.Name = "loop_entry";
        this.loop_entry.IsEditable = true;
        this.loop_entry.InvisibleChar = '●';
        this.table2.Add(this.loop_entry);
        Gtk.Table.TableChild w8 =
((Gtk.Table.TableChild)(this.table2[this.loop_entry]));
        w8.LeftAttach = ((uint)(1));
        w8.RightAttach = ((uint)(2));
        w8.YOptions = ((Gtk.AttachOptions)(4));
        this.vbox2.Add(this.table2);
        Gtk.Box.BoxChild w9 = ((Gtk.Box.BoxChild)(this.vbox2[this.table2]));
        w9.Position = 1;
        w9.Expand = false;
        w9.Fill = false;
        // Container child vbox2.Gtk.Box+BoxChild
        this.hseparator1 = new Gtk.HSeparator();
        this.hseparator1.Name = "hseparator1";
        this.vbox2.Add(this.hseparator1);
        Gtk.Box.BoxChild w10 =
((Gtk.Box.BoxChild)(this.vbox2[this.hseparator1]));
        w10.Position = 2;
        w10.Expand = false;
        w10.Fill = false;
        // Container child vbox2.Gtk.Box+BoxChild
        this.hbuttonbox2 = new Gtk.HButtonBox();
        this.hbuttonbox2.Name = "hbuttonbox2";
        // Container child hbuttonbox2.Gtk.ButtonBox+ButtonBoxChild
        this.button7 = new Gtk.Button();
        this.button7.CanFocus = true;
        this.button7.Name = "button7";
        // Container child button7.Gtk.Container+ContainerChild
        this.hbox3 = new Gtk.HBox();
        this.hbox3.Name = "hbox3";
        this.hbox3.Spacing = 7;
        // Container child hbox3.Gtk.Box+BoxChild
        this.label7 = new Gtk.Label();
        this.label7.Name = "label7";
        this.label7.LabelProp = Mono.Unix.Catalog.GetString("Run");
        this.label7.UseMarkup = true;
        this.hbox3.Add(this.label7);
        Gtk.Box.BoxChild w11 =
((Gtk.Box.BoxChild)(this.hbox3[this.label7]));
        w11.Position = 0;
        w11.Expand = false;
        w11.Fill = false;
        // Container child hbox3.Gtk.Box+BoxChild
        this.image1 = new Gtk.Image();
        this.image1.Name = "image1";
        this.image1.Pixbuf = Stetic.IconLoader.LoadIcon(this, "gtk-execute",
Gtk.IconSize.Menu, 16);
        this.hbox3.Add(this.image1);
        Gtk.Box.BoxChild w12 =
((Gtk.Box.BoxChild)(this.hbox3[this.image1]));
        w12.Position = 1;
        w12.Expand = false;
        w12.Fill = false;
        this.button7.Add(this.hbox3);
        this.button7.Label = null;
        this.hbuttonbox2.Add(this.button7);
        Gtk.ButtonBox.ButtonBoxChild w14 =
((Gtk.ButtonBox.ButtonBoxChild)(this.hbuttonbox2[this.button7]));
        w14.Expand = false;
        w14.Fill = false;
        // Container child hbuttonbox2.Gtk.ButtonBox+ButtonBoxChild
        this.exit_button = new Gtk.Button();
        this.exit_button.CanFocus = true;
        this.exit_button.Name = "exit_button";
        // Container child exit_button.Gtk.Container+ContainerChild
        this.hbox82 = new Gtk.HBox();
        this.hbox82.Name = "hbox82";
        this.hbox82.Spacing = 6;
        // Container child hbox82.Gtk.Box+BoxChild
        this.label8 = new Gtk.Label();
        this.label8.Name = "label8";
        this.label8.LabelProp = Mono.Unix.Catalog.GetString("Exit");
        this.label8.UseMarkup = true;
        this.hbox82.Add(this.label8);
        Gtk.Box.BoxChild w15 =
((Gtk.Box.BoxChild)(this.hbox82[this.label8]));
        w15.Position = 0;
        w15.Expand = false;
        w15.Fill = false;
        // Container child hbox82.Gtk.Box+BoxChild
        this.image2 = new Gtk.Image();
        this.image2.Name = "image2";
        this.image2.Pixbuf = Stetic.IconLoader.LoadIcon(this, "gtk-quit",
Gtk.IconSize.Menu, 16);
        this.hbox82.Add(this.image2);
        Gtk.Box.BoxChild w16 =
((Gtk.Box.BoxChild)(this.hbox82[this.image2]));
        w16.Position = 1;
        w16.Expand = false;
        w16.Fill = false;
        this.exit_button.Add(this.hbox82);
        this.exit_button.Label = null;
        this.hbuttonbox2.Add(this.exit_button);
        Gtk.ButtonBox.ButtonBoxChild w18 =
((Gtk.ButtonBox.ButtonBoxChild)(this.hbuttonbox2[this.exit_button]));
        w18.Position = 1;
        w18.Expand = false;
        w18.Fill = false;
        this.vbox2.Add(this.hbuttonbox2);
        Gtk.Box.BoxChild w19 =
((Gtk.Box.BoxChild)(this.vbox2[this.hbuttonbox2]));
        w19.Position = 3;
        w19.Fill = false;
        this.Add(this.vbox2);
        if ((this.Child != null)) {
            this.Child.ShowAll();
        }
        this.Show();
        this.DeleteEvent += new Gtk.DeleteEventHandler(this.OnDeleteEvent);
        this.exit_button.Clicked += new
System.EventHandler(this.OnDeleteEvent);
    }
}

-- 
View this message in context: http://www.nabble.com/GUI-don%27t-refresh-the-progress-bar-tp19934122p19934122.html
Sent from the Mono - Gtk# mailing list archive at Nabble.com.



More information about the Gtk-sharp-list mailing list