473,624 Members | 2,612 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Referring to form objects from inside a thread pool

One way of passing data to a thread is to encapsulate the thread inside a
class. However, I can't refer to my main form objects from within the
class/thread as it says "Reference to a non-shared member requires an Object
reference".

How do I refer to items such as Textbox1.text etc on my main form?

-Jerry
May 17 '06 #1
2 1553
Don't quote me, but I don't think it's good practice to refer to specific
components on a form from a thread. It couples the thread class and the
form too closely together. The best way I've found to do this is to store a
reference to an instance of the form in the thread class and then whenever
you want to signal to the main form to do something, "Invoke" a delegate on
the form. As an example of what this might look like below. We are
basically marshalling a call from the calling thread onto the form thread to
execute some function, passing in parameters. This function can contain
code to make modifications to your forms and/or the controls on them. I
usually have delegates for signalling error conditions, success, failure,
starting and ending an operation, but it all depends on what you are doing
with it.

http://msdn2.microsoft.com/en-us/library/zyzhdc6b.aspx

' Declare one or more delegates

Private Delegate Sub DoSomething_Del egate(...)

' Store a reference to an instance of the form you want to signal

Private m_Form as MainForm
......
......

' At some moment in the thread, invoke to the form.

Invoke_DoSometh ing (...)
.....
.....
' Invoke to the form, passing parameters (if required), via. a delegate.

Public Sub Invoke_DoSometh ing (...)

Dim Parameters(...) As Object

Parameters(0) = ...
Parameters(1) = ...
Parameters(2) = ...
Parameters(3) = ...

....

Try

m_Form.Invoke(N ew DoSomething_Del egate(AddressOf
m_Form.DoSometh ing_Invoked), Parameters)

Catch ex As Exception

' Something went titsup.

End Try

End Sub

' Create a method on the form called "DoSomething_In voked"

Public Sub DoSomething_Inv oked(....)

....
....Modify controls etc.
....

End Sub
"Jerry Spence1" <je**********@s omewhere.com> escribió en el mensaje
news:44******** **************@ ptn-nntp-reader01.plus.n et...
One way of passing data to a thread is to encapsulate the thread inside a
class. However, I can't refer to my main form objects from within the
class/thread as it says "Reference to a non-shared member requires an
Object reference".

How do I refer to items such as Textbox1.text etc on my main form?

-Jerry

May 17 '06 #2
Actually, this is a nice, useful explaination of why/what we are doing with
invocation..... :

http://msdn.microsoft.com/library/de...ms06112002.asp

"Jerry Spence1" <je**********@s omewhere.com> escribió en el mensaje
news:44******** **************@ ptn-nntp-reader01.plus.n et...
One way of passing data to a thread is to encapsulate the thread inside a
class. However, I can't refer to my main form objects from within the
class/thread as it says "Reference to a non-shared member requires an
Object reference".

How do I refer to items such as Textbox1.text etc on my main form?

-Jerry

May 17 '06 #3

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

Similar topics

3
2208
by: David Sworder | last post by:
This message was already cross-posted to C# and ADO.NET, but I forgot to post to this "general" group... sorry about that. It just occured to me after my first post that the "general" group readers might have some thoughts on this perplexing .NET blocking issue. (see below) ===== Hi,
7
2857
by: David Sworder | last post by:
Hi, I'm developing an application that will support several thousand simultaneous connections on the server-side. I'm trying to maximize throughput. The client (WinForms) and server communicate via a socket connection (no remoting, no ASP.NET). The client sends a message to the server that contains some instructions and the server responds in an asynchronous fashion. In other words, the client doesn't block while waiting for the...
3
2792
by: ExclusiveResorts | last post by:
Can the CallContext be used reliably for storing request specific data? We are developing an application library that uses the CallContext to keep an IdentityMap (hashtable of business objects that have been loaded from the DB) and a collection of business objects that have been modified during the current request. These object maps need to be globally visible throughout a request and expire at the end of it. The maps are accessed using...
2
1124
by: Eric Cathell | last post by:
I am creating a custom TCPDatagram object. I am using : mTcp() as tcpClient mNetstream() as NetworkStream below is the actual code with my questions at the end. Public Class TCPDatagram Private mTcp() As TcpClient
14
2012
by: James | last post by:
I am trying to a add a method to a helper class library I built that will fade out the current form. My code is this: public void fade(object currentForm) { int z = 0; for(double i=1.0; i> 0; i-=.1) { currentForm.Opacity = i; while(z<10000)
1
2095
by: baronadav | last post by:
Greetings, My application suppose to identify one web site for a pool of about 100 different websites. i.e at any given time it is connected to only one website (say google.com), however it does not know what is this website (it could be yahoo.com, microsoft.com, etc). Before it does anything else, it must first identify what website it is connected to (i.e if it is google.com, yahoo.com,...) To speed up things, the application uses...
5
3789
by: admin | last post by:
ok This is my main. Pretty much it goes through each category and starts up 4 worker threads that then ask for groups to gether from. My problem is that when the thread gets done it keeps the mysql connections open so I end up with quite a few at the end. Is there a different way that I should do this? class Program { static string categories = { "emulation" , "audio" , "console" , "anime" , "xxx" , "tv" , "pictures" , "video" };
9
18017
by: RvGrah | last post by:
I'm completely new to using background threading, though I have downloaded and run through several samples and understood how they worked. My question is: I have an app whose primary form will almost always lead to the user opening a certain child window that's fairly resource intensive to load. Is it possible to load this form in a backgroundworker and then use the Show method and hide method as necessary? Anyone know of
34
2777
by: Creativ | last post by:
Why does Thread class not support IDisposable? It's creating quite some problem. Namely, it can exhaust the resource and you have not control over it.
0
8246
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8179
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8685
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8490
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6112
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2612
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
1
1796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.