Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: ActionListener runs from timer appears to run again before first one is done

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    7
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question ActionListener runs from timer appears to run again before first one is done

    Below is a fragment of code I'm using (simplified for this post).

    I'm having a problem wherein it appears that my ActionListener runs again before the last one had completed. There are two possibilies that I can think of to explain what I am seeing by placing some debug variables in the code that runs when a doClick is called on a button.

    (I set a debug variable true at the beginning, and false at the end of the action for a doClick but this timer code sometimes sees the value of the debug variable as true - which it wouldn't unless....)

    Either,


    1. A repeating 50ms timer can invoke the code in my ActionListener a second time before the first one has finished (e.g. reached the final close bracket).
    2. The call to doClick from my ActionListener code somehow is running asynchronously and is still active after the doClick call, such that the next time the timer goes off, the button clicking code is still running.


    Can anyone explain what I am seeing?

    Is there an easy way to keep the ActionListener code from running again before the previous one has finished - if this is what is happening?

    [CODE=java]
            ActionListener al = new ActionListener() {
                AT-SIGN Override // forum won't let me put an actual at sign here, says I have to many urls???
                public void actionPerformed(ActionEvent arg0) {
                    try{
                         // if ( debug variable is true )  then log this as a strange event
                         //   ....   invoke a doClick on a Jbutton object ...
                    }catch(IOException io){
                        io.printStackTrace();
                    }
                }
            };
     
     
     
     
            // this code is executed one time only
            Timer tm= new Timer(1000,al); 
            tm.setInitialDelay(1000);
            tm.setDelay(50);
            tm.start();


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: ActionListener runs from timer appears to run again before first one is done

    It's unlikely that the action listener code is being called multiple times within 50ms by the timer, but it may be triggered by some other code that uses the same listener. Inspect all code that uses 'al' as its listener and determine the fix. The obvious fix would be to not use 'al' multiple times; create new listeners for each component that needs one. (A doClick() call in a timer's action listener is suspect, but I suppose there could be a reason for it.)

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    7
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener runs from timer appears to run again before first one is done

    Quote Originally Posted by GregBrannon View Post
    It's unlikely that the action listener code is being called multiple times within 50ms by the timer, but it may be triggered by some other code that uses the same listener. Inspect all code that uses 'al' as its listener and determine the fix. The obvious fix would be to not use 'al' multiple times; create new listeners for each component that needs one. (A doClick() call in a timer's action listener is suspect, but I suppose there could be a reason for it.)
    The ActionListener calls a doClick which does quite a bit of work, including opening a tcp connection and sending a command. If doClick waits for everything to get done in the button's action code (including blinking an icon) then it's entirely likely that it would take more than 50ms.

    BTW, I cannot change anything else in the program, it's not my code, I'm just adding a small extension. I wrote this ActionListener and the timer in the code I showed, so I know nothing else is using that ActionListener. More precisely, is I google-coded it, i.e. just copy and pasted an example. I really don't know the details of how this works in java.

    I think you are telling me to not repeat automatically, but rather start the next timer only after I'm done with the current. This would be my choice, but I don't know how to do that. I really am a novice java programmer. I was hoping that the timing was such that the next 50ms wouldn't start counting until I left the ActionListener - i.e. drop through the last close bracket.

    So, that would insure that the code does not reenter before finished. But I still don't know if the doClick code is running async in a seperate thead. It could be that my current situation is that both the above cases are possible, that the timer re-entered and doClick does not wait for the button action to be completed.


    (why a doClick in the timer action? Weeeelll...

    I'm building a quick solution that let's me leverage on all the work needed to send remote commands to a tivo over the network. I run an external program in a pipeline - myprogram | java program - and inside the java program I poll stdin in the timer and do a read only if there's something been sent from myprogram. Then if something is there, I interpret it and invoke the doClick. The java program GUI has buttons for sending pausing and skipping commands via tcp/ip to a tivo.

    I'm sure there's a better way to do this, but except for this timing issue, it works. And I have a workaround by putting a public flag variable in the doClick action code and one big if statement in the ActionListener to detect if that code is running and do nothing else until the next timer goes off. But I'd like to know better what's going on here and perhaps find a cleaner solution.
    )

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: ActionListener runs from timer appears to run again before first one is done

    Can you make a small, complete program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    7
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener runs from timer appears to run again before first one is done

    Quote Originally Posted by Norm View Post
    Can you make a small, complete program that compiles, executes and shows the problem?
    (I think you replied before I finished editing my last post)

    That's a problem for me, as I really haven't the java experience to be able to write a complete program with a gui and all. And as I mentioned in my edit, the program I am "invading" is quite complex as to what it does when a button is clicked.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: ActionListener runs from timer appears to run again before first one is done

    If there is one action listener class, how many instances of it are there? Can there be more than one created and each one is called at its own times? To see: Make the listener a full class with a constructor that prints a message when an instance is created.

    Can more than one component be sending events to the listener at different times? Call the Thread class's dumpStack() method when it is called to see if the trace has any useful info.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2013
    Posts
    7
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener runs from timer appears to run again before first one is done

    The code for my ActionListener and timer is in the program's main, and I would guess that only gets executed one time. There is a second Timer (that I didn't write), but it would appear to have it's own ActionListener.


    Below's a little more context. I don't really understand the invokeLater or the run context. The only other Java code I wrote was about 20 years ago when I taught myself how to write a java applet. I have programmed mostly in C and tcl/tk with a little C++ experience.

    So, I'm afraid that the chances that I could make some of the changes you have suggested, and get it right, would make suspect any information that would gathered.

    I'm hoping that someone knows and can explain the little details about repeating timers and doClick. I figured this was the right sub-forum, as it had some questions on ActionListener's and deals with gui's. Perhaps I should repost in the more general "I have a problem" sub-forum.

    public static void main(String[] argv) {
         SwingUtilities.invokeLater(
            new Runnable() {
               public void run() {
                  // All uncaught exceptions go to message window in GUI
                  Thread.setDefaultUncaughtExceptionHandler(new myExceptionHandler());
     
                  // Create and display main frame
                  config.gui.getJFrame().setVisible(true);
     
                ActionListener al = new ActionListener() {
                     Override
                    public void actionPerformed(ActionEvent arg0) {
                        try{
                               // my code, poll stdin, parse, set gui text box values, call various doClicks
                        }catch(IOException io){
                            io.printStackTrace();
                        }
                    }
                };
     
                Timer tm = new Timer(1000,al); // Timer(TimeInMilliSeconds, ActionListener) 1000ms = 1s 
                tm.setInitialDelay(1000);
                tm.setDelay(50);
                tm.start();
               }
            }
         );
     
         // Invoke a 300ms period timer for job monitor
         timer = new Timer(300, new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
               jobMonitor.monitor(config.gui, timer);
            }    
         });
         timer.start();
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: ActionListener runs from timer appears to run again before first one is done

    Programs can be complicated. Without seeing the whole of a program with the problem, it may not be possible to say what is causing the problem.

    I would guess that only gets executed one time.
    That's not good enough. Every possibility must be checked out.

    details about repeating timers and doClick.
    That is exactly what I was trying to get you to debug in post#6.

    Does the doClick() method send an event to the listener? How is that event handled as compared to a user created event?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Dec 2013
    Posts
    7
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default question on repeating timers, and calling doClick from one

    If I set up a repeating timer, with one ActionListener, as shown below, can the ActionListener be re-entered before the prior invocation has finished?

    In the below code, with a 50ms repeat interval, suppose the ActionListener takes longer than 50ms, will another instance of the ActionListener code be invoked while the prior one is still busy?

    Also, if I call a doClick method on a gui button to simulate a user click, does the doClick return before the clicking code finishes or does it wait?

    // this code is executed one time only from main
     
    ActionListener al = new ActionListener() {
        Override
        public void actionPerformed(ActionEvent arg0) {
        	// poll stdin, if not ready do nothing
        	// otherwise read, interpret, and make calls to doClick 
        	// to simulate button clicks by user
        }
    };
     
     
    Timer tm = new Timer(1000,al); // Timer(TimeInMilliSeconds, ActionListener) 1000ms = 1s 
    tm.setInitialDelay(1000);
    tm.setDelay(50);
    tm.start();

    (I am reposting this as a question here; I think perhaps this is the better place to post than the awt/swing sub-forum where I originally posted)

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: ActionListener runs from timer appears to run again before first one is done

    Threads merged.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Dec 2013
    Posts
    7
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener runs from timer appears to run again before first one is done

    Quote Originally Posted by Norm View Post
    Programs can be complicated. Without seeing the whole of a program with the problem, it may not be possible to say what is causing the problem.

    That's not good enough. Every possibility must be checked out.

    That is exactly what I was trying to get you to debug in post#6.

    Does the doClick() method send an event to the listener? How is that event handled as compared to a user created event?
    Well, the whole program is in about 150 java files, probably about 50k lines of code. It's built using an xml file with something called ant. I downloaded it and followed the build from source instructions. Without that, I don't know enough java today to be able to build a hello world program.


    I've just added a few changes, notably this timer and action code. I guessed from my other language experience that a doClick causes something to be executed in the right context to simulate a button push by the user. I don't know how to call that code directly, or if it's even possible. That code should be completely unaware of my timer, so I don't see any way that it would be sending an event to my listener. I don't know enough java to even know if that is possible. The only reference to my listener in the entire program is in the call to the timer.

    I reposted because I thought it would be better to start over with much less information and simply ask a direct question. I don't think I can figure out what is going on here through trial and error.

    Sorry to be such a dum dum. This is my first post on this forum.

  12. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: ActionListener runs from timer appears to run again before first one is done

    Also posted here.

  13. #13
    Junior Member
    Join Date
    Dec 2013
    Posts
    7
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener runs from timer appears to run again before first one is done

    Well, you got me! I pretty much gave up getting an answer here, so I just posted it over there. I didn't realize the java world was so small. That was pretty quick

  14. #14
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: ActionListener runs from timer appears to run again before first one is done

    There's no "got you," no harm, no foul. Some sites don't appreciate cross-posting at all. This one and DIC tolerate it but appreciate knowing about it so that time isn't wasted either repeating advice given elsewhere or giving opposing advice due to different interpretations. It's just a common courtesy to provide a link to other posts that is appreciated by all.

  15. #15
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: ActionListener runs from timer appears to run again before first one is done

    This thread was closed in DIC for violating their rules against illegal activities, in this case hacking a Tivo box which is specifically prohibited by their user agreement. From the JPF Announcements topic:

    "5. This website does not condone any illegal activity (sharing of copyrighted materials illegally, planning any attacks against anyone, etc.). Any activity contrary to this will result in negative consequences."

    While the "negative consequences" aren't specified, I recommend this thread be closed.

Similar Threads

  1. Timer.scheduleAtFixedRate() is scheduling itself to run on the wrong day
    By JamesHenderson in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 11th, 2013, 04:24 PM
  2. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  3. Replies: 2
    Last Post: January 6th, 2012, 10:50 PM
  4. Replies: 7
    Last Post: October 13th, 2011, 03:03 AM