comparison snippets/busyindicator/Snippet130.d @ 117:8cdaac0dc743

Added more snippets from TomD
author Frank Benoit <benoit@tionex.de>
date Sat, 12 Jul 2008 20:09:06 +0200
parents 7194dba256b8
children
comparison
equal deleted inserted replaced
116:f53c6274734f 117:8cdaac0dc743
5 * which accompanies this distribution, and is available at 5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html 6 * http://www.eclipse.org/legal/epl-v10.html
7 * 7 *
8 * Contributors: 8 * Contributors:
9 * IBM Corporation - initial API and implementation 9 * IBM Corporation - initial API and implementation
10 * D Port:
11 * Thomas Demmer <t_demmer AT web DOT de>
10 *******************************************************************************/ 12 *******************************************************************************/
11 13 module busyindicator.Snippet130;
12 /* 14 /*
13 * BusyIndicator example snippet: display busy cursor during long running task 15 * BusyIndicator example snippet: display busy cursor during long running task
14 * 16 *
15 * For a list of all SWT example snippets see 17 * For a list of all SWT example snippets see
16 * http://www.eclipse.org/swt/snippets/ 18 * http://www.eclipse.org/swt/snippets/
17 */ 19 */
18 module busyindicator.Snippet130;
19
20 import dwt.DWT; 20 import dwt.DWT;
21 import dwt.events.SelectionAdapter; 21 import dwt.events.SelectionAdapter;
22 import dwt.events.SelectionEvent; 22 import dwt.events.SelectionEvent;
23 import dwt.layout.GridLayout; 23 import dwt.layout.GridLayout;
24 import dwt.layout.GridData; 24 import dwt.layout.GridData;
34 import dwt.dwthelper.utils; 34 import dwt.dwthelper.utils;
35 import dwt.dwthelper.Runnable; 35 import dwt.dwthelper.Runnable;
36 import dwt.dwthelper.System; 36 import dwt.dwthelper.System;
37 37
38 import tango.core.Thread; 38 import tango.core.Thread;
39 import tango.io.Stdout;
39 import tango.util.Convert; 40 import tango.util.Convert;
40 import tango.util.log.Trace; 41 import tango.util.log.Trace;
42
41 43
42 void main(String[] args){ 44 void main(String[] args){
43 Snippet130.main(args); 45 Snippet130.main(args);
44 } 46 }
45 47
49 Display display = new Display(); 51 Display display = new Display();
50 Shell shell = new Shell(display); 52 Shell shell = new Shell(display);
51 shell.setLayout(new GridLayout()); 53 shell.setLayout(new GridLayout());
52 Text text = new Text(shell, DWT.MULTI | DWT.BORDER | DWT.V_SCROLL); 54 Text text = new Text(shell, DWT.MULTI | DWT.BORDER | DWT.V_SCROLL);
53 text.setLayoutData(new GridData(GridData.FILL_BOTH)); 55 text.setLayoutData(new GridData(GridData.FILL_BOTH));
54 int[1] nextId; 56 int[] nextId = new int[1];
55 Button b = new Button(shell, DWT.PUSH); 57 Button b = new Button(shell, DWT.PUSH);
56 b.setText("invoke long running job"); 58 b.setText("invoke long running job");
57 59
58 b.addSelectionListener(new class() SelectionAdapter { 60 b.addSelectionListener(new class() SelectionAdapter {
59 public void widgetSelected(SelectionEvent e) { 61 public void widgetSelected(SelectionEvent e) {
60 Runnable longJob = new class() Runnable { 62 Runnable longJob = new class() Runnable {
61 bool done = false; 63 bool done = false;
62 int id; 64 int id;
63 private void runThread(){ 65 public void run() {
64 id = nextId[0]++; 66 Thread thread = new Thread({
65 display.syncExec( dgRunnable( &printStart, text, id )); 67 id = nextId[0]++;
66 for (int i = 0; i < 6; i++) { 68 display.syncExec(new class() Runnable {
69 public void run() {
70 if (text.isDisposed()) return;
71 text.append("\nStart long running task "~to!(char[])(id));
72 }
73 }); // display.syncExec
74 /*
75 * This crashes when more than 1 thread gets created. THD
76 for (int i = 0; i < 100000; i++) {
77 if (display.isDisposed()) return;
78 Stdout.formatln("do task that takes a long time in a separate thread {}", id);
79 }
80 */
81 // This runs fine
82 for (int i = 0; i < 6; i++) {
83 if (display.isDisposed()) return;
84 Trace.formatln("do task that takes a long time in a separate thread {} {}/6", id, i);
85 Thread.sleep(0.500);
86 }
87
67 if (display.isDisposed()) return; 88 if (display.isDisposed()) return;
68 Trace.formatln("do task that takes a long time in a separate thread {} {}/6", id, i); 89 display.syncExec(new class() Runnable {
69 Thread.sleep(0.500); 90 public void run() {
70 } 91 if (text.isDisposed()) return;
71 if (display.isDisposed()) return; 92 text.append("\nCompleted long running task "~to!(char[])(id));
72 display.syncExec( dgRunnable( &printEnd , text, id )); // display.syncExec 93 }
73 done = true; 94 }); // display.syncExec
74 display.wake(); 95 done = true;
75 } 96 display.wake();
76 public void run() { 97 }); // thread = ...
77 Thread thread = new Thread( &runThread );
78 thread.start(); 98 thread.start();
79 99
80 while (!done && !shell.isDisposed()) { 100 while (!done && !shell.isDisposed()) {
81 if (!display.readAndDispatch()) 101 if (!display.readAndDispatch())
82 display.sleep(); 102 display.sleep();
105 if (text.isDisposed()) return; 125 if (text.isDisposed()) return;
106 Trace.formatln( "Completed long running task {}", id ); 126 Trace.formatln( "Completed long running task {}", id );
107 text.append("\nCompleted long running task "~to!(char[])(id)); 127 text.append("\nCompleted long running task "~to!(char[])(id));
108 } 128 }
109 } 129 }
110
111