File: ProgressPanel.cs

package info (click to toggle)
mono-tools 4.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 13,868 kB
  • sloc: cs: 100,909; sh: 3,845; makefile: 2,040; xml: 126
file content (76 lines) | stat: -rw-r--r-- 1,538 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//
// ProgressPanel.cs: A panel with a progress bar and a button
//
// Author: Mario Sopena
// 

using System;
using Gtk;
using System.Threading;
 
namespace Monodoc {
	
class ProgressPanel : VBox {
	
	// Delegates called when starting and finishing
	public delegate void StartWorkDelegate ();
	public StartWorkDelegate StartWork;
	public delegate void FinishWorkDelegate ();
	public FinishWorkDelegate FinishWork;

	ProgressBar pb;
	ThreadNotify notify;
	uint timer; 

	public ProgressPanel (string message, string button, StartWorkDelegate StartWork, FinishWorkDelegate FinishWork)
	{
			Gtk.Label l = new Gtk.Label (message);
			l.UseMarkup = true;
			l.Show ();
			PackStart (l);
			
			pb = new ProgressBar ();
			pb.Show ();
			PackEnd (pb, false, false, 3);

			Button b = new Button (button);
			b.Show ();
			b.Clicked += new EventHandler (OnStartWorking);
			PackEnd (b, false, false, 3);

			this.StartWork = StartWork;
			this.FinishWork = FinishWork;
	}

	void OnStartWorking (object sender, EventArgs a)
	{
		Button b = (Button) sender;
		b.Sensitive = false;
		// start a timer to update the progress bar
		timer = Gtk.Timeout.Add ( (uint) 100, new Function (DoUpdateProgressbar));
		
		Thread thr = new Thread (new ThreadStart (Work));
	    thr.Start ();
		notify = new ThreadNotify (new ReadyEvent (Finished));
		
	}

	void Work ()
	{
		StartWork ();
		notify.WakeupMain ();
	}
	
	void Finished ()
	{
		Gtk.Timeout.Remove (timer);
		FinishWork ();
	}

	bool DoUpdateProgressbar ()
	{
		pb.Pulse ();
		return true;
	}
}
}