Results 1 to 6 of 6

Thread: How to stop a Task spawned by TaskRunner

  1. #1
    Sencha Premium User
    Join Date
    Feb 2015
    Posts
    25
    Answers
    2

    Default Answered: How to stop a Task spawned by TaskRunner

    I'm having difficulty in controlling tasks once they have started. Of course if i use Ext.utilTaskManager.stopAll(), it does what it's supposed to do. Basically i have a grid and the grid has a checkbox in its toolbar in docedItems that has a listeners: { change: 'pollNetworks' }.

    This calls a function in my ViewController for that grid panel. The pollNetworks function looks as follows:

    Code:
        /**
         * @param {Ext.form.field.Field} component
         * @param {Object} newValue
         */
        pollNetworks: function (component, newValue) {
            var combo = Ext.ComponentQuery.query('combobox[name=tag_name]')[0];
            var pollNetworks = function () {
                Ext.getStore('networks').load({
                    params: {tag_name: combo.value}
                });
            };
    
            var runner = new Ext.util.TaskRunner();
            var task = runner.newTask({
                scope: this,
                run: pollNetworks,
                interval: 30000
            });
    
            if (newValue === true) {
                console.log(task);
                Ext.Msg.alert('info', 'starting pollNetworks task');
                task.start();
            } else {
                if (component.getName() === 'poll_networks' && newValue !== true) {
                    console.log(task);
                    Ext.Msg.alert('warn', 'starting pollNetworks task');
                    task.stop();
                }
            }
        },
    The idea is that when the checkbox is checked - it should start the task, and when unchecked, it should stop or destroy the task. I've added Ext.Msg.alert() calls for debugging purposes and they work just fine. The task however, never stops. I can see while tailing the webserver, that the same call is being made. How do you do this with TaskRunner?

    Part II - I used Ext.util.TaskManager and simply put a stopAll() and this worked. My problem is i have another window that can be launched as a modal on top of the grid panel w/ its own pollTask for different detail data. If i use Ext.util.TaskManager.stopAll() this stops both tasks and i really need to be able to manage each task independently of the other, and that's what lead me to use TaskRunner. Any help would be appreciated. Let me know if more code is needed.

    Thanks

  2. You are creating new instances of the task every time pollNetworks is called. Instead you should create it once and store the reference for later use. Something like the following:

    Code:
    pollNetworks: function(component, newValue) {    
        
        if (!this.pollingTask) {
            var combo = Ext.ComponentQuery.query('combobox[name=tag_name]')[0],
                pollNetworks = function() {
                    Ext.getStore('networks').load({
                        params: {
                            tag_name: combo.value
                        }
                    });
                };
            this.pollingTask = Ext.util.TaskManager.newTask({
                scope: this,
                run: pollNetworks,
                interval: 30000
            });
        }
    
    
        if (newValue === true) {
            Ext.Msg.alert('info', 'starting pollNetworks task');
            this.pollingTask.start();
        } else if (component.getName() === 'poll_networks' && newValue !== true) {
            Ext.Msg.alert('warn', 'starting pollNetworks task');
            this.pollingTask.destroy();
        }
    }

  3. #2
    Sencha Premium Member MaximGB's Avatar
    Join Date
    Jun 2007
    Location
    Moscow, Russia
    Posts
    542
    Answers
    4

    Default

    RTFM

    Code:
    var task = Ext.TaskManager.start({
        run: updateClock,
        interval: 1000
    });
    
    ...
    
    task.destroy();
    p.s. Task's start/stop methods also might be useful
    Use the force - read the source.

  4. #3
    Sencha Premium User
    Join Date
    Feb 2015
    Posts
    25
    Answers
    2

    Default

    Quote Originally Posted by MaximGB View Post
    RTFM

    Code:
    var task = Ext.TaskManager.start({
        run: updateClock,
        interval: 1000
    });
    
    ...
    
    task.destroy();
    p.s. Task's start/stop methods also might be useful
    I'm trying with something functionally equivalent no?
    Code:
        
        pollNetworks: function (component, newValue) {
            var combo = Ext.ComponentQuery.query('combobox[name=tag_name]')[0];
            var pollNetworks = function () {
                Ext.getStore('networks').load({
                    params: {tag_name: combo.value}
                });
            };
    
            var task = Ext.util.TaskManager.newTask({
                scope: this,
                run: pollNetworks,
                interval: 30000
            });
    
            if (newValue === true) {
                Ext.Msg.alert('info', 'starting pollNetworks task');
                task.start();
            } else {
                if (component.getName() === 'poll_networks' && newValue !== true) {
                    Ext.Msg.alert('warn', 'starting pollNetworks task');
                    task.destroy();
                }
            }
    I don't understand why this would not work. I'm creating an instance of a task and running instance methods on it to stop and/or destroy it.

  5. #4
    Sencha User
    Join Date
    Apr 2013
    Posts
    913
    Answers
    105

    Default

    You are creating new instances of the task every time pollNetworks is called. Instead you should create it once and store the reference for later use. Something like the following:

    Code:
    pollNetworks: function(component, newValue) {    
        
        if (!this.pollingTask) {
            var combo = Ext.ComponentQuery.query('combobox[name=tag_name]')[0],
                pollNetworks = function() {
                    Ext.getStore('networks').load({
                        params: {
                            tag_name: combo.value
                        }
                    });
                };
            this.pollingTask = Ext.util.TaskManager.newTask({
                scope: this,
                run: pollNetworks,
                interval: 30000
            });
        }
    
    
        if (newValue === true) {
            Ext.Msg.alert('info', 'starting pollNetworks task');
            this.pollingTask.start();
        } else if (component.getName() === 'poll_networks' && newValue !== true) {
            Ext.Msg.alert('warn', 'starting pollNetworks task');
            this.pollingTask.destroy();
        }
    }

  6. #5
    Sencha Premium Member MaximGB's Avatar
    Join Date
    Jun 2007
    Location
    Moscow, Russia
    Posts
    542
    Answers
    4

    Default

    Because you always re-create the task. The task instance you'd run and the one you stop are two different entities.
    Use the force - read the source.

  7. #6
    Sencha Premium User
    Join Date
    Feb 2015
    Posts
    25
    Answers
    2

    Default

    Thanks to you both! I was zeroing in on that issue just as you posted... This solved that issue. Appreciate it. I'd like to accept both your responses as the best solution. Thanks for taking the time.

    Cheers

Similar Threads

  1. TaskRunner task taskStartTime, taskRunTime
    By aarffy in forum Ext: Q&A
    Replies: 1
    Last Post: 5 Nov 2014, 4:27 PM
  2. Replies: 1
    Last Post: 19 Apr 2012, 9:14 AM
  3. [FIXED] 4.1 Beta 3: TaskRunner - initial run of task is delayed
    By mdipersio in forum Ext:Bugs
    Replies: 1
    Last Post: 23 Feb 2012, 10:58 AM
  4. is this a bug of TaskRunner?
    By aj3423 in forum Ext 3.x: Help & Discussion
    Replies: 0
    Last Post: 2 Jun 2010, 8:49 PM
  5. Replies: 2
    Last Post: 19 Mar 2009, 12:56 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •