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:oEvents();
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.
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:oEvents();
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.