473,388 Members | 1,375 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,388 software developers and data experts.

Expected Exception

It's a big taboo to manipulate Windows Form directly from another thread
and I did come across the issue in my actual development. My colleague and
I were talking about it this morning and we wanted to see if we could actually
get the exception to happen. So I did this sample in VB .NET.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Dim t As New Thread(AddressOf Me.MyThreadSub)
t.Start()
End Sub
Private Sub MyThreadSub()
Button1.Text = "Test"
End Sub

Somehow, error is not happening... Now I'm confused..

Nov 21 '05 #1
8 2577
Keep trying ;-)

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Hayato Iriumi" <hi*****@hotmail.com> wrote in message
news:32*********************@news.microsoft.com...
It's a big taboo to manipulate Windows Form directly from another thread
and I did come across the issue in my actual development. My colleague and
I were talking about it this morning and we wanted to see if we could actually get the exception to happen. So I did this sample in VB .NET.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim t As New Thread(AddressOf Me.MyThreadSub)
t.Start()
End Sub
Private Sub MyThreadSub()
Button1.Text = "Test"
End Sub

Somehow, error is not happening... Now I'm confused...

Nov 21 '05 #2
Keep trying ;-)

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Hayato Iriumi" <hi*****@hotmail.com> wrote in message
news:32*********************@news.microsoft.com...
It's a big taboo to manipulate Windows Form directly from another thread
and I did come across the issue in my actual development. My colleague and
I were talking about it this morning and we wanted to see if we could actually get the exception to happen. So I did this sample in VB .NET.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim t As New Thread(AddressOf Me.MyThreadSub)
t.Start()
End Sub
Private Sub MyThreadSub()
Button1.Text = "Test"
End Sub

Somehow, error is not happening... Now I'm confused...

Nov 21 '05 #3
The problem is that forms aren't thread safe from the standpoint of multiple
threads simultaneously writing to a form. You will generate the error much
faster if you create 10 threads and have them all randomly write to your
form. The way to handle this is to pass a message to the form and have it
do it's own update. The message can have as one of it's parameters the
address of the data payload being passed to the form.

Mike Ober.

"Hayato Iriumi" <hi*****@hotmail.com> wrote in message
news:32*********************@news.microsoft.com...
It's a big taboo to manipulate Windows Form directly from another thread
and I did come across the issue in my actual development. My colleague and
I were talking about it this morning and we wanted to see if we could actually get the exception to happen. So I did this sample in VB .NET.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim t As New Thread(AddressOf Me.MyThreadSub)
t.Start()
End Sub
Private Sub MyThreadSub()
Button1.Text = "Test"
End Sub

Somehow, error is not happening... Now I'm confused...

Nov 21 '05 #4
The problem is that forms aren't thread safe from the standpoint of multiple
threads simultaneously writing to a form. You will generate the error much
faster if you create 10 threads and have them all randomly write to your
form. The way to handle this is to pass a message to the form and have it
do it's own update. The message can have as one of it's parameters the
address of the data payload being passed to the form.

Mike Ober.

"Hayato Iriumi" <hi*****@hotmail.com> wrote in message
news:32*********************@news.microsoft.com...
It's a big taboo to manipulate Windows Form directly from another thread
and I did come across the issue in my actual development. My colleague and
I were talking about it this morning and we wanted to see if we could actually get the exception to happen. So I did this sample in VB .NET.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim t As New Thread(AddressOf Me.MyThreadSub)
t.Start()
End Sub
Private Sub MyThreadSub()
Button1.Text = "Test"
End Sub

Somehow, error is not happening... Now I'm confused...

Nov 21 '05 #5
Hello W.G. Ryan eMVP,

I've never tried so hard to get an exception to happen... but I suceeded
in it. :D I got the following exception.

An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll
Additional information: Controls created on one thread cannot be parented
to a control on a different thread.

And this is what I did.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
For i As Integer = 0 To 10
Dim t As New Thread(AddressOf Me.MyThreadSub)
t.Start()
Next
End Sub
Private Sub MyThreadSub()
Dim t As New TextBox
Me.Controls.Add(t)
End Sub

I do remember now that that's exactly what I did in my actual development
and got stuck for a while because I didn't know about that concept. Anyway...
I successfully got an exception to happen! Now I'm happy!

Keep trying ;-)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Hayato Iriumi" <hi*****@hotmail.com> wrote in message
news:32*********************@news.microsoft.com...
It's a big taboo to manipulate Windows Form directly from another
thread and I did come across the issue in my actual development. My
colleague and I were talking about it this morning and we wanted to
see if we could

