473,404 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Help with Threads, and Callback from Unmanaged to Managed

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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Apr 19 '06 #1
2 1846
Hi Marcus,

"Marcus Kwok" <ri******@gehennom.invalid> wrote in message
news:eJ****************@TK2MSFTNGP02.phx.gbl...
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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply


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/de...ms06112002.asp
[2]
http://msdn.microsoft.com/library/de...ms08162002.asp
[3]
http://msdn.microsoft.com/library/de...ms01232003.asp

Marcus Heege
Apr 20 '06 #2
Marcus Heege <NO****@heege.net> wrote:
Hi Marcus,

"Marcus Kwok" <ri******@gehennom.invalid> wrote in message
news:eJ****************@TK2MSFTNGP02.phx.gbl...
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.
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/de...ms06112002.asp
[2]
http://msdn.microsoft.com/library/de...ms08162002.asp
[3]
http://msdn.microsoft.com/library/de...ms01232003.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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Apr 25 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Cool Guy | last post by:
Consider: void Start() { if (!TryToDoSomething()) ShowErrorMessage(); }
4
by: Sai Kit Tong | last post by:
I have to interface managed application with my legacy dll. I have employed the wrapper approach but I have to deal with the asynchronous callback from the legacy dll, which likely goes through a...
2
by: Sasha Nikolic | last post by:
I have one unmanaged and one managed class in the same vc++ project. Managed object creates one instance of unmanaged class and needs to pass a callback reference so that unmanaged class can...
5
by: Maxwell | last post by:
Hello, Newbie question here. I have a VS.NET 2003 MC++ (not C++/cli) project where I have a managed class reference in a unmanaged class...simple enough. To keep things short I am for the most...
3
by: Tyler | last post by:
Can someone help by explaining why the following class will not compile (VS2005), and what can be done to pass the function pointer to the "C" method? When I compile the following program, I get...
7
by: harishashim | last post by:
I am wrapping a digital camera API using Managed C++ VS .NET 2003). I have this function that called as bellow in the API sample. err = PR_RC_StartViewFinder( m_hCamera, //line 1...
3
by: markb | last post by:
Hi My C# app is being called from a callback from an unmanaged DLL. One of the parameters of the callback is of type BOOL. I am using PInvoke to marshal this to a (managed) bool. The problem is...
0
by: DavidT | last post by:
Hello, at first, exuse if the following question is simple to solve, but i normaly coding with C# and now have to use C++/CLI for one project. My Problem is that i have to use a native c++ sdk...
1
by: Kenneth Porter | last post by:
I have an unmanaged library that can accept an instance of an abstract class and call back into it. I'm writing a wrapper for this library. How do I wrap a managed callback in a class derived...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.