How to interrupt infinite loop

want to stop infinite loop

Page 1 of 1

7 Replies - 19081 Views - Last Post: 24 May 2008 - 03:12 PM Rate Topic: -----

#1 Person  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-May 08

How to interrupt infinite loop

Post icon  Posted 24 May 2008 - 04:58 AM

Hi Everybody,
I want to know what is the best way to stop infinite loop (Note: I need infinite loop in my code but I can't stop it)

Thank you
Person


boolean flag2 = true;
		boolean Flag3=true;
		while (flag2) {
			arr =getarray(arr);

			try {
				resultarr = Translate(scale, Xrotat, Yrotat, Zrotat, arr);
			} catch (IOException e) {
				e.printStackTrace();
			}
			float Z_wrist = arr[6];
			float Z_Elbow = arr[7];
			if ((resultarr[0] != 0) && (resultarr[1] != 0)
					&& (resultarr[2] != 0)) {
				String data2 = resultarr[0] + " " + resultarr[1] + " "
						+ resultarr[2] + " MOVETO";
			
				System.out.println(data2);
				if ((Z_wrist > Z_Elbow)&&(Flag3==false)) {
					System.out.println("GRIP" );
					Flag3=true;
				} else if((Z_wrist<Z_Elbow)&&(Flag3==true)) {
					System.out.println("UNGRIP\r\n");
					Flag3=false;
				} {
					System.out.println("UNGRIP");
					Flag3=false;
				}
				
			} 
		}




Is This A Good Question/Topic? 0
  • +

Replies To: How to interrupt infinite loop

#2 .Maleficus.  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 129
  • Joined: 07-March 08

Re: How to interrupt infinite loop

Posted 24 May 2008 - 06:20 AM

If it's infinite, you can't stop it. If you do need to stop it, it won't be infinite. What good would an infinite loop be anyways? Just do this.

while (flag2 == true){

And make flag2 == false wherever you need the loop to end.

This post has been edited by .Maleficus.: 24 May 2008 - 06:21 AM

Was This Post Helpful? 0
  • +
  • -

#3 cutegrrl  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 77
  • Joined: 12-May 08

Re: How to interrupt infinite loop

Posted 24 May 2008 - 06:54 AM


// infinite loop
while(true){

if(condition){
break;
}



Was This Post Helpful? 0
  • +
  • -

#4 Person  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-May 08

Re: How to interrupt infinite loop

Posted 24 May 2008 - 07:04 AM

The problem is that this suppose to not end until user clicks in close button. I mean the loop should be controlled by GUI button.
when this program run all buttons in GUI are not active and I can't press any buttons.

Person
Was This Post Helpful? 0
  • +
  • -

#5 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: How to interrupt infinite loop

Posted 24 May 2008 - 10:04 AM

Well, in the close button method 'actionPerformed', you need to put System.exit(0), under the condition if the user presses the close button.

public void actionPerformed(ActionEvent e)
{
    // other button actions

    if (e.getEvent() == closeButton) // I don't remember if 'getEvent()' is right, i don't remember the exact method.
        System.exit(0);
}


And .Maleficus, you don't need the "== true" in your while loop codition, however it does still work.

while(flag2)
    // put flag2 = false; wherever you want it to stop.

This post has been edited by Locke37: 24 May 2008 - 10:09 AM

Was This Post Helpful? 0
  • +
  • -

#6 Person  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-May 08

Re: How to interrupt infinite loop

Posted 24 May 2008 - 10:26 AM

Close.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {

							System.exit(0);
						}
					});



I already have action performed method for the close button but it is not active while the program running. however it works when the program is not running(I mean when I display the GUI with out executing the infinite loop because there is a button to start executing the infinite loop).

I don't know why??!!
Was This Post Helpful? 0
  • +
  • -

#7 .Maleficus.  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 129
  • Joined: 07-March 08

Re: How to interrupt infinite loop

Posted 24 May 2008 - 11:52 AM

It's trying to finish your infinite loop before it processes the button click.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to interrupt infinite loop

Posted 24 May 2008 - 03:12 PM

View PostPerson, on 24 May, 2008 - 07:04 AM, said:

The problem is that this suppose to not end until user clicks in close button. I mean the loop should be controlled by GUI button.
when this program run all buttons in GUI are not active and I can't press any buttons.

Person

So made at least your "stop/close" button active before entering the loop.
The actionPerformed() method will be executed in a new thread and you can set flag2 = false there

This post has been edited by pbl: 24 May 2008 - 03:24 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1