473,399 Members | 3,603 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,399 software developers and data experts.

Delegates with out parameters not marshalling right

In a winform application, I use worker threads to perform asynchronous tasks
(e.g., mail merge with Word). The thread procs need to gather some user
options from controls in the UI, and I know that must be done using a
delegate that's running on the main UI thread. (Can't safely access a
window from a thread different than the one that created the window.)

Rather than write and execute 7 separate methods to get things like checkbox
and radio button values, I wrote two methods, one of which accepts 3 'out'
parameters and the other accepts 4 'out' parameters. Thus, I can pass the
parameters via the delegate invocation, and upon return from it, they should
contain my values from the UI. Trouble is, they don't.

My first indication that something was wrong was when the compiler insisted
that my parameter variables be initialized before invoking the delegate.
Since both the delegate and the method itself adorn those parameters with
the 'out' keyword, the compiler should not have required that. In fact,
that's the only real difference between an 'out' parameter and a 'ref'
parameter.

My second clue was when, upon return from the invocation, the initial values
were still in those variables, despite my having seen them reassigned when I
traced through the method call. It appears that marshalling is being done
only in the IN direction, but not the OUT direction.

Has anyone else here come up against this issue? If so, what did you end up
doing? Thanks!
Nov 15 '05 #1
3 2138
Jerry Houston wrote:
In a winform application, I use worker threads to perform asynchronous tasks
(e.g., mail merge with Word). The thread procs need to gather some user
options from controls in the UI, and I know that must be done using a
delegate that's running on the main UI thread. (Can't safely access a
window from a thread different than the one that created the window.)


A piece of code would help.
Does cross-thread call cause marshalling at all???
In old dark times of MFC I used messages in need of cross-thread data
exchange.

Vadim Chekan.

Nov 15 '05 #2
"Jerry Houston" <JH******@AMSWorld.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Has anyone else here come up against this issue? If so, what did you end up doing? Thanks!


Jerry,

I haven't tried to use out parameters in a delegate but I can offer an
alternative suggestion, you could use a class to store the parameters
similiar to the CancelEventArgs class.

hth
andrew
Nov 15 '05 #3
Jerry Houston wrote:
In a winform application, I use worker threads to perform
asynchronous tasks (e.g., mail merge with Word). The thread procs
need to gather some user options from controls in the UI, and I know
that must be done using a delegate that's running on the main UI
thread. (Can't safely access a window from a thread different than
the one that created the window.)
Right. You have to call Form.Invoke and pass a delegate to the method that
you want the form to run on the UI thread.
Rather than write and execute 7 separate methods to get things like
checkbox and radio button values, I wrote two methods, one of which
accepts 3 'out' parameters and the other accepts 4 'out' parameters.
Thus, I can pass the parameters via the delegate invocation, and upon
return from it, they should contain my values from the UI. Trouble
is, they don't.
This is where I lose you. Invoke looks like one of these two

public object Invoke(Delegate method);

the docs says that method should be an instance of MethodInvoker or *any
delegate thattakes a void parameter*

the other version is:

public virtual object Invoke(Delegate method, object[] args);

the docs say: args is an array to pass as arguments to the specified method.

Note that both use a mehod called MarshaledInvoke, which calls
InvokeMarshalledCallback. The details of the delegate and parameters are
passed through a Control.ThreadMethodEntry field which is accessed by
InvokeMarshalledCallback. In effect the code looks like this (for Invoke
declared above)

if (method is MethodInvoker)
{
MethodInvoker i = method as MethodInvoker;
i.Invoke();
}
else
{
retVal = method.DynamicInvoke(args);
}

Note that DynamicInvoke takes an array of object, and this is *not* out or
ref. The only thing that is returned is the return value of the delegate
(retVal) which is returned from MarshaledInvoke to Invoke.
My second clue was when, upon return from the invocation, the initial
values were still in those variables, despite my having seen them
reassigned when I traced through the method call. It appears that
marshalling is being done only in the IN direction, but not the OUT
direction.
Yes
Has anyone else here come up against this issue? If so, what did you
end up doing? Thanks!


Use a delegate that returns a value. If you want multiple values, create a
class with multiple fields:

class Data
{
public string one;
public string two;
}

object GetData()
{
Data data = new Data();
data.one = this.txtOne.Text;
data.two = this.txtTwo.Text;
return data;
}

void ClickMyButton(object sender, EventArgs e)
{
Data data = this.Invoke(new MethodInvoker(GetData));
// access the values here.
}

Richard
--
my email ev******@zicf.bet is encrypted with ROT13 (www.rot13.org)
Nov 15 '05 #4

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

Similar topics

1
by: Natalia DeBow | last post by:
Hi, I am working on a Windows-based client-server application. I am involved in the development of the remote client modules. I am using asynchronous delegates to obtain information from...
12
by: Grant | last post by:
I am having great difficulty understanding this and any code samples I find online are kilometres long and complicated to understand...Please could someone give me a simple exampe of how to get a...
14
by: Lior Amar | last post by:
Quick question about threads and delegates. I have the following scenario Thread A (CLASSA) spawns Thread B (CLASSB) and passes it a DelegateA to a callback Thread B Invokes a DelegateB...
12
by: ichor | last post by:
hi i cant seem to understand the difference. can anyone explain or point me to some tutorial that can. thanks
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
2
by: Bob Johnson | last post by:
It appears to me that the only things that differentiates any delegates is (1) the type name, and (2) the signature (return type or void, and parameters). Is this true? Take, for instance, the...
7
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
2
by: calenlas | last post by:
Hi all, I'm taking my first steps into C# <--C++ DLL Interop and unfortunately I've run into (what seems to be) a very complicated case as my first task. Perhaps someone here can help me. I...
10
by: Roger Frost | last post by:
Since we are currently on the subject of multi-threading in a different thread, I have a question about delegates. When I use delegates, I just create one for each different parameter set that I...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.