PC Review


Reply
Thread Tools Rate Thread

Accessing form components from a thread

 
 
Lilith
Guest
Posts: n/a
 
      9th Aug 2007
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.

--
Lilith
 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      9th Aug 2007
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).


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"Lilith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>
> --
> Lilith



 
Reply With Quote
 
 
 
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      9th Aug 2007
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.

"Lilith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>
> --
> Lilith



 
Reply With Quote
 
Lilith
Guest
Posts: n/a
 
      9th Aug 2007
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


On Thu, 9 Aug 2007 13:58:08 -0400, "Ignacio Machin \( .NET/ C# MVP \)"
<machin TA laceupsolutions.com> wrote:

>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.
>
>"Lilith" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed)...
>> 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.
>>
>> --
>> Lilith

>

 
Reply With Quote
 
Lilith
Guest
Posts: n/a
 
      9th Aug 2007
Thanks very much. I'll have a look.

Lil

On Thu, 9 Aug 2007 13:47:05 -0400, "Nicholas Paldino [.NET/C# MVP]"
<(E-Mail Removed)> wrote:

>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).

 
Reply With Quote
 
Lilith
Guest
Posts: n/a
 
      9th Aug 2007
On Thu, 09 Aug 2007 12:39:48 -0500, Lilith <(E-Mail Removed)> wrote:

>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
 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      10th Aug 2007
Hi,

"Lilith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Thu, 09 Aug 2007 12:39:48 -0500, Lilith <(E-Mail Removed)> wrote:
>
>>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.


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.



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ShowDialog - Cross-thread operation not valid: Control'CheckAccountInfo' accessed from a thread other than the thread it was createdon. Tom C Microsoft C# .NET 9 20th Feb 2008 09:15 PM
Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on Joe Microsoft C# .NET 4 12th Mar 2007 10:59 AM
Thread A calls a delegate on Thread B but Thread A executes it!?!? Paul Tomlinson Microsoft C# .NET 4 3rd Feb 2005 11:09 PM
Components added to a Components Surface not in Components Collection? Microsoft Microsoft Dot NET Framework 2 20th Feb 2004 08:42 PM
Shut down thread - thread closes form, form doesn't close because thread calls it etc... Robin Tucker Microsoft VB .NET 4 17th Oct 2003 12:03 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:06 PM.