ERLEDIGT
NEIN
NEIN
ANTWORTEN
1
1
ZUGRIFFE
1228
1228
EMPFEHLEN
-
Hallo Leute,
ich habe folgendes Problem und weiß nicht genau, wie ich es anpacken soll:
Ich habe einen Thread A (ist nicht der Main-Thread). Dieser Thread A stößt zwei weitere Threads B und C an. Innerhalb von B + C sollen zwei Fenster mit SWT geöffnet werden (welche auch parallel bedienbar sein sollten). Bei mir öffnet sich max. immer nur ein Fenster, weil er dann hängen bleibt. wenn ich die GUI in B + C mit asyncExec() aufrufe, bekomme ich überhaupt nichts zu sehen. Als Display benutze ich in B + C Display.getDefault() (was ist hier eigentlich der Unterschied zu Display.getCurrent()), welches ich dann an die GUI weitergebe. Wichtig ist vielleicht noch, dass von der GUI in B + C jeweils eine eigene Instanz gebildet wird.
Kann mir vielleicht jemand einen groben plan geben, wie ich das architektonisch am besten anpacke, dass ich von den zwei threads (B + C) aus zwei GUIs öffnen kann, welche gleichzeitig bedienbar sind?
Vielen Dank für eure Hilfe!
Greetz, iTob
-
15.07.09 15:30 #2
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.886
- Blog-Einträge
- 29
Hallo,
von unterschiedlichen Threads aus SWT Shells aufzumachen macht die ganze Sache ein wenig komplizierter als gewöhnlich:
Code java: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
package de.tutorials.swt.training; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import org.eclipse.swt.events.DisposeEvent; import org.eclipse.swt.events.DisposeListener; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class SWTUI { final Lock lock = new ReentrantLock(); final Condition displayReady = lock.newCondition(); final ExecutorService executorService = Executors.newCachedThreadPool(); /** * @param args */ public static void main(String[] args) { new SWTUI().createAndRunUI(); } private void shutdown() { System.out.println("shutting down..."); executorService.shutdownNow(); } private void createAndRunUI() { executorService.execute(new Runnable() { public void run() { lock.lock(); System.out.println("booting swt display in: " + Thread.currentThread()); Display display = Display.getDefault(); try { displayReady.signal(); } finally { lock.unlock(); } while (!display.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } System.out.println("Display disposed"); shutdown(); } }); lock.lock(); try { try { displayReady.await(); } catch (InterruptedException e) { e.printStackTrace(); } } finally { lock.unlock(); } executorService.execute(new Runnable() { public void run() { executorService.execute(new Runnable() { public void run() { showWindow("A"); } }); executorService.execute(new Runnable() { public void run() { showWindow("B"); } }); for (int i = 0; i < 100; i++) { System.out.println(i + " xxx"); try { TimeUnit.MILLISECONDS.sleep(100); } catch (InterruptedException e) { System.out.println("Task cancelled..."); break; } } } }); } public void showWindow(final String title) { System.out.println("Creating window from: " + Thread.currentThread() + " " + title); executorService.execute(new Runnable() { public void run() { final Display display = Display.getDefault(); System.out.println("New Shell with: " + display + " " + display.getThread()); display.asyncExec(new Runnable() { public void run() { Shell shell = new Shell(display); shell.setText(title); shell.setSize(320, 240); shell.addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent e) { if (display.getShells().length == 1) { System.out.println("Last shell was closed"); display.dispose(); } } }); shell.open(); } }); } }); } }
Eine weitere Möglichkeit das ganze ein wenig einfacher zu haben wäre es, JFace ApplicationWindows verwenden.
Gruß TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
Ähnliche Themen
-
GMF Problematik
Von abesier im Forum Swing, Java2D/3D, SWT, JFaceAntworten: 0Letzter Beitrag: 18.08.08, 14:27 -
Thread A stösst Änderung in Thread B an. Aber wie?
Von BeaTBoxX im Forum .NET CaféAntworten: 12Letzter Beitrag: 13.12.06, 11:52 -
max - c4d (Thread split by IKEAFREAX on 09.07.2002 23:43 (thread by pasq))
Von pasq im Forum 3D Studio MaxAntworten: 19Letzter Beitrag: 12.07.02, 11:13





Zitieren

Login





