繁体   English   中英

获取并设置线程中的Flag变量

[英]Getting and Setting a Flag variable in a thread

我正在与允许我使用指纹扫描仪的JNI连接。 我编写的代码将扫描的ByteBuffer解析为JNI并将其转换为BufferedImage进行保存。

我无法弄清楚的是如何在我的GUI上的jlabel图标尝试更新之前等待扫描线程完成。 最简单的方法是什么?

还需要添加什么?

编辑:

//Scanner class
Thread thread = new Thread() {
        public void run() {
            // [...] get ByteBuffer and Create Image code
            try {
                File out = new File("C:\\Users\\Desktop\\print.png");
                ImageIO.write(padded, "png", out);
                // [???] set flag here
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };
thread.start();
return true;

//Gui class
private void btnScanPrintActionPerformed(java.awt.event.ActionEvent evt) {
    Scanner scanPrint = new Scanner();
    boolean x = scanPrint.initDevice();
    //Wait for the scanning thread to finish the Update the jLabel here to show
    //the fingerprint
} 

不确定你是否使用Swing或Android for UI但是你想要通知主事件派遣线程(在swing中它被称为)。 您将运行扫描线程,然后在完成时向EDT发送“消息”,并执行您要对按钮执行的操作。

Thread thread = new Thread(new Runnable(){
     public void run(){
         //scan
         SwingUtiltilies.invokeLater(new Runnable(){
              //here you can update the the jlabel icon
              public void run(){
                  jlabel.setText("Completed");
              }
         });
     } 
});

在UI开发中,无需等待操作完成,因为您总是希望EDT能够响应。

在扫描线程的末尾,使用SwingUtilities.invokeLater()来更新gui和扫描结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM