473,407 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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 1540
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_Delegate(...)

' 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_DoSomething (...)
.....
.....
' Invoke to the form, passing parameters (if required), via. a delegate.

Public Sub Invoke_DoSomething (...)

Dim Parameters(...) As Object

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

....

Try

m_Form.Invoke(New DoSomething_Delegate(AddressOf
m_Form.DoSomething_Invoked), Parameters)

Catch ex As Exception

' Something went titsup.

End Try

End Sub

' Create a method on the form called "DoSomething_Invoked"

Public Sub DoSomething_Invoked(....)

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

End Sub
"Jerry Spence1" <je**********@somewhere.com> escribió en el mensaje
news:44**********************@ptn-nntp-reader01.plus.net...
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**********@somewhere.com> escribió en el mensaje
news:44**********************@ptn-nntp-reader01.plus.net...
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
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...
7
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...
3
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...
2
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 ...
14
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;...
1
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...
5
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...
9
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...
34
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
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
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
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...

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.