473,499 Members | 1,672 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Handling events of threads from a different class's object

89 New Member
Question details: VB .NET / threads / events / GUI

Imagine the following situation:

A method from object "A" creates "n" threads.

Variables from these threads contains values that should be updated to a form (only when this form exists), one form can be shown/created for each thread. Thought the forms might not exist, the threads will be always running.

I dont want to control the components (eg. labels) of the form from the threads because if I do so, they must exist otherwise we get an NullReferenceException.. and also dont want to create them all and leave them "invisible" because this would be a waste of memory (imagine if we have 20 forms). Because of this (we dont know when the form will be created), we cant invoke its components also.

My idea was to create an "in-the-middle" object with all variables (that must be updated in the form) being changed from the threads , and make this object raise an event when there is a change, so that an event handler in the form's code will update these values to the form's labels/controls. What do think? so maybe I wouldnt need the threads handling all the controls directly.

The forms are in a separate class, then how do you create an event handler of these threads that you dont even know the names (because it was instanciated in another class)? or how do you manipulate this object from outside (once we dont have the reference to it there)?

As Microsoft says:
A delegate is an object that you can use to call a method of another object
.. but what about getting variables or thread's variables?

thank you for your attention.
Aug 31 '07 #1
9 2087
kenobewan
4,871 Recognized Expert Specialist
To be honest, you asked, I think that this is a poor design. I wonder if your thread is going to chew up more memory then serving up the controls as the forms are requested. What thread are you going to keep running? I dont believe that this was how threads were intended to be used.
Aug 31 '07 #2
thiago777
89 New Member
To be honest, you asked, I think that this is a poor design. I wonder if your thread is going to chew up more memory then serving up the controls as the forms are requested. What thread are you going to keep running? I dont believe that this was how threads were intended to be used.
that was a better response that I was expecting =)

Im aware of this, but I dont find a good way to keep my forms updated in "real time" when my processes are running as threads, and especially when I dont know the exact number of threads that will be necessary (they will be created according to a configuration file), just as the forms should only "consume memory" when a user requests its info about the running threads.

Probably this is due my lack of experience on programming, I would be glad if you could help me in some way.

thank you,
Thiago.
Aug 31 '07 #3
thiago777
89 New Member
I dont believe that this was how threads were intended to be used.
How do you believe it is then?
Sep 3 '07 #4
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
I wouldnt use threads...
this is how i interpret your situation...
forexample you have 3 forms open. (f1,f2,f3)
values in f1 is changed, and you want to reload f2 and f3.

If this is the situation....I would approach it in a different way
1. all forms cal only have one instance open...So create a static class with all the forms as static variables, and if ther set to null, open/instantiate them (if required)
2. Create some public events which are to fired in each othe forms to be updated.
3. create public methods in the static class which will act as a trigger to update the forms
4. when the value in f1 changes, use the method in the static (trigger of step 3) class to update the forms.

Thats how I have done some of my applications. works well.
Sep 3 '07 #5
thiago777
89 New Member
The problem is not the form running as a thread, but the interaction between my threads AND the forms.
Sep 5 '07 #6
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
The problem is not the form running as a thread, but the interaction between my threads AND the forms.
Yes, so if your static class stores all the instances of your forms, your thresds or any other object can access it !!!
Sep 5 '07 #7
thiago777
89 New Member
Yes, so if your static class stores all the instances of your forms, your thresds or any other object can access it !!!
I cant store all instances of my forms, once they should be created dynamically (must discover how many will be necessary during run-time)
Sep 5 '07 #8
r035198x
13,262 MVP
I suggest you spend time reading about delegates *again* and then try your design again.
Sep 5 '07 #9
thiago777
89 New Member
I suggest you spend time reading about delegates *again* and then try your design again.
Im trying a simpler way with event driven GUI's.

What I have is an object that raises some events. Then I'm trying to get these events from the form like this:

1-On the form's class constructor get a reference of the object.
2-Create some AddHandlers for these events
3-Create the methods that will be called from the event handler's to update the form's components info.

This is the code of the form:

Expand|Select|Wrap|Line Numbers
  1. Public Class ThreadsOverview
  2.    'Class responsible for handling events 
  3.    'from referenced object and displaying
  4.    'given info in the this form
  5.    Public ObjectReference As Object
  6.  
  7.    Private Sub New(ByRef ConnObjRef As Object)
  8.  
  9.       ' Dieser Aufruf ist für den Windows Form-Designer erforderlich.
  10.       InitializeComponent()
  11.  
  12.       ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
  13.  
  14.       ObjectReference= ConnObjRef
  15.  
  16.    End Sub
  17.  
  18.    Private Sub ThreadsOverview_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  19.       If Me.Visible = True Then
  20.          AddHandler ObjectReference.E_ConnStatus(sts as boolean) , AddressOf Me.ConnStatusChange
  21.  
  22.       End If
  23.    End Sub
  24. End Class
  25.  
The "E_ConnStatus" is the name of the event from the object.

The problem is Im stuck in the first step I described, this is the code giving the error:

Expand|Select|Wrap|Line Numbers
  1. AddHandler ObjectReference.E_ConnStatus(sts as boolean) , AddressOf Me.ConnStatusChange
Of course, the error is : "The AddHandler or statement event operand RemoveHandler must be a point-qualified expression or a simple name." ,
because it doesnt know the object. I never worked with "as Object" before, so Im not sure if this is the way to go.

How to fix this?
Sep 6 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

7
5962
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
10
4922
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....
0
1402
by: Efim | last post by:
Hi, I have got some problem with sending of events in .NET. I am using remouting. The client has got 2 objects for receiving different types of events (responses and events) The server has got...
2
2884
by: Natalia DeBow | last post by:
Hi there, I am working on an client-server app, where the client asynchronously issues a request for the server to perform some action and the server is supposed to notify the client when the...
12
2760
by: scsharma | last post by:
Hi, I am working on creating a webapplication and my design calls for creating main webform which will have menu bar on left hand side and a IFrame which will contain all the forms that are shown...
12
4102
by: Jack Russell | last post by:
My unstanding of all VB up to and including vb6 is that an event could not "interrupt" itself. For instance if you had a timer event containing a msgbox then you would only get one message. ...
2
2116
by: Paul E. Orman | last post by:
I have a piece of VB code (.NET 1.1 - VB 2003) that loads data from a database through a timer. So the timer is setup and from it I call the procedure that loads the latest records from the...
2
2805
by: Jordan | last post by:
I need to handle UI events in a worker thread instead of the primary UI thread. In C#, is the normal UI event handling behavior to run in a context thread on the thread pool or are events always...
5
2779
by: =?Utf-8?B?VmFubmk=?= | last post by:
Hi, I have a component where I need to have thread-safe access to a list. Operations on the list are done in critical sections (lock (lockObject) { ... } ) in the usual way, to make sure that no...
0
7007
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
7174
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
7220
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...
1
6894
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...
0
5470
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
4919
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
3099
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
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
297
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...

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.