Jump to content

Do Two Tasks at Once


centenial

Recommended Posts

Hello,

 

I have a while Loop that works something like this (I've simplified it so you can see the general logic of what I'm trying to accomplish):

 

<?php

$i = 1;

while (!file_exists($completed_file))
{
    -- progress bar code here --

    if ($i == 1)
    {
       ProcessTask();
    }

    $i++;
}
?>

 

Basically, I want to output a progress bar while I perform a task in the background. When the task is completed it writes a $completed_file txt file, so I know when to stop the progress bar.

 

The problem is that when it gets to the ProcessTask() function, it stops to run that WHOLE function. So basically it sits at 10% until the ProcessTask function is completed, and then it outputs everything to the page. Instead of that I want it to run the Function in the background, allowing my progress bar to run smoothly and completely.

 

Is there a way to run a task/function in the background of the while loop, so my progress bar doesn't get interrupted?

 

Your advice/experience is very much appreciated.

Link to comment
https://forums.phpfreaks.com/topic/55111-do-two-tasks-at-once/
Share on other sites

You can only implement a progress bar if the process has some way of reporting back it's progress. Since I don't know what ProcessTask() does, it's impossible to say wether it is doable or not.

 

As kenrbnsn stated, you would need javascript to do this, but you could implement a Javascript + PHP solution (aka, AJAX) to implement this.

 

Now, if your ProcessTask() function can be broken down into smaller tasks, then you could have those tasks fire in succession reporting back to the page via AJAX and updating the progress that way.

Link to comment
https://forums.phpfreaks.com/topic/55111-do-two-tasks-at-once/#findComment-272475
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.