Link to home
Start Free TrialLog in
Avatar of srinathchow
srinathchowFlag for United States of America

asked on

pass the method from c++ to C# for ThreadPool.QueueUserWorkItem

Hi I am trying to something like

I have a method called process written in c++. and trying to do ThreadPool.QueueUserWorkItem(process) in C#. This ThreadPool.QueueWorkItem is defined in C# in the constructor in a class

So, I got the instance of that class in c++ and trying to pass the c++ method to the C# file for Threadpool.

Any help is greatly appreciated! Thanks a lot!
Example Code:
 
C# code:
public delegate void Process();
public class ThreadClass
{
//Constructor
public ThreadClass(Process del,string x)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(0,del), x);
}
}
 
c++ code:
int main(array<System::String ^> ^args)
{
string x = "something";
//when the below call is made the prcess must be queusd for work
ThreadClass(&process,x);
}
 
static void process()
{
// do something
}

Open in new window

Avatar of Omego2K
Omego2K
Flag of United States of America image

1. Declare your ThreadClass as follows:

public delegate void Process(object test);
    public class ThreadClass
    {
        public ThreadClass(Process del, string x)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(del), x);
        }
    }


2. Declare your process method in C++ as follows:
Void process(Object ^ee){MessageBox::Show("addsa");}

3. To use the delegate, use this code:
String ^x = "something";
ClassLibrary1::Process ^pro = gcnew ClassLibrary1::Process(this, &Form1::process);
ClassLibrary1::ThreadClass ^dd = gcnew ClassLibrary1::ThreadClass(pro, x);

ASKER CERTIFIED SOLUTION
Avatar of Omego2K
Omego2K
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of srinathchow

ASKER

Thanks for the help Omego!

I am lookiing for the 3rd scenario and tried it. I directly used the Threadpool::QueueUserWorkItem in c++ and it works. I am trying it pass it to C# for Queuing because I am new to c++ and I got errors which got no success.

The mistake i am doing is i am declaring the void Process(System ^ Object) in main.h instead of main.cpp file. By following your above steps mentioned in 3 solved my problem.

code:
using namespace main;
void Process(Object^ signal);
int main(array<System::String ^> ^args)
{
String ^x = "something";
System::Threading::ThreadPool::QueueUserWorkItem(gcnew WaitCallback(&Process,x);
}

static void Process(Object^ signal)
{ //do something }

Thanks a lot!