How can I make my program Run forever?

I would like to know how can I make my program execute some method while the GUI still open. I have something like this.
  public static void main(String args[]) {
    System.out.println("****************************");
    System.out.println("    PACKET READER CONSOLE   ");
    System.out.println("****************************");
    IPDetector window = new IPDetector(); // IPDetector is the JFrame
    window.setTitle("IPDetector Analyzer");
    window.pack();
    window.show();
    PortListener pl = new PortListener();// Is my portlistener class
    PacketReader c = new PacketReader();
    while(JFrame still open){// I dont know how to put a statemente here
      pl.start();// this method reads from a port and returns a string
      String cc = pl.data;// gets the string from the port listener
      while(!cc.equals("")){
        c.portWriter(cc);// writes the string into a file
  }I want that my portlistener keeps reading all the time, and if is something in the socket information.
Should I use a thread? Any ideas? thanks.
Chris

I still not understanding how to make it thread. My main class is this one IPDetector. and it looks like this.
public class IPDetector extends JFrame {
  // Declaration of the instance variables
  private static ArrayofDisplay  ad = new ArrayofDisplay();
  private ArrayofCreators database = new ArrayofCreators();
  JLabel sourceLabel;//etc..
  public IPDetector() {
    IPDetectorLayout customLayout = new IPDetectorLayout();
    getContentPane().setFont(new Font("Helvetica", Font.PLAIN, 12));
    getContentPane().setLayout(customLayout);
    sourceLabel = new JLabel("Source IP Add.");
    getContentPane().add(sourceLabel); 
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
  // I get confused here...
  private boolean alive; // Do I need to declared here?
  public void setAlive(boolean val) { // This one also?
    alive = val;
  // IPDetector Methods...
  public void displayCaller(ArrayofDisplay aD){  }
  public void setAndReplace(String text)  {    }
  public void refresh(){ }
  public boolean action(Event evt, Object arg){ }
  //etc...
  public static void main(String args[]) {
    System.out.println("***********************************************");
    IPDetector window = new IPDetector();
    window.setTitle("IPDetector");
    window.pack();
    window.show();
    PortListener pl = new PortListener();
    PacketReader c = new PacketReader();
    while (alive) {// Is this correct here?
      pl.start();
      String cc = pl.data;
      while(!cc.equals("")){
        c.portWriter(cc);
        window.refresh();
        cc = "";
class IPDetectorLayout implements LayoutManager {
  public IPDetectorLayout() {  }
  public void addLayoutComponent(String name, Component comp) {  }
  public void layoutContainer(Container parent) {  }
}

Similar Messages

  • How can I make a program run in the background?

    I'm working on a custom console program. I plan to have the commands as seperate applications and create a server/client relationship so they can communicate with each other. However whenever I call a command I don't want to have a console window pop up, flicker out, etc. I want it to look like it's one complete app. So I'm wondering how can I have the app run in the background?

    If you're using a unix system, use the "&" argument (or character, what is its name anyway?) at the end of the command line.
    I think you can use javaw in the MS-Windows world.

  • How can I make my program run faster ? (changing colour indicators quicker)

    Hi everyone, I have a program with 100 indicator colour backgrounds that change according to 100 temperatures. A case structure (with 100 cases) sets the background colours one after the other and runs in a while loop with no timing (as fast as possible). The temperatures are loaded up from an array that is initialized at the beginning of the program. The idea is to have an overall view of the change of temperatures (just by looking at the change of colours). Everything works fine except that it doesn’t run fast enough... The change of colours is not smooth at all ... you can really see the increments. It works fine with 20 temperatures for instance. 1) Would anybody have a solution to improve this? I was wondering if Labview perhaps actualises all of the values each time one changes (just like with the excel cells: an option that you can desactivate and make things run quicker when you use a macro) which would make a big loss of time....  
    There is no data saving this is just a post treatment program. So My my program isn't doing anything else.
     2) I have attached a screen print of a problem a faced when making up the VI I attached. Why can’t I wire directly to the Index Array that is in the For Loop in my VI… I had to go throw a shift register to make up the example. When going back to my original program I couldn’t use the index array a second time (screen shot). Thanks a lot for any help, Regards, User  
    Solved!
    Go to Solution.
    Attachments:
    Forum question.vi ‏13 KB
    forum.JPG ‏50 KB

    Here's a possible solution:
    Ton
    Message Edited by TCPlomp on 11-03-2010 11:16 AM
    Free Code Capture Tool! Version 2.1.3 with comments, web-upload, back-save and snippets!
    Nederlandse LabVIEW user groep www.lvug.nl
    My LabVIEW Ideas
    LabVIEW, programming like it should be!
    Attachments:
    Forum question.png ‏23 KB

  • How can we let the program run in backgroud automatically?

    normally,we need to find peak off time run big program
    that read pooled table and slowly.
    How can we let the program run in backgroud automatically?
    BTW:before that pop a window just give runner a notice.

    Here is an example, try this one.
    REPORT ztest.
    PARAMETERS: p_vbeln LIKE vbak-vbeln,
                p_bkrun NO-DISPLAY.
    DATA: ls_vbak LIKE vbak.
    DATA: v_answer,
          v_jobcount LIKE tbtcjob-jobcount.
      IF p_bkrun IS INITIAL.
    *-- not background processing
        CALL FUNCTION 'POPUP_TO_CONFIRM_STEP'
             EXPORTING
                  textline1      = 'This may time out.'
                  textline2      = 'Do you want to run in background?'
                  titel          = 'Warning!!!'
                  cancel_display = space
             IMPORTING
                  answer         = v_answer.
        IF v_answer = 'J'.
    *-- run in the background
          CALL FUNCTION 'JOB_OPEN'
               EXPORTING
                    jobname          = 'ZTEST'
               IMPORTING
                    jobcount         = v_jobcount
               EXCEPTIONS
                    cant_create_job  = 1
                    invalid_job_data = 2
                    jobname_missing  = 3
                    OTHERS           = 4.
          IF sy-subrc <> 0.
            MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno
                    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
            EXIT.
          ENDIF.
    *-- submit the program in the background
          SUBMIT ztest
            WITH p_bkrun = 'X'
            WITH p_vbeln = p_vbeln
            USER sy-uname
            VIA JOB 'ZTEST' NUMBER v_jobcount AND RETURN.
    *-- close the job
          CALL FUNCTION 'JOB_CLOSE'
               EXPORTING
                    jobcount             = v_jobcount
                    jobname              = 'ZTEST'
                    strtimmed            = 'X'
               EXCEPTIONS
                    cant_start_immediate = 1
                    invalid_startdate    = 2
                    jobname_missing      = 3
                    job_close_failed     = 4
                    job_nosteps          = 5
                    job_notex            = 6
                    lock_failed          = 7
                    OTHERS               = 8.
          IF sy-subrc <> 0.
            MESSAGE ID sy-msgid TYPE 'W' NUMBER sy-msgno
                    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
          ENDIF.
          EXIT.
        ELSE.
          CLEAR v_answer.
        ENDIF.
      ENDIF.
      CHECK v_answer IS INITIAL.
      SELECT SINGLE * FROM vbak
                      INTO ls_vbak
                     WHERE vbeln = p_vbeln.
      IF sy-subrc <> 0.
        WRITE:/ 'Invalid Order Id.'.
      ENDIF.
    END-OF-SELECTION.
      CHECK v_answer IS INITIAL.
      IF p_bkrun IS INITIAL.
        WRITE:/ 'Here is the result running the program in foreground.'.
      ELSE.
        WRITE:/ 'Here is the result running the program in background.'.
      ENDIF.
      WRITE:/ ls_vbak-vbeln,
              ls_vbak-vkorg.

  • How can I make my phone run faster?

    anytime I have to download or upload or search the web it is very very slow .

    ray miranda wrote:
    I have my iMac purchased in 2008 and it is running very slow.  I have a time capsule.  I believe it only has 1 GB of SD Ram.  Will it run faster/better if I increase the Ram?
    YUP!
    How large is you HD and how much space do you have left?
    Care to share which OS you are using?
    How can I make my iMac run faster?

  • How can i make my iphone run faster

    How can I make my iphone 4 run faster?

    ray miranda wrote:
    I have my iMac purchased in 2008 and it is running very slow.  I have a time capsule.  I believe it only has 1 GB of SD Ram.  Will it run faster/better if I increase the Ram?
    YUP!
    How large is you HD and how much space do you have left?
    Care to share which OS you are using?
    How can I make my iMac run faster?

  • How can i make simple program to match colors ?(photos taken by camera)

    how can i make simple program to match colors ?(photos taken by camera)

    Hi khaledyr,
    use a "random number" and compare for ">0.5" to get a (1 or 0) signal
    What exactly are you searching for?
    - Hints for choosing hardware?
    - How to output voltage or relais signals?
    - Help on using DAQ(mx)?
    - Help on generating (boolean) waveforms?
    Message Edited by GerdW on 04-16-2010 08:15 AM
    Best regards,
    GerdW
    CLAD, using 2009SP1 + LV2011SP1 + LV2014SP1 on WinXP+Win7+cRIO
    Kudos are welcome

  • How can i make my mac run powerpc applications?

    i want to play my downloaded game n every time i click it it gives me a message saying that power pc appliations arnt supported so i want to if theres a way how i can make y mac run power pc applications?

    Thanks Niel - I've deleted my post as it's obviously wrong. I learn something every day on here

  • �How can i make my program not to shutdown after using wsimport on runtime?

    I have this code:
    // More code before this one
    WsImport ws = new WsImport();
    String[] s = new String[3];
    s[0] = "-d";
    s[1] = "wsdl";
    s[2] = URL;
    try {       
        ws.main(s);
    } catch (java.lang.Throwable e) {
    // More code after this onewhen ws.main(s) finish its job it forces my program to shutdown too ='(
    How can i fix that?
    Any help will be really appreaciated.

    kevjava wrote:
    jschell wrote:
    [...] run it in another app and interact with it from your main app.As shown in the example here:
    [http://www.java2s.com/Code/JavaAPI/java.lang/ProcesswaitFor.htm|http://www.java2s.com/Code/JavaAPI/java.lang/ProcesswaitFor.htm]
    If the other application's main() method is calling System.exit(), this is the only semi-sane way that i know of to get around it.thanks, it worked:)

  • How can i make my macbook run faster again?

    Hey, i own 15 inch macbook pro 2011 that came with snow leopard and i upgraded it to lion but i observed that my computer got slower speacially when i start up i wait much more longer than i used to.I don't have many programs downloaded i mean only 130 gb is used out 500 so, is there anything i can do to make my mac run faster? In addition i am thinking about to make participation with win7 does it affect the speed of my computer negative? Thank you.

    If you have only 4GBs of RAM, this article may help you - https://discussions.apple.com/thread/3238726
    You may need to upgrade the RAM or make changes to the apps you use..  Since your Hard Drive is nicely underutilized and is probably behaving well, that should not be the problem.
    Check to see if you are getting Page Outs and read the article above

  • How can i make my mac run like new

    What can I do to make my computer run like when I first bought? Thanks

    I am confused. You posted in the FaceTime for Mac Forum and your profile says iPhone and iOS. So what is you are asking about here that is performing slowly? More details would really help.
    Allan

  • How can I make my macbook run faster?

    I've recently installed hp stuff for my printer and my hard drive is almost full.
    Is there anything else I can do besides clearing some memory and taking off the hp stuff? 
    I was also thinking about upgrading to snow leopard.  Would that make a difference?
    Thanks!!

    First, I think you are already running Snow Leopard? Your sig says "10.6.3".... Having said that, upgrading to a new OS when there is a problem will NOT make things better - it will usually make things worse. So, when there is a problem, it needs to be addressed first before thinking of upgrading or installing more stuff.
    Now to your hard drive space: Mac OS requires an absolute minimum of 10 - 15% of total hard drive space available at all times to operate properly. So how large is your hard drive? If you have less than 15% left, start clearing some space hogging stuff (usually movies, pics, music) onto an external drive. Also make sure you frequently use "empty trash". Before going further, please post your total hard drive capacity and how much is left. And, for future reference your hard drive is not "memory", that is usually referred to as "RAM".

  • How can i make the .exe run on the computer without Labview?

    .exe can't run on computer without Labview.  Is there any way?  Thank you.

    You need to install the LabVIEW Runtime engine for your specific LabvIEW version.
    In the project window you can create an installer that includes the Runtime Engine (RTE).
    Ton
    Free Code Capture Tool! Version 2.1.3 with comments, web-upload, back-save and snippets!
    Nederlandse LabVIEW user groep www.lvug.nl
    My LabVIEW Ideas
    LabVIEW, programming like it should be!

  • How do you make a program run when any user logs in?

    I have an application which will need to run when any users logs in.
    Such that Joe downloads and installs the application, logs out, then Sally logs in and the application runs for Sally.
    Does anyone know how to do this?

    Hey Steve, thanks for that link. It seems to be what
    I am looking for. What is the meaning of the
    ~/Library vs /Library ? They are definitely
    different folders.
    Yes, they are definitely different folders. The "~" character represents the current users home folder, so "~/Library" represents the Library folder that's located inside a users home folder. Anything placed in there will only affect the one user whose home folder you've accessed.
    The "/Library" folder is the Library folder that exists at to root of the boot volume. Things placed in this Library folder will affect all users of the system. Basically it's sort of a "global" Library.
    Also, from a script, how do I add an item to execute
    for that kind of PList?
    That could be tricky based on the structure of that particular plist file. I haven't really looked at it closely but one place you could start is to read the "man" page for the "defaults" command... enter "man defaults" in Terminal. The "defaults" command allows you to read/write plist files, but defaults is not very good at accessing deeply nested plist items.
    Related to that, how do I tell if the logging item
    for my App is already there? I do not want to keep
    adding to the list if it is there. If someone
    deletes my app and then reinstalls it, I do not want
    it to run twice, three times, etc..
    Again, you could possibly read the plist using the defaults command and determine whether your item was already present or not.
    Do you know of the one in the ~/Library path, what
    user it execute as? Since it is all users, it
    probably is root or something like that.
    No, the one in ~/Library is in each individual user home folder. It will execute with the current user's privileges. This is where Login Items normally go when you go through the GUI... "Sys.Prefs -> Accounts -> Login Items" and add a login item for one user.
    Even items placed in /Library, which should execute for all users, will execute with the current user's privileges.
    In the near
    future we might need root privileges, so I might need
    a program to startup for all users as root instead of
    the user.
    Is your app, that needs to run at login time, a GUI application or is it a faceless shell script (or something similar). Your original post gave me the impression that you needed to launch a GUI application. However, if it's a shell script then you probably want to look at doing a LoginHook instead of using the Login Items procedure at the web page I posted earlier. I believe a LoginHook will also give you the ability to run the script as root.
    Check out this link at the ADC website.
    or
    Take a look at this information and this utility at Mike Bombich's website.
    Steve

  • How can i make this query run for a form

    This query works fine for a sql report
    SELECT *
    FROM STUDENTS
    WHERE given_name||' '||family_name = :P101_USERNAME
    AND password = :P101_PASSWORD;
    But i want it to do the same thing for a form but i dont have the option like i do for the report where i can just apply the condition to the process.
    Anyone any ideas how i could do this so that when i go to the form this information is already there based on the username and password from the previous page?

    Assuming that page 101 is your login form, just lookup the primary key of the student based on the values presented in the login page fields, then set and pass this value to the form page.
    So you would create a process that runs near the very end of the the login processes on page 101, something like this:
    declare
    l_student_id students.student_id%TYPE;
    begin
    select student_id into l_student_id from students where  given_name||' '||family_name = :P101_USERNAME and password = :P101_PASSWORD;
    -- then set the value of your form page primary key field
    :P1_STUDENT_ID := l_student_id;
    end;I've done something similar and it works fine for me, although I haven't looked at it recently.
    Earl

Maybe you are looking for

  • 'Best for video' greyed out in displays prefs

    I finally got my macbook(blackbook running 10.5.8) hooked up to my lcd tv (i have been using my old eMac for this a while) & I'm fooling around with the settings to optimize performance & for some reason the 'Best for video' is greyed out in System p

  • PP sub-contracting error

    Hi Experts, While creating routing with external operation details I am getting this error, same Mat.group is maintained in Mat.master and Info record Material class differs from material class  from info record Message no. 06354 Diagnosis The materi

  • Trying to update my iPhone 3Gs.  Phone is now stuck with a picture of cd and usb picture on front of phone

    It states that it is stuck in recovery mode.  I am getting an error message with a code 1603.  Could someone please help.  Also, is there any way to not lose my personal information?  I am not computer savy...please help!

  • Read Hashmap into a Linked List

    Hi, I have a hashmap containing objects; I want to put those objects into a linked list. Where do I start? I haven't the foggiest idea.... I suppose I have to iterate through the hashmap and extract the objects and then put them into a linked list. D

  • How do you view desktop mode in safari?

    How do you view desktop version in safari in I phone 5