login about faq

How does one cleanup the Task object after the task has completed? (in C++)

asked Sep 24 '15 at 09:58

chilkat's gravatar image

chilkat ♦♦
11.8k316358421


The CkTask object may be deleted immediately after calling the Run method. All Ck* objects are really just wrappers around a reference-counted underlying implementation object (the "Cls" objects -- which are not publicly exposed C++ classes). When a task begins in the background thread, the background thread has a reference to the underlying ClsTask object. Therefore, the app's CkTask object may be deleted. When the TaskCompleted callback happens, the CkTask reference that is passed to it is actually a new CkTask wrapper around the same instance of the "ClsTask" internal object.

Here's a code sample:

static bool _taskCompletedCallbackHappened = false;

class MyTaskProgress : public CkHttpProgress {

public: MyTaskProgress() { printf("constructorn"); }; virtual ~MyTaskProgress() {};

void TaskCompleted(CkTask &task) 
{ 
    // The CkTask object passed in here is actually a new CkTask wrapper
    //wrapped around the same internal ClsTask implementation object..
printf("TaskCompleted called\n"); 
_taskCompletedCallbackHappened = true;
};

};

static MyTaskProgress _taskProgress;

bool AsyncTesting::qa_asyncTaskCompleted(void) { CkHttp http;

// Always make sure to set the EventCallbackObject before 
// calling the Async method..
http.put_EventCallbackObject(&_taskProgress);
CkTask *task = http.GetHeadAsync("http://www.chilkatsoft.com/");

task->Run();
// It is OK to delete the CkTask object here..
delete task;

// Let's hang around to see if the callback happens and the task completes.
// (We don't have to do this..  
// the application could instead continue with anything else)   
while (!_taskCompletedCallbackHappened)
{
Sleep(10);
}

printf("qa_asyncTaskCompleted success");
return true;
}
link

answered Sep 24 '15 at 10:04

chilkat's gravatar image

chilkat ♦♦
11.8k316358421

edited Sep 24 '15 at 10:06

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×65
×22
×9

Asked: Sep 24 '15 at 09:58

Seen: 698 times

Last updated: Sep 24 '15 at 10:06

powered by OSQA