473,387 Members | 1,789 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,387 software developers and data experts.

thread parameters

I would like to pass an Object* to a delegated thread function.
However, the framework does not accommodate this. I have seen examples
using VB and even C#. In fact, C# appears to have language facilities
that make accomplishing this very easy. However, I only have the
luxury of coding this up in C++. So far the closest I've been able to
come to facilitating this is something like the following.

typedef void (*DelegateFunc)(Object __gc *);

__gc class ThreadClosure
{
private:

static Object* c_pArgs;
static DelegateFunc c_pfDelegate;

public:

ThreadClosure(
DelegateFunc pfDelegate,
Object* pArgs)
{
c_pfDelegate = pfDelegate;
c_pArgs = pArgs;
}

virtual ~ThreadClosure(void) {}

static void Run(void)
{
if (c_pfDelegate)
c_pfDelegate(c_pArgs)
}
};

The one flaw in the above scheme is the use of statis members. I have
no real guarantee between constructing an instance and initializing
the static closure and passing the run execution to the thread
delegate that the closure will remain what I set it to.

Ideally I'd like to pass a closure instance, or the Run method address
from that instance, to the thread delegate. Is this possible in the
C++ implementation of ThreadStart()?

Any tips would be massively appreciated!

Best regards,
Michael Powell
Nov 17 '05 #1
2 2359
Hi Mike,
I would like to pass an Object* to a delegated thread function.
However, the framework does not accommodate this. I have seen examples
using VB and even C#. In fact, C# appears to have language facilities
that make accomplishing this very easy. However, I only have the
luxury of coding this up in C++. So far the closest I've been able to
come to facilitating this is something like the following.


You can use the exact same facilities in MC++ as C# and VB.NET have for
asynchronous invocation of delegates. Here's a simple example:

#using <mscorlib.dll>

using namespace System;
using namespace System::Threading;

public __gc class ClassWithThreads
{
public:
void MethodWithArg(Object* data)
{
Object* argt __gc[] = { __box(Thread::CurrentThread->GetHashCode()),
data};
Console::WriteLine (
S"MethodWithArgs(): Thread Hash={0}, Data={1}", argt
);
}
};

public __delegate void ThreadDelegate(Object*);

int main()
{
Console::WriteLine(S"Main(): Thread Hash={0}",
__box(Thread::CurrentThread->GetHashCode()));
ClassWithThreads* obj = new ClassWithThreads();
ThreadDelegate* del = new ThreadDelegate(obj,
&ClassWithThreads::MethodWithArg);

IAsyncResult* ar = del->BeginInvoke (
S"some string",
0 /* AsyncCallback */,
0 /* asyncState */
);
ar->AsyncWaitHandle->WaitOne();
}

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #2
po******@comcast.net (Mike) wrote in message news:<62**************************@posting.google. com>...

I figured it out. Something closer to this.

typedef void (*DelegateFunc)(Object __gc *);

__gc class ThreadClosure
{
private:

Object* m_pArgs;
DelegateFunc m_pfDelegate;

public:

ThreadClosure(
DelegateFunc pfDelegate,
Object* pArgs)
{
this->m_pfDelegate = pfDelegate;
this->m_pArgs = pArgs;
}

virtual ~ThreadClosure(void) {}

static void Run(void)
{
// It is up to caller and callee (thread) to agree
// what the arguments mean (if anything).
if (this->m_pfDelegate)
this->m_pfDelegate(this->m_pArgs)
}
};

and to use... (psuedocode)

ThreadClosure* pClosure = new ThreadClosure(...);
// This is the key!
ThreadStart* pDelegate = new ThreadDelegate(
pClosure, ThreadClosure::Run);
Thread* pThread = new Thread(pDelegate);
pThread->Start();

Best regards,
Michael Powell
Nov 17 '05 #3

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

Similar topics

0
by: fowlertrainer | last post by:
Hello ! I have been created a Zope (Python) Product. But it is must store inner global definitions (some conversion parameters), what I need to change in only one thread, or only once. See...
31
by: AlexeiOst | last post by:
Everywhere in documentation there are recommendations to use threads from thread pooling for relatively short tasks. As I understand, fetching a page or multiple pages (sometimes up to 50 but not...
2
by: Job Lot | last post by:
How can I pass parameters to a procedure which is called in thread? '! Spin off a new thread. myThread = New Thread(New ThreadStart(AddressOf GetRawFigures)) myThread.IsBackground = True...
8
by: Maurice LING | last post by:
Hi, I just have a simple question about threads. My classes inherits from threading.Thread class. I am calling threading.Thread.run() method to spawn a few threads to parallel some parts of my...
2
by: Carlos | last post by:
Hi all, I am familiar with passing parameters to a thread function using C++, but I needt to learn it using C#. Can someone shed some light on how to do this? Code snippets will be great to show...
4
by: Charles Law | last post by:
Hi guys. I have two threads: a main thread and a background thread. Lots of stuff happens in the background thread that means I have to update several (lots) of controls on a form. It is...
3
by: SatCom | last post by:
Hello, I had originally posted this in the winforms.controls discussion, forgive the double post, Here is where I need help... I have been porting some VB6 to VB2005 and here is the issue with...
2
by: Jerry Spence1 | last post by:
One way of passing data to a thread is to encapsulate the thread inside a class. However, I can't refer to my main form objects from within the class/thread as it says "Reference to a non-shared...
5
by: Code Monkey | last post by:
Is there anyway I can start a get a job done by starting a new thread with MULTIPLE parameters? operations only take a maximum of 1 parameter? I need a way of passing (at minimum) at least 3...
1
by: Chrace | last post by:
Hi all, I have a problem with with Thread.Join( Timeout ) where the timeout never occurs. I basically need to make a connection to an AS400 box which works fine. Once in a blue moon the AS400...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.