473,569 Members | 2,573 Online
Bytes | Software Development & Data Engineering Community
+ 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 2133
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.e duwrote in message
news:b3******** *************** *********@4ax.c om...
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.e duwrote 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
synchronizatio n. 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.e duwrote:
>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
synchronizatio n. 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.e duwrote in message
news:83******** *************** *********@4ax.c om...
On Thu, 09 Aug 2007 12:39:48 -0500, Lilith <li****@dcccd.e duwrote:
>>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
synchronizati on. 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
6407
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 have a function which simply loops infinately that is part of a form, what happens when a form event (eg button click) occurs? will the event handler...
10
4931
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. My problem is that I must not be doing it right because only some of the stuff works as would be expected. I'll post what exactly is happening, then...
3
2013
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 handles are stored in session variables. This works fine without a problem. Ported it to ASPX, accessing the same DCOM server from code behind...
4
1275
by: JackRazz | last post by:
Is there a way to get a components parent form from within the component? Thanks - JackRazz
5
1939
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
1548
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 following code to connect with database: Imports ByteFX.Data.MySqlClient
0
12062
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 likelihood of CRM success from less than 20 percent to 60 percent. WHITEPAPER :
4
1452
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
2258
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 SetCursorCallBack(Cursor cursor); // - Checks to see if the current thread is the creator of the current Form. If the form was // created on another thread, call...
0
7701
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8130
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7677
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.