473,468 Members | 1,332 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Accessing form components from a thread

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
Aug 9 '07 #1
4 2124
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" <li****@dcccd.eduwrote in message
news:b3********************************@4ax.com...
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

Aug 9 '07 #2
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.comwrote:
>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" <li****@dcccd.eduwrote in message
news:b3********************************@4ax.com.. .
>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
Aug 9 '07 #3
On Thu, 09 Aug 2007 12:39:48 -0500, Lilith <li****@dcccd.eduwrote:
>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
Aug 9 '07 #4
Hi,

"Lilith" <li****@dcccd.eduwrote in message
news:83********************************@4ax.com...
On Thu, 09 Aug 2007 12:39:48 -0500, Lilith <li****@dcccd.eduwrote:
>>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.

Aug 10 '07 #5

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

Similar topics

6
by: Joseph | last post by:
hi 1) i plan on having an awt canvas component (to draw graphs) on a JFrame with other swing components..will this be okay? i've read that swing and awt aren't compatible.. 2)Also, if i...
10
by: Drakier Dominaeus | last post by:
This is my first time posting here, so please forgive me if I do anything incorrectly. I've been learning C# and working with different things and decided I wanted to get into Multi-Threading....
3
by: Alex | last post by:
I'm having a problem porting an ASP solution to ASPX. In the ASP solution I'm accessing a DCOM server, create sub DCOM objects and call functions from VB script on the ASP pages. The DCOM object...
4
by: JackRazz | last post by:
Is there a way to get a components parent form from within the component? Thanks - JackRazz
5
by: RSH | last post by:
I havent been able to set a property from another class with out getting some sort of error. Can someone please tell me what I'm doing wrong here? Public Class Form1
1
by: Miku | last post by:
Hi Guies, I am new to vb.net. In my project I am using vb.net & MySql 4.0.17 as a backend. For database connectivity i have downloaded ByteFX - Mysql .net native provider. I have written the...
0
by: sonu | last post by:
I have following client side code which i have used in my asp.net project SummaryFeatured Resources from the IBM Business Values Solution Center WHITEPAPER : CRM Done Right Improve the...
4
by: Frank Rizzo | last post by:
Hello, I know that it's a bad thing to update UI components on a background thread. But is it fairly safe to access them? I want to iterate through a tree list. Thanks.
5
by: Mythran | last post by:
I've read some documentation about accessing objects created in other threads and the code below is similar to what I've gathered for what I have to do: private delegate void...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.