0

I have a function that runs for about 0.7 seconds on my not-so-new development machine. (it runs for about 3 seconds on another machine I tested)

I want to show the user some pre-message about half a second before the above function is done.

I don't want to show the message too long before the function is done as it will be annoying to just look at it and wait. On the other hand, I would rather not wait until the function is done because the whole thing starts from a user action and I don't want to waste time - it's better if I can show that message while the other function is doing its job.

I've already added a loop with a short Thread.sleep() to let the pre-message hang if the function was "too fast" but I'm afraid that usually won't be the case... And so, I want to see if I could roughly estimate the execution time based on current machine specifications and even by the current CPU usage and do that before running the function. Also, since we are talking about seconds and milliseconds, if getting this information will take more than a few milliseconds then it's not worth it. In this case, I might calculate it only once when the application is loaded.

Does anybody have an idea how to do that?

5
  • 4
    Run the function on a background thread, have the message display on the UI thread, and remove the message when the background thread has completed its task. You don't need to estimate anything and you can still keep the UI free. Commented Mar 13, 2013 at 14:20
  • Assuming timing is always a bad idea. How would you know your function even runs for more than .5 seconds on any machine? Commented Mar 13, 2013 at 14:22
  • Assuming time is impossible: What if in the last 10s the user starts a heavy task which has higher priority and your task is just left to starve? Commented Mar 13, 2013 at 14:22
  • Is there some viable means of determining how much work is left? Commented Mar 13, 2013 at 14:23
  • in my case i cant show a "processing.." message. i only want a message (an image) to pop just before the process is done. of course i'm using separate threads to run the "heavy" function and to display the pop message Commented Mar 13, 2013 at 15:59

1 Answer 1

1

Estimating time is pointless, and most likely impossible to be acurate. (Though there might be some scenarios where it could be done).

Look at Windows file copying in the past "10 seconds left.... 2 minutes left.... 5 seconds left" It kept changing its estimate based on whatever metrics it used. Better to just show a spnning image, or message, to let the user know something is going on.

If you are processing a list of items, then it will be much easier for you, as the message could be:

Processing item 4 of 100.

At least then the user can know, roughly, what the code is doing. If you have nothing like this to inform the user, then I would cut your losses and show a simple "Processing...." message, or some icon, whatever takes your fancy for your solution.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.