Help with Threads, and Callback from Unmanaged to Managed

M

Marcus Kwok

I have processing code (I'll call it the "model") written in native
unmanaged pure C++, and I have put a GUI on top of it written using
Windows Forms (.NET 1.1). The GUI is used to set the parameters for the
model, and once all parameters are set and checked to be valid, I run
the model. The model takes a long time to run, so I decided to run it
in a background thread, and have it notify the GUI when it is complete.
The GUI will then display the results of the model's run.


I have tried this:

Thread* model_thread = new Thread(new ThreadStart(this, &ControlPanel::run_model));
model_thread->IsBackground = true;
model_thread->Start();
//model_thread->Join();

while (model_thread->IsAlive) {
System::Windows::Forms::Application::DoEvents();
Thread::Sleep(0);
}

show_results();

which seems to work, but looping on the thread's status seems "dirty" to
me, and I remember reading somewhere in MSDN that thread status
shouldn't be used to synchronize threads. If I remove the loop, then
the GUI immediately tries to show the results before they are ready. If
I use Thread::Join(), then my GUI loses responsiveness (I plan on adding
"Cancel" functionality as well as a progress meter). An additional
problem with the way it works now is if the user clicks on my "Exit"
button during processing (which calls my form's MdiParent->Close()
method), the background worker thread doesn't terminate, and I have to
manually kill it using the Task Manager, even though it is set to be a
background thread!


I think I want something similar to a delegate, but the model must
remain as pure C++, i.e., no Microsoft extensions. I tried adding a
void* to the model and passing the GUI window's "this" pointer so that
it could call a generic callback() function, but apparently I cannot
convert a __gc pointer to a void pointer.

Ideally, I would like to be able to specify an object and a non-static
member function that the model should call when it is finished. I know
that in order to declare a pointer to member function, the class must be
known at declaration time, so I cannot just declare a generic function
pointer for it.

Would it work if I created an abstract base class that my GUI form can
inherit from, and use a pointer to member function of this ABC? For
example, would something like the following work:

class CallbackInterface {
public:
virtual void callback() = 0;
};


public __gc class ControlPanel : public System::Windows::Forms::Form
, public CallbackInterface {
// class implementation here
};


class Model {
void* callback_object;
void (*CallbackInterface::callback)();

void run() {
// processing that takes a while

callback_object->*callback(); // syntax may not be 100% correct
}
};


Thanks for any advice, and sorry for the long post.
 
M

Marcus Heege

Hi Marcus,

Marcus Kwok said:
I have processing code (I'll call it the "model") written in native
unmanaged pure C++, and I have put a GUI on top of it written using
Windows Forms (.NET 1.1). The GUI is used to set the parameters for the
model, and once all parameters are set and checked to be valid, I run
the model. The model takes a long time to run, so I decided to run it
in a background thread, and have it notify the GUI when it is complete.
The GUI will then display the results of the model's run.


I have tried this:

Thread* model_thread = new Thread(new ThreadStart(this,
&ControlPanel::run_model));
model_thread->IsBackground = true;
model_thread->Start();
//model_thread->Join();

while (model_thread->IsAlive) {
System::Windows::Forms::Application::DoEvents();
Thread::Sleep(0);
}

show_results();

which seems to work, but looping on the thread's status seems "dirty" to
me, and I remember reading somewhere in MSDN that thread status
shouldn't be used to synchronize threads. If I remove the loop, then
the GUI immediately tries to show the results before they are ready. If
I use Thread::Join(), then my GUI loses responsiveness (I plan on adding
"Cancel" functionality as well as a progress meter). An additional
problem with the way it works now is if the user clicks on my "Exit"
button during processing (which calls my form's MdiParent->Close()
method), the background worker thread doesn't terminate, and I have to
manually kill it using the Task Manager, even though it is set to be a
background thread!


I think I want something similar to a delegate, but the model must
remain as pure C++, i.e., no Microsoft extensions. I tried adding a
void* to the model and passing the GUI window's "this" pointer so that
it could call a generic callback() function, but apparently I cannot
convert a __gc pointer to a void pointer.

Ideally, I would like to be able to specify an object and a non-static
member function that the model should call when it is finished. I know
that in order to declare a pointer to member function, the class must be
known at declaration time, so I cannot just declare a generic function
pointer for it.

Would it work if I created an abstract base class that my GUI form can
inherit from, and use a pointer to member function of this ABC? For
example, would something like the following work:

class CallbackInterface {
public:
virtual void callback() = 0;
};


public __gc class ControlPanel : public System::Windows::Forms::Form
, public CallbackInterface {
// class implementation here
};


class Model {
void* callback_object;
void (*CallbackInterface::callback)();

void run() {
// processing that takes a while

callback_object->*callback(); // syntax may not be 100% correct
}
};


Thanks for any advice, and sorry for the long post.

nice first name :)

there are common patterns to solve your problem. If you build code with .NET
2.0, you can drop a .NET component named Backgroundworker on your form and
handle events DoWork, ProgressChanged, and RunWorkerCompleted.

If you use .NET 1.0 or 1.1, you have do some more manual work, but it is
really doable.

Read Chris Sells' teriffic articles about that topic

[1]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms06112002.asp
[2]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms08162002.asp
[3]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms01232003.asp

Marcus Heege
 
M

Marcus Kwok

Marcus Heege said:
Hi Marcus,



nice first name :)

Thanks, yours is pretty cool too :)
there are common patterns to solve your problem. If you build code with .NET
2.0, you can drop a .NET component named Backgroundworker on your form and
handle events DoWork, ProgressChanged, and RunWorkerCompleted.

If you use .NET 1.0 or 1.1, you have do some more manual work, but it is
really doable.

Read Chris Sells' teriffic articles about that topic

[1]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms06112002.asp
[2]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms08162002.asp
[3]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms01232003.asp

Thanks, indeed I am using .NET 1.1 so it looks like I have a bit of work
ahead of me. These articles look very interesting.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top