473,625 Members | 2,853 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 NullReferenceEx ception.. 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 2104
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_ConnStat us" 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
5988
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
10
4939
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 I'll post the sample code I'm using that is giving me the problems. I'm sure its something I've...
0
1413
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 two objects for sending of these events. The client opens tcp port 0 to receive events: if (ChannelServices.GetChannel("tcp") == null) {
2
2893
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 action was performed and send the data back by raising an event. My question is how do I catch exceptions on the client side that can potentially be thrown while the server is trying to perform an action, since the client used BeginInvoke to...
12
2783
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 when menu items are clicked.Besides these i would like to put a custom status bar. Any error message encountered in any of the webpage will be displayed in the banner. The problem iam encountering is how to access the customer status bar in child...
12
4129
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. However in vb.net you get continual messages (even setting the system modal property). Firstly, are these two assumptions right and if so what is the approved
2
2131
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 database. This works fine. However, I attempt to notify the user when data accesses occur. The way I attempt to accomplish this is by changing the background color of a label on the form the user is looking at. I use red for when the database is...
2
2826
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 invoked on the primary UI thread? Thanks, Jordan
5
2806
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 other thread will modify the list while operations are running. However, I at the end of the critical section, I need to fire an OnChange type of event, publishing the changed list item as part of my custom EventArgs. I want to be certain that...
0
8189
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
8692
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...
1
8354
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8497
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
6116
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
4089
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...
0
4192
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2621
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

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.