473,565 Members | 2,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Some controls seem disposed when using delegates ... ?

Hi,

I'm using a delegate to edit some controls on formA from another thread on
formB.

This works nicely the first time I run formA. But if I close formA and
reopen it I get errors.

When changing the checked state of a radiobutton I get an error saying that
I cannot access the control since it is disposed. But I can still change the
text in a textbox without errors. The textboxes doesn't seem disposed to the
other thread, but the radiobuttons do.

Why this strange behaviour?
I'd be really thankfull for any help on this.

Best regards
Daniel

ps. below are the parts of my code that edit the controls. ds.
'these lines are in the other thread
Dim del As Measure.MyDeleg ate
del = New Measure.MyDeleg ate(AddressOf Measure.updateV alues)
Dim xh As Object = Me.Handle
Me.BeginInvoke( del, currentValues) 'currentValues is an array of Objects

'in the declarations of Measure
Public Delegate Sub MyDelegate( ... ByVal value8 As Boolean, ByVal value9 As
String, ... )
Private Shared Event currentValuesCh anged()
Private Shared rbnIn1Checked As Boolean = False
Private Shared txtSpeedText As String = ""

'in the OnLoad sub
AddHandler currentValuesCh anged, AddressOf HandlerCurrentV aluesChanged

'the sub that is executed using the delegate
Public Shared Sub updateValues( ... ByVal value8 As Boolean, ByVal value9 as
String, ... )
'...
rbnIn1Checked = value8
txtSpeedtext = value9
'...
RaiseEvent currentValuesCh anged()
End Sub

'the handler of the event
Private Sub HandlerCurrentV aluesChanged()
'...
Me.rbnIn1.Check ed = rbnIn1Checked 'this line raises an error
Me.txtSpeed.tex t = txtSpeedtext 'this doesn't
'...
End Sub
Nov 21 '05 #1
3 1172
Hi Daniel

I suspect that the problem is that you have an AddHandler call when you load
the form, but do not call RemoveHandler when the form closes. Therefore, the
HandlerCurrentV aluesChanged function is trying to access controls on the old
form, which have been disposed.

HTH

Charles
"dast" <da**@discussio ns.microsoft.co m> wrote in message
news:FD******** *************** ***********@mic rosoft.com...
Hi,

I'm using a delegate to edit some controls on formA from another thread on
formB.

This works nicely the first time I run formA. But if I close formA and
reopen it I get errors.

When changing the checked state of a radiobutton I get an error saying
that
I cannot access the control since it is disposed. But I can still change
the
text in a textbox without errors. The textboxes doesn't seem disposed to
the
other thread, but the radiobuttons do.

Why this strange behaviour?
I'd be really thankfull for any help on this.

Best regards
Daniel

ps. below are the parts of my code that edit the controls. ds.
'these lines are in the other thread
Dim del As Measure.MyDeleg ate
del = New Measure.MyDeleg ate(AddressOf Measure.updateV alues)
Dim xh As Object = Me.Handle
Me.BeginInvoke( del, currentValues) 'currentValues is an array of Objects

'in the declarations of Measure
Public Delegate Sub MyDelegate( ... ByVal value8 As Boolean, ByVal value9
As
String, ... )
Private Shared Event currentValuesCh anged()
Private Shared rbnIn1Checked As Boolean = False
Private Shared txtSpeedText As String = ""

'in the OnLoad sub
AddHandler currentValuesCh anged, AddressOf HandlerCurrentV aluesChanged

'the sub that is executed using the delegate
Public Shared Sub updateValues( ... ByVal value8 As Boolean, ByVal value9
as
String, ... )
'...
rbnIn1Checked = value8
txtSpeedtext = value9
'...
RaiseEvent currentValuesCh anged()
End Sub

'the handler of the event
Private Sub HandlerCurrentV aluesChanged()
'...
Me.rbnIn1.Check ed = rbnIn1Checked 'this line raises an error
Me.txtSpeed.tex t = txtSpeedtext 'this doesn't
'...
End Sub

Nov 21 '05 #2
Thank you Charles!

It seems that was it. It's working fine now.
I wasn't aware that I needed a RemoveHandler. Thank you very much for
teaching me that.

The very best of regards
/Daniel
Nov 21 '05 #3
I'm using a delegate to edit some controls on formA from another thread on
formB.


You should only manipulate controls from the UI thread they were
created on, not from background threads.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #4

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

Similar topics

0
1019
by: celeluck | last post by:
Could any one tell me when the controls and components are disposed? The IDE generates a "components" field, but only a few components are added into that container. And only the "components" field is disposed by the IDE generated code. Those components use system resource, don't they?
2
2270
by: cm | last post by:
HI, here's my problem; My application loads up 3 forms at startup. Deppending on what button is press on form1 i want to show or hide controls on form2. I cant seem to get access to the controls on from2 to hide them unless i create another instance from within form1 which i dont want to do. i just want to use the one creted when the...
2
2100
by: Tim Marsden | last post by:
Hi, This is what I am doing, please comment if this is the correct way. I need to add controls to a form dynamically. Within the Page_Load event (is not Postback) I run a routine to create the controls, and populate the dropdowns etc, I use addhandler to define delegates to capture events raised by the controls. I store these controls...
4
3560
by: Charles Law | last post by:
Hi guys. I have two threads: a main thread and a background thread. Lots of stuff happens in the background thread that means I have to update several (lots) of controls on a form. It is quite tiresome to have to write code to call MyControl.Invoke for each control on the form, along with the delegates that are required for each. Is...
1
2525
by: osmarjunior | last post by:
How to ensure that a form is disposed? When I work with modal forms I do the following: using (Form1 frm = new Form1()) { frm.ShowDialog(); } As far as I read, when it ends the using scope, the form is disposed. But if I don't use modal forms, like this:
0
2444
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
I read from a serialport using a worker thread. Because the worker thread t does not loop often, I cannot wait to terminate the worker thread using a boolean in the While condition. So I have a StopReader() method that simply aborts the worker thread (is there a better way for the above situation?). The StopReader creates an...
1
2024
by: Martin | last post by:
Hi all ! I use Visual Studio .NET 2005 SP1 (+ Updates for Vista) on Windows Vista. I have a strange problem in a WinForm application. In a UserControl, which inherit another UserControl (simply contains a SplitterPanel), I've added controls (most of them are TextBox with the property Anchors set to Left and Right) to Panel2. All theses...
1
14542
by: sklett | last post by:
I've read several articles/blogs/threads about anchoring/docking child controls in a FlowLayoutPanel. It sounds like it *should* work, but I can't get it to work for the life of me. Jon Skeet: I've created a short and complete sample to illustrate the problem. Beat you to it! :0) I would really appreciate if anyone could take a look...
2
1680
by: richard.martino | last post by:
Windows Experts: 1. I have a desktop application with a UserControl that contains controls: TextBoxes, RadioButtons, CheckBoxes and NumericUpDowns. 2. I want to warn the user that, if there is unsaved data within my UserControl when he tries to 'X' out of the entire application, to display a MessageBox. 3. Unfortunately,...
0
7584
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...
0
7888
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
8108
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
7644
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
7951
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...
1
5484
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...
0
3643
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...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
925
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.