Search code examples
javaswingsingle-instance

Notification popup doesn't run with single instance


I want to display notification pop up when user attempts to run application second time , So I create NotificationPop obj and call the method to display the dialog within my single instance class ,but , it doesn't display popup when application runs second time there is no problem with my NotificationPop window it functions normal however when I call it within Single Instance doesn't display. in output window of Netbeans it displays dialog is closed as well. Do i miss any step here ?

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

public class SingleInstance {

    public static File f;
    public static FileChannel channel;
    public static FileLock lock;
    public static TrayCon trayobj;
    public static boolean checkstatus;
    public static NotificationPop obj;

    public static void main(String args[]) throws IOException {
        try {
            f = new File("key");
            if (f.exists()) {
                f.delete();
            }
            channel = new RandomAccessFile(f, "rw").getChannel();
            lock = channel.tryLock();
            if (lock == null) {
                obj = new NotificationPop();
                obj.displaypopupmessage();
                System.exit(0);
                channel.close();
                throw new RuntimeException("Only 1 instance can run");
            }

            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {

                    trayobj = new TrayCon();
                    trayobj.CreateTrayCon(trayobj);
                }
            });

        } catch (IOException ex) {
        }
    }
}

Solution

  • ok I have added thread sleep 6 seconds , fixed the issue

            if (lock == null) {
    
            obj = new NotificationPop();
            obj.proragramstatuswarning();
            Thread.sleep(6000);
            System.exit(0);
            channel.close();
            throw new RuntimeException("Only 1 instance can run");
    
    
        }