Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8065759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:46:04+00:00 2026-06-05T11:46:04+00:00

I am facing a very silly problem in my project and not able to

  • 0

I am facing a very silly problem in my project and not able to resolve it. I am using AsyncTask for fetching current location of user. In that i used one counter to work as timer. Inside doInBackground I am incrementing that counter and if its greater than x value then i cancel the async task.

Here is the code snippet : –

class CurrentPositionTask extends AsyncTask<String, Void, Void> {
        long counter = 0;
        boolean flag = true;

        @Override
        public void onPreExecute() {
            // TODO Auto-generated method stub
        }

        @Override
        public Void doInBackground(String... params) {
            // TODO Auto-generated method stub
            do {
                counter++;
                if (latitude != 0 && longitude != 0) {
                    flag = false;
                }
            } while (counter <= 100000);
            return null;
        }

        @Override
        public void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
        }

    }

But here the while condition is not working. I also tried the for loop instead of while but that is not also working for me. Also face very strange problem, when i print the counter using sysout its working fine but without it its not working..

All suggestions are welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:46:06+00:00Added an answer on June 5, 2026 at 11:46 am

    This loop

    while (counter <= 100000 || flag);
    

    will go on as long as any of the conditions hold, so it will continue while counter <= 10000 or flag is true. If flag is false but counter is less than 100000 it will continue. If you intend to terminate the loop when flag is false, then you need to do instead:

    while (counter <= 100000 && flag);
    

    Update: It seems from your comments that you expect the hundred thousand incrementation of the counter variable to take roughly 20-30 seconds. Current day processors are way faster than that. I won’t even mention the speed variation between processors, because its negligible anyway. You can wait for real 30 seconds with this loop, though is a busy-waiting loop (just like your original intent) and not something nice to do:

    final long startTime = System.currentTimeMillis();
    do {
        ...
    } while( System.currentTimeMillis() - startTime <= 30 * 1000 );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm facing a very silly problem and its really hard to resolve for me.
In my 2nd ASP.NET MVC project I'm facing a very weird problem: when I
A very standard example of the problem that I am facing is that of
I'm facing with an old problem that it made me confuse very much. So
I am facing a very silly problem with my code for buttons in the
I am facing the very strange problem in HTML/CSS.I am using chrome 12.0.742.122. Now
I'm facing a very strange problem in my ASP.NET Application. When the user clicks
I am facing a very weird problem: I am using mailer.py as post-commit hooks
This is a very basic and silly question but i am facing this problem
I facing very huge problem now, that is I have usercontrol (created by me)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.