官术网_书友最值得收藏!

Time for action – animating the second hand

The second hand is drawn with a redraw on the Canvas, but this will need to be run periodically. If it is redrawn once per second, it can emulate a clock ticking.

Eclipse has a jobs plug-in, which would be just right for this task, but this will be covered in Chapter 4, Interacting with the User. So to begin with, a simple Thread will be used to issue the redraw.

  1. Open the ClockView class.
  2. Add the following at the bottom of the createPartControl method:
    Runnable redraw = () -> {
      while (!clock.isDisposed()) {
        clock.redraw();
        try {
          Thread.sleep(1000);
        } catch (InterruptedException e) {
          return;
        }
      }
    };
    new Thread(redraw, "TickTock").start();
  3. Relaunch the test Eclipse instance, and open the Clock View.
  4. Open the host Eclipse instance and look in the Console View for the errors.

What just happened?

When the ClockView is shown, a Thread is created and started, which redraws the clock once per second. When it is shown, an exception is generated, which can be seen in the host Eclipse instance's Console View:

Exception in thread "TickTock"
org.eclipse.swt.SWTException: Invalid thread access
  at org.eclipse.swt.SWT.error(SWT.java:4477)
  at org.eclipse.swt.SWT.error(SWT.java:4392)
  at org.eclipse.swt.SWT.error(SWT.java:4363)
  at org.eclipse.swt.widgets.Widget.error(Widget.java:783)
  at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:574)
  at org.eclipse.swt.widgets.Control.redraw(Control.java:2279)
  at com.packtpub.e4.clock.ui.views.ClockView$2.run(ClockView.java:29)

This is expected behavior in this case, but it's worth taking a dive into the SWT internals to understand why.

Many windowing systems have a UI thread, which is responsible for coordinating the user interface updates with the program code. If long-running operations execute on the UI thread, then the program can appear to hang and become unresponsive. Many windowing systems will have an automated process that changes the cursor into an hourglass or spinning beach ball if the UI thread for an application is blocked for more than a short period of time.

SWT mirrors this by providing a UI thread for interacting with the user interface, and ensures that updates to SWT components are performed on this thread. Redraws occur on the SWT thread, as do calls to methods such as createPartControl.

In the clock update example, updates are being fired on a different thread (in this case, the TickTock thread), and this results in the exception shown earlier. So how are these updates run on the correct thread?

主站蜘蛛池模板: 曲沃县| 宜春市| 茌平县| 大邑县| 沈丘县| 乌拉特前旗| 台中县| 浦县| 南岸区| 武强县| 扶绥县| 敦化市| 镇安县| 海盐县| 通江县| 怀柔区| 新巴尔虎左旗| 玛沁县| 崇义县| 武定县| 吉木乃县| 杨浦区| 泰安市| 绵阳市| 文登市| 石城县| 博客| 台南县| 息烽县| 康马县| 城固县| 攀枝花市| 横峰县| 齐河县| 师宗县| 临朐县| 措美县| 改则县| 汉川市| 朝阳县| 达日县|