简体   繁体   English

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

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

I am interfacing with a JNI that allows me to use a fingerprint scanner. 我正在与允许我使用指纹扫描仪的JNI连接。 The code I have written takes the scanned ByteBuffer parsed back to it by the JNI and turns it into a BufferedImage for saving. 我编写的代码将扫描的ByteBuffer解析为JNI并将其转换为BufferedImage进行保存。

What I can't figure out is how to wait for the scan thread to finish before the the jlabel icon on my GUI tries to update. 我无法弄清楚的是如何在我的GUI上的jlabel图标尝试更新之前等待扫描线程完成。 What would be the easiest way to do this? 最简单的方法是什么?

What else do I need to add? 还需要添加什么?

Edit: 编辑:

//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
} 

Not sure if you are using Swing or Android for UI but you would want to notify the main event dispatch thread (in swing it is called just that). 不确定你是否使用Swing或Android for UI但是你想要通知主事件派遣线程(在swing中它被称为)。 You would run the scanning thread, then when complete send a 'message' to the EDT with the action that you want to be done to the button. 您将运行扫描线程,然后在完成时向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");
              }
         });
     } 
});

In UI development there is no waiting for an action to complete because you always want the EDT to be responsive. 在UI开发中,无需等待操作完成,因为您总是希望EDT能够响应。

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

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

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