File: test-threads.cs

package info (click to toggle)
clutter-sharp 1.0.0~alpha3~git20090817.r1.349dba6-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 4,104 kB
  • ctags: 2,193
  • sloc: xml: 23,456; cs: 9,946; sh: 3,393; perl: 1,213; makefile: 270; awk: 50; sed: 13
file content (170 lines) | stat: -rw-r--r-- 3,427 bytes parent folder | download
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Threading;

using Clutter;

namespace ClutterTest 
{
	class TestThreadData {
		// Stage stage;
		Label label;

		public Label Label {
			get { return label; } 
		}

		Timeline timeline;

		bool cancelled;

		public bool Cancelled {
			get { return cancelled; } 
		}

		public TestThreadData (Label label, Timeline timeline) {
			this.label = label;
			this.timeline = timeline; 
		}

		public bool HandleIdle () {
			Console.WriteLine ("Thread completed");

			label.Text = "Completed";
			timeline.Stop ();

			return false;
		}
	} 

	class TestUpdate {
		TestThreadData data;
		int count; 

		public TestUpdate (int count, TestThreadData data) {
			this.count = count;
			this.data = data; 
		}

		public bool HandleIdle () {
			string text = string.Format ("Count to {0}", count);
			data.Label.Text = text;

			return false;
		}
	}

	class ThreadWrapper {
		TestThreadData data;
		
		public ThreadWrapper (TestThreadData data) {
			this.data = data; 
		}

		void DoSomethingVerySlow () {
			if (data.Cancelled)
			 	return; 

			for (int i = 0; i < 100; i++) {
			 	System.Threading.Thread.Sleep (100);
				
				if (i % 10 != 0)
				 	continue;

				Idle.Add (new TestUpdate (i, data).HandleIdle);
			}
		} 

		public void ThreadMethod () {
			DoSomethingVerySlow ();
			
			Idle.Add (data.HandleIdle); 
		}
	}

	class Program {
	 	static Color stage_color = new Color (0xcc, 0xcc, 0xcc, 0xff);
		static Color rect_color = new Color (0xee, 0x55, 0x55, 0x99);

		// FIXME: it looks like with threading we need to keep reference to all
		// these items :-(
		static Alpha alpha;
		static Label count_label;
		static Label label;
		static Rectangle rect;
		static Timeline timeline; 
		static Behaviour behaviour;

		static Stage stage;

		static void HandleKeyPress (object sender, KeyPressEventArgs args) {
			uint symbol = args.Event.Symbol;
			Gdk.Key key = (Gdk.Key) Enum.Parse (typeof(Gdk.Key), symbol.ToString ()); 

			switch (key) {
				case Gdk.Key.s:
					timeline.Start ();

					TestThreadData data = new TestThreadData (count_label, timeline);
					ThreadWrapper wrapper = new ThreadWrapper (data);

					Thread thread = new Thread (wrapper.ThreadMethod);
					thread.Start ();
					break;
			 	case Gdk.Key.q:
					Clutter.Main.Quit ();
					break;
			}
		 
		}

		static void Main () {
			if (!GLib.Thread.Supported)
				GLib.Thread.Init ();

			Clutter.Threads.Init ();
			Clutter.Threads.Enter ();  

			ClutterRun.Init ();

			stage = Stage.Default;
			stage.Color = stage_color;
			stage.SetSize (800, 600);

			count_label = new Label ("Mono 16", "Counter");
			count_label.SetPosition (350, 50);
			count_label.Show ();

			label = new Label ("Mono 16", "Press 's' to start");
			label.SetPosition (50, 50);
			label.Show ();

			rect = new Rectangle (rect_color);
			rect.SetPosition (150, 150);
			rect.SetSize (25, 25);
			rect.Show ();

			timeline = new Timeline (150, 50);
			timeline.Loop = true;

			alpha = new Alpha (timeline, Sine.Func);
			behaviour = new BehaviourRotate(alpha, 
							RotateAxis.ZAxis, 
							RotateDirection.Cw,
							0.0,
							360.0);
			behaviour.Apply (rect);

			stage.AddActor (rect);
			stage.AddActor (count_label);
			stage.AddActor (label);

			stage.ButtonPressEvent += delegate { Clutter.Main.Quit(); };
			stage.KeyPressEvent += HandleKeyPress;

			stage.ShowAll ();

			ClutterRun.Main ();
			Clutter.Threads.Leave ();
		}
	}
}