473,503 Members | 1,697 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.MyDelegate
del = New Measure.MyDelegate(AddressOf Measure.updateValues)
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 currentValuesChanged()
Private Shared rbnIn1Checked As Boolean = False
Private Shared txtSpeedText As String = ""

'in the OnLoad sub
AddHandler currentValuesChanged, AddressOf HandlerCurrentValuesChanged

'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 currentValuesChanged()
End Sub

'the handler of the event
Private Sub HandlerCurrentValuesChanged()
'...
Me.rbnIn1.Checked = rbnIn1Checked 'this line raises an error
Me.txtSpeed.text = txtSpeedtext 'this doesn't
'...
End Sub
Nov 21 '05 #1
3 1163
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
HandlerCurrentValuesChanged function is trying to access controls on the old
form, which have been disposed.

HTH

Charles
"dast" <da**@discussions.microsoft.com> wrote in message
news:FD**********************************@microsof t.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.MyDelegate
del = New Measure.MyDelegate(AddressOf Measure.updateValues)
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 currentValuesChanged()
Private Shared rbnIn1Checked As Boolean = False
Private Shared txtSpeedText As String = ""

'in the OnLoad sub
AddHandler currentValuesChanged, AddressOf HandlerCurrentValuesChanged

'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 currentValuesChanged()
End Sub

'the handler of the event
Private Sub HandlerCurrentValuesChanged()
'...
Me.rbnIn1.Checked = rbnIn1Checked 'this line raises an error
Me.txtSpeed.text = 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
1012
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"...
2
2265
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...
2
2086
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...
4
3555
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...
1
2521
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,...
0
2429
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...
1
2017
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...
1
14534
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: ...
2
1674
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...
0
7199
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,...
0
7074
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
7273
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,...
1
6982
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
7451
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
4667
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...
0
3161
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...
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
374
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.