Accessing form components from a thread

L

Lilith

This was easy in C++ (well, relatively.) Now I'm in the C#
environment and everything I try doesn't work.

I'm running a thread in which I have two accesses I need to components
on a form. In one instance it's to simply check the text of a button
as an indicator of whether I've got a request to end the thread. This
is a read-only situation so I don't have to worry about
synchronization. The other is a need to write status information to
either a text box or a label. The thread is the only thing that
writes to this field so, again, no conflict.

I've tried with a static method within the form itself and with a
non-static method contained in a class object. I've tried accessors,
with which I'm not fully comfortable yet and might not know how or
where to declare it (I get an "already contains definition..." error.
I've tried fully qualified paths to the text but get an error message
that I need an object reference for the non-static field.

Please advise.
 
N

Nicholas Paldino [.NET/C# MVP]

Lilith,

It might have been easy in C++, but I wonder if you are doing it
correctly. For windows controls (any language which uses the Windows API),
you have to access controls (read or writing) on the thread that created the
control. You are doing work on another thread, so you have to marshal the
call back to the UI thread.

In order to do this, you will need to call the Invoke method on a
control that was created on the UI thread, passing a delegate which will
perform the operation you want (in this case, reading the value, or writing
the value).
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I would like to know how you just to do it in C++ that worked.
Since all the windows versions the UI can only be accessed from the main
(the UI) thread only.

In .NET if you want to modify the interface from another thread you have to
use Control.Invoke. For this you need to have a reference to one of the
controls (IT DOES NOT MATTER WHICH). but Invoke will assure that the method
is executed in the UI thread.
 
L

Lilith

In C++ I was able to pass 'this' to the thread function through the
AfxBeginThread call. This gave me access, through the pointer, to the
control. I could also pass messages through, IIRC, the postmessage
and sendmessage calls.

I'll have a look at Invoke and see what I can divine.

Thanks,
Lil
 
L

Lilith

This was easy in C++ (well, relatively.) Now I'm in the C#
environment and everything I try doesn't work.

I'm running a thread in which I have two accesses I need to components
on a form. In one instance it's to simply check the text of a button
as an indicator of whether I've got a request to end the thread. This
is a read-only situation so I don't have to worry about
synchronization. The other is a need to write status information to
either a text box or a label. The thread is the only thing that
writes to this field so, again, no conflict.

I've tried with a static method within the form itself and with a
non-static method contained in a class object. I've tried accessors,
with which I'm not fully comfortable yet and might not know how or
where to declare it (I get an "already contains definition..." error.
I've tried fully qualified paths to the text but get an error message
that I need an object reference for the non-static field.

Please advise.

Thanks to you both for the advice. I had to look it up and do a bit
of copying, but I think I've got the jist of what's going on. If you
can verify that I understand what happens I think I can move on.

In essence a delegate is set up using the footprint of the method that
needs to be called back. Another method is called that creates an
instance of the callback method and pass it, along with suitable
arguments, to the Invoke function, which then invokes the callback
method (with arguments) and, since it becomes part of the UI thread
there's no conflict.

Basically correct? :-/

Thanks again,
Lil
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Lilith said:
Thanks to you both for the advice. I had to look it up and do a bit
of copying, but I think I've got the jist of what's going on. If you
can verify that I understand what happens I think I can move on.

In essence a delegate is set up using the footprint of the method that
needs to be called back.

A delegate is like a function pointer in C++, only that it has a defined
signature. In this way the compiler can check for the correct signature.
But note a fine difference, a delegate is a type declaration, like a class
or a struct.
Another method is called that creates an
instance of the callback method and pass it, along with suitable
arguments, to the Invoke function,

No exactly, An instance of that type is created. The type is the delegate.
You can say that the constructor require the name of the method it will
call.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top