actually
get the exception to happen. So I did this sample in VB .NET.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs)
Handles Button1.Click
Dim t As New Thread(AddressOf Me.MyThreadSub)
t.Start()
End Sub
Private Sub MyThreadSub()
Button1.Text = "Test"
End Sub
Somehow, error is not happening... Now I'm confused...

Nov 21 '05 #6
Hello W.G. Ryan eMVP,

I've never tried so hard to get an exception to happen... but I suceeded
in it. :D I got the following exception.

An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll
Additional information: Controls created on one thread cannot be parented
to a control on a different thread.

And this is what I did.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
For i As Integer = 0 To 10
Dim t As New Thread(AddressOf Me.MyThreadSub)
t.Start()
Next
End Sub
Private Sub MyThreadSub()
Dim t As New TextBox
Me.Controls.Add(t)
End Sub

I do remember now that that's exactly what I did in my actual development
and got stuck for a while because I didn't know about that concept. Anyway...
I successfully got an exception to happen! Now I'm happy!

Keep trying ;-)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Hayato Iriumi" <hi*****@hotmail.com> wrote in message
news:32*********************@news.microsoft.com...
It's a big taboo to manipulate Windows Form directly from another
thread and I did come across the issue in my actual development. My
colleague and I were talking about it this morning and we wanted to
see if we could

actually
get the exception to happen. So I did this sample in VB .NET.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs)
Handles Button1.Click
Dim t As New Thread(AddressOf Me.MyThreadSub)
t.Start()
End Sub
Private Sub MyThreadSub()
Button1.Text = "Test"
End Sub
Somehow, error is not happening... Now I'm confused...

Nov 21 '05 #7
There is no expected exception. It's just not thread safe.

"Hayato Iriumi" wrote:
It's a big taboo to manipulate Windows Form directly from another thread
and I did come across the issue in my actual development. My colleague and
I were talking about it this morning and we wanted to see if we could actually
get the exception to happen. So I did this sample in VB .NET.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Dim t As New Thread(AddressOf Me.MyThreadSub)
t.Start()
End Sub
Private Sub MyThreadSub()
Button1.Text = "Test"
End Sub

Somehow, error is not happening... Now I'm confused...

Nov 21 '05 #8
Hello Rulin,

Yeah, if it's not thread safe, you expect exception, wouldn't you?
There is no expected exception. It's just not thread safe.

"Hayato Iriumi" wrote:
It's a big taboo to manipulate Windows Form directly from another
thread and I did come across the issue in my actual development. My
colleague and I were talking about it this morning and we wanted to
see if we could actually get the exception to happen. So I did this
sample in VB .NET.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Handles Button1.Click
Dim t As New Thread(AddressOf Me.MyThreadSub)
t.Start()
End Sub
Private Sub MyThreadSub()
Button1.Text = "Test"
End Sub
Somehow, error is not happening... Now I'm confused...

Nov 21 '05 #9

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

Similar topics

2
by: SaCompGeek | last post by:
I have a class that defines a Key/Name value collection that i generated by and XML Element with multiple attributes. The attribute are used to create the collection of 'Binate' objects. This is...
4
by: pei_world | last post by:
I have followed a example from a book exactly, but it seems not working at all. can anyone tell me what is going on? ========= Global.asax.cs ============ public static Entry...
4
by: javatopia | last post by:
Hello, I am trying to show a Crystal Reports 10 Enterprise report in an ASP.NET page (C#). I can run the report via the admin console just fine. When I try to show the report, after setting up...
6
by: ECathell | last post by:
I am having a problem with my try..catch blocks... I push a button on my form. using SQLDMO I verify the status of the server. If it is off I turn it on. I then run a query that requires a...
0
by: Robert Rotstein | last post by:
I have a simple web service which operates correctly. But when I try to add a SoapExtension, it always throws an exception: System.InvalidOperationException: Client found response content type...
4
by: Mark | last post by:
I have written a PocketPC application that connects to a web service, and when im connected via the cable or via wireless then it pulls back the data correctly. However when connected to GPRS, i...
8
by: Karsten Schramm | last post by:
Hi, when I run the following code: using System; namespace ConsoleApplication22 { class Program {
5
by: stevewilliams2004 | last post by:
I was wondering if someone could explain the output I am getting for the program below. What I expected from the main program output was "Cat" but instead I see "Mammal". The output is also...
0
by: =?Utf-8?B?R2VuaXVzIExvZ2ljcw==?= | last post by:
I am a newbie to WSE and I am using WSE 3.0. Scenario is that I have created server side certificate using makecert tool and added it to client side as present in the batch file that comes with the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...
0
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...

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.