0

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) {
        }
    }
}
mussdroid
  • 732
  • 1
  • 9
  • 22
  • Why are you deleting the file (especially outside the lock section)? – MadProgrammer Aug 25 '14 at 06:49
  • you mean if fragment , let me add it there , – mussdroid Aug 25 '14 at 06:52
  • I'm sure that I'm posted here two(-three) various code in SSCCE/MCVE form (most importnant) for single instance FileIO/Socket/???, from your code isn't possible to suggest something – mKorbel Aug 25 '14 at 06:53
  • @mad programmer to avoid the dublication if the file already exist within folder – mussdroid Aug 25 '14 at 06:54
  • This question might be useful for you: https://stackoverflow.com/questions/177189/how-to-implement-a-single-instance-java-application – icza Aug 25 '14 at 06:55
  • It's likely that the call to `System.exit(0)` is been made before your notification popup is been shown...but we don't have the code for that to be 100% sure... – MadProgrammer Aug 25 '14 at 06:58
  • If the file is locked, what makes you think that it can be deleted? – MadProgrammer Aug 25 '14 at 06:58
  • @MadProgrammer sure, required, because a new JVM instance need to be killed, but this question is shot into dark – mKorbel Aug 25 '14 at 06:59
  • @mKorbel If the OP wants the file to be deleted when the first instance exists, that while not use `deleteOnExit`...the file can't be deleted while it's locked ... :P – MadProgrammer Aug 25 '14 at 07:00
  • Oh, by the way, when I stripped down you example, it works fine ;) – MadProgrammer Aug 25 '14 at 07:02
  • if don't put system exit , windows task manager full of running java process , everytime user runs the application , new java process is created under windows task manager – mussdroid Aug 25 '14 at 07:04
  • @MadProgrammer thats true, this is the good point, but opened File can't be deleted – mKorbel Aug 25 '14 at 07:09
  • Okay, you need to make your notification popup act like dialog. Stop code execution until you dismiss it or it closes and THEN call `System.exit`...again, don't have that code, can't tell you what to do... – MadProgrammer Aug 25 '14 at 07:10
  • I know that you guys are professional on these topics , please a little bit respect to the junior engineers like me , so please post your answer and help me :)) – mussdroid Aug 25 '14 at 07:12
  • yeah my notification pop up class works fine when it is called from other places even here if put it on top but whithin that if it doesnt work until i remove the system exit , if i remove system exit , multiple java processes created , so my application runs behind multiple times – mussdroid Aug 25 '14 at 07:12
  • The problem is it "seems" that the notification is not a blocking calling, it's not block the execution flow, so `System.exit` is allowed to be called BEFORE you notification is even displayed... – MadProgrammer Aug 25 '14 at 07:20
  • ok i removed system exit , pop up works fine but Java(TM) Platform SE binary process for each running the application new JPSEB created you can see under windows task manager https://www.dropbox.com/s/nrzy1ushnsyubnb/Windows%20Task%20Manager.PNG?dl=0 – mussdroid Aug 25 '14 at 07:27
  • You need some way to coordinate the two...You need to be able to display the notification, wait till it's "hidden" and THEN call `System.exit`... – MadProgrammer Aug 25 '14 at 07:34
  • is this going to help me ? http://stackoverflow.com/questions/15747277/how-to-make-java-program-exit-after-a-couple-of-seconds – mussdroid Aug 25 '14 at 07:56
  • Only if you know how long the notification is displayed for. Might be better to have a callback mechanism that would allow the notification object to send back some kind of event telling the caller when the notification is dismissed... – MadProgrammer Aug 25 '14 at 07:59

1 Answers1

0

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");


    }
mussdroid
  • 732
  • 1
  • 9
  • 22