• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Thread running PaintEvent e.gc hangs the main thread

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My first attempt at programming a Thread; want an sinewave graph to run in a thread. Simplified code posted here, as long as the sinewave is running, the main window is unresponsive.


import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class WBthreadBare {

protected Shell shell;


/**
* Launch the application.
*
* @param args
*/
public static void main(String[] args) {
try {
WBthreadBare window = new WBthreadBare();
window.open();

} catch (Exception e) {
e.printStackTrace();
}

}

/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}

/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Thread Test");

// Trace trace = new Trace();
(new Trace()).start();

}

class Trace extends Thread {

public Trace() {
Run();
}

public void Run() {

shell.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int b = 0; b < 720; b++) {
for (int a = 1; a < 120 * Math.PI; a++) {

double theta1 = +(b + a - 1) % 360 * 2 * Math.PI / 180;
double theta2 = +(b + a) % 360 * 2 * Math.PI / 180;
int y1 = ((int) (Math.sin((theta1)) * 1000)) / 15 + 90;
int y2 = ((int) (Math.sin((theta2)) * 1000)) / 15 + 90;

e.gc.setForeground(e.display
.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
e.gc.drawLine(a, y1, a + 1, y2);

e.gc.setForeground(e.display
.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawLine(a - 1, y1, a, y2);

} // end for a
} // end for b

} // end paintControl
}); // end PaintListener

} // end Run()

} // end class Trace

}

Code doesn't look like it is formatted as code; not sure how to do that here.
 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically it looks like your doing all the work on the one and only GUI thread. The way round it would be to move the sine wave drawing code onto its own thread / job which would periodically call update allowing user interations to get in.
 
reply
    Bookmark Topic Watch Topic
  • New Topic