473,385 Members | 2,013 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,385 software developers and data experts.

Threading...

I think I'm doing this wrong :

I have a class with a public shared method such as follows:

public shared sub myFunction
dim frm as new myFrm
dim t as new Threading.Thread(Addressof myFrm.myShowDialog
t.start
end sub

In the frm - the myShowDialog just does a "me.showdialog".

The idea is that this function will display a form, but then return control
back to the calling routine immediately without waiting for the form to be
closed.

It works, but I've had several reports of the form that is displayed
"crashing" as it is being displayed, with random errors (I've not got a
sample) or the whole application just disappearing (without error).

Can anybody assist, either by confirming or otherwise that what I'm doing is
reasonable, or perhaps letting me know a better way.

Regards
Simon

ps Previously posted by mistake to vb.controls group.

--
================================
Simon Verona
Dealer Management Service Ltd
Stewart House
Centurion Business Park
Julian Way
Sheffield
S9 1GD

Tel: 0870 080 2300
Fax: 0870 735 0011

--
================================
Simon Verona
Dealer Management Service Ltd
Stewart House
Centurion Business Park
Julian Way
Sheffield
S9 1GD

Tel: 0870 080 2300
Fax: 0870 735 0011
Jul 17 '06 #1
14 1594
well im kinda curious on why it looks like your attempting to show another
form on a different thread....are you meaning to put the form on another
thread, and if so why?
--
-iwdu15
Jul 17 '06 #2

Simon Verona wrote:
I think I'm doing this wrong :

I have a class with a public shared method such as follows:

public shared sub myFunction
dim frm as new myFrm
dim t as new Threading.Thread(Addressof myFrm.myShowDialog
t.start
end sub

In the frm - the myShowDialog just does a "me.showdialog".

The idea is that this function will display a form, but then return control
back to the calling routine immediately without waiting for the form to be
closed.
If this is what you want, why not just .Show (rather than .ShowDialog)
the form? Anyway...
>
It works, but I've had several reports of the form that is displayed
"crashing" as it is being displayed, with random errors (I've not got a
sample) or the whole application just disappearing (without error).

Can anybody assist, either by confirming or otherwise that what I'm doing is
reasonable, or perhaps letting me know a better way.
I think you are falling foul of the rule whereby UI elements cannot be
safely interacted with from threads other than their 'owning' thread -
that on which they are created. So here the myFrm is created on the
thread that enters this procedure, but then passed to a different
thread to show itself. Thus this second thread will be accessing UI
elements it didn't create -bad.

The fix (if there are deeper reasons why you need to do this sort of
thing) is to move the form creation from before thread-creation to
after it. That is, at the moment you are saying:

Create form
Create thread (giving it the form)
Start thread - thread goes on to show form

Instead you should say

Create thread
Start thread - thread goes on to create form, then show it.

Probably the logical place for this new routine would be a Shared
procedure in myFrm:

Public Shared Sub NewShowDialog
Dim f As New myFrm
f.ShowDialog
End Sub

then replace your current myFunction with

Dim t As New Thread(AddressOf myFrm.NewShowDialog)
t.Start

But please first consider my first question about .Show vs
..ShowDialog...

--
Larry Lard
Replies to group please
When starting a new topic, please mention which version of VB/C# you
are using

Jul 17 '06 #3
Good question...

Maybe if I explain the application....

I have a main form, which displays a list of entries. The application
allows you to click on an entry to open a form to view/update the details of
that entry.

However, I wanted to allow multiple entries to be displayed together.

Also, I wanted to encapsulate the display of the form within a class (it is
used in many parts of the application as well, so I felt it would be
easier).

If I display the form without creating the thread, the form dies as soon as
the subroutine exits back to the main form (IIRC!), so I came up with this
as a workaround. It also seemed to have the affect of making the
application seem to run smoother (though that could just be me). If I don't
create the thread and just use showdialog, then the original form is blocked
until it returns.

I don't know why it should be causing a problem, but if there is a better
way of acheiving the same thing without creating the thread then I'm up for
it!

Regards
Simon

--
================================
Simon Verona
Dealer Management Service Ltd
Stewart House
Centurion Business Park
Julian Way
Sheffield
S9 1GD

Tel: 0870 080 2300
Fax: 0870 735 0011

"iwdu15" <jmmgoalsteratyahoodotcomwrote in message
news:C1**********************************@microsof t.com...
well im kinda curious on why it looks like your attempting to show another
form on a different thread....are you meaning to put the form on another
thread, and if so why?
--
-iwdu15

Jul 17 '06 #4
See my previous reply, I've had problem with the form disappearing when the
subroutine exits... (I'm presuming the object is killed on exit?)

I understand what you are saying about the frm creation and use being on
seperate threads. I must admit to not thinking of having a shared entry on
the form to display itself!

Regards
Simon

--
================================
Simon Verona
Dealer Management Service Ltd
Stewart House
Centurion Business Park
Julian Way
Sheffield
S9 1GD

Tel: 0870 080 2300
Fax: 0870 735 0011

"Larry Lard" <la*******@hotmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
>
Simon Verona wrote:
>I think I'm doing this wrong :

I have a class with a public shared method such as follows:

public shared sub myFunction
dim frm as new myFrm
dim t as new Threading.Thread(Addressof myFrm.myShowDialog
t.start
end sub

In the frm - the myShowDialog just does a "me.showdialog".

The idea is that this function will display a form, but then return
control
back to the calling routine immediately without waiting for the form to
be
closed.

If this is what you want, why not just .Show (rather than .ShowDialog)
the form? Anyway...
>>
It works, but I've had several reports of the form that is displayed
"crashing" as it is being displayed, with random errors (I've not got a
sample) or the whole application just disappearing (without error).

Can anybody assist, either by confirming or otherwise that what I'm doing
is
reasonable, or perhaps letting me know a better way.

I think you are falling foul of the rule whereby UI elements cannot be
safely interacted with from threads other than their 'owning' thread -
that on which they are created. So here the myFrm is created on the
thread that enters this procedure, but then passed to a different
thread to show itself. Thus this second thread will be accessing UI
elements it didn't create -bad.

The fix (if there are deeper reasons why you need to do this sort of
thing) is to move the form creation from before thread-creation to
after it. That is, at the moment you are saying:

Create form
Create thread (giving it the form)
Start thread - thread goes on to show form

Instead you should say

Create thread
Start thread - thread goes on to create form, then show it.

Probably the logical place for this new routine would be a Shared
procedure in myFrm:

Public Shared Sub NewShowDialog
Dim f As New myFrm
f.ShowDialog
End Sub

then replace your current myFunction with

Dim t As New Thread(AddressOf myFrm.NewShowDialog)
t.Start

But please first consider my first question about .Show vs
.ShowDialog...

--
Larry Lard
Replies to group please
When starting a new topic, please mention which version of VB/C# you
are using

Jul 17 '06 #5
if you want to show a new form on a different thread, then the new thread
must then create the form object:

Private Delegate Sub delNewThread

Private Sub btnNewThread_Click(...)

Dim x as New delNewThread(AddressOf CreateNewFormThreadBegin)

Me.Invoke(x)

End Sub

Private Sub CreateNewFormThreadBegin()

Dim frm as New Form2

frm.ShowDialog()

End Sub
that will create a new form on a different thread then show it
--
-iwdu15
Jul 17 '06 #6
well, if you wan to do it that way, then make sure our thread safe. Threads
can only access objects they create, for instance your Form1 cannot access
things on your other thread's form and vice versa, keep that in mind. To
access methods on other threads, use the threads Invoke method, dont call the
method directly. Keeping that in mind, there wouldnt be a need to call
"ShowDialog" and show the form on a new thread....i dont know if that would
cause issues or not, so your best bet would be calling "Show", not
"ShowDialog"
--
-iwdu15
Jul 17 '06 #7
Larry

I had a go at implementing your thought, but of course, the "real" code is
not quite as simple as the example I posted:

The real code passes a variable through to the form when constructing ie

dim frm as new myfrm(myvar)
dim t as new threading.thread(addressof frm.myShowDialog)
t.start

I can't work out how to create a shared function that I can call on another
thread AND pass a parameter!

Regards
Simon

--
================================
Simon Verona
Dealer Management Service Ltd
Stewart House
Centurion Business Park
Julian Way
Sheffield
S9 1GD

Tel: 0870 080 2300
Fax: 0870 735 0011

"Larry Lard" <la*******@hotmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
>
Simon Verona wrote:
>I think I'm doing this wrong :

I have a class with a public shared method such as follows:

public shared sub myFunction
dim frm as new myFrm
dim t as new Threading.Thread(Addressof myFrm.myShowDialog
t.start
end sub

In the frm - the myShowDialog just does a "me.showdialog".

The idea is that this function will display a form, but then return
control
back to the calling routine immediately without waiting for the form to
be
closed.

If this is what you want, why not just .Show (rather than .ShowDialog)
the form? Anyway...
>>
It works, but I've had several reports of the form that is displayed
"crashing" as it is being displayed, with random errors (I've not got a
sample) or the whole application just disappearing (without error).

Can anybody assist, either by confirming or otherwise that what I'm doing
is
reasonable, or perhaps letting me know a better way.

I think you are falling foul of the rule whereby UI elements cannot be
safely interacted with from threads other than their 'owning' thread -
that on which they are created. So here the myFrm is created on the
thread that enters this procedure, but then passed to a different
thread to show itself. Thus this second thread will be accessing UI
elements it didn't create -bad.

The fix (if there are deeper reasons why you need to do this sort of
thing) is to move the form creation from before thread-creation to
after it. That is, at the moment you are saying:

Create form
Create thread (giving it the form)
Start thread - thread goes on to show form

Instead you should say

Create thread
Start thread - thread goes on to create form, then show it.

Probably the logical place for this new routine would be a Shared
procedure in myFrm:

Public Shared Sub NewShowDialog
Dim f As New myFrm
f.ShowDialog
End Sub

then replace your current myFunction with

Dim t As New Thread(AddressOf myFrm.NewShowDialog)
t.Start

But please first consider my first question about .Show vs
.ShowDialog...

--
Larry Lard
Replies to group please
When starting a new topic, please mention which version of VB/C# you
are using

Jul 17 '06 #8
look at my code example above
--
-iwdu15
Jul 17 '06 #9
Thanks for the example...

However, I'm not 100% positive how I would modify your example to pass a
variable to the form.

I presume that I can't set a local private variable (declared outside of ay
subroutines) and then access it from within the CreateNewFormThreadBegin
sub? I'm presuming that can I can't directly pass any variables directly
through to the CreateNewFromThreadBegin (ie modifying it to Private Sub
CreateNewFromThreadBegin(myvar as string) )

Apologies if I've missed the obvious - Can I use the facts that it's 5pm and
very hot to mitigate my stupidity? <G>

Thanks
Simon

--
================================
Simon Verona
Dealer Management Service Ltd
Stewart House
Centurion Business Park
Julian Way
Sheffield
S9 1GD

Tel: 0870 080 2300
Fax: 0870 735 0011

"iwdu15" <jmmgoalsteratyahoodotcomwrote in message
news:D6**********************************@microsof t.com...
look at my code example above
--
-iwdu15

Jul 17 '06 #10
i appologize, i forgot to add the code to create a thread..silly me, heres
the updated code, im working on pass variables now...i forgot how to do it
myself
Private Delegate Sub delNewThreadBegin()
Private Sub NewThreadBeginSub()

Dim frm As New Form1

frm.ShowDialog()

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim thr As New Threading.Thread(AddressOf ThreadStart)

thr.Start()

End Sub

Private Sub ThreadStart()

Dim x As New delNewThreadBegin(AddressOf NewThreadBeginSub)

Me.Invoke(x)

End Sub

--
-iwdu15
Jul 17 '06 #11
i cant figure it out rite now, im a 17 year old at work trying to multi task
and its nto working too well so my code doesnt even make sense to me....il
write the answer when i get home, i have a few examples on my home computer
--
-iwdu15
Jul 17 '06 #12
Simon Verona wrote:
See my previous reply, I've had problem with the form disappearing when the
subroutine exits... (I'm presuming the object is killed on exit?)
I'd be tempted to go back and re-examine that situation, to be honest.
To paint with a *very* broad brush, multi-threading is something you
shouldn't do unless you quite obviously _must_.
>
I understand what you are saying about the frm creation and use being on
seperate threads. I must admit to not thinking of having a shared entry on
the form to display itself!
The thought process there was that the behaviour of <creating a new
myFrm and displaying itclearly 'belongs to' the myFrm class, but not
to any particular instance of it =shared method.

--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Jul 17 '06 #13

Simon Verona wrote:
I think I'm doing this wrong :

I have a class with a public shared method such as follows:

public shared sub myFunction
dim frm as new myFrm
dim t as new Threading.Thread(Addressof myFrm.myShowDialog
t.start
end sub

In the frm - the myShowDialog just does a "me.showdialog".

The idea is that this function will display a form, but then return control
back to the calling routine immediately without waiting for the form to be
closed.

It works, but I've had several reports of the form that is displayed
"crashing" as it is being displayed, with random errors (I've not got a
sample) or the whole application just disappearing (without error).

Can anybody assist, either by confirming or otherwise that what I'm doing is
reasonable, or perhaps letting me know a better way.
Simon...

All of this really depends on your architecture. Is this routine that
is showing the form on a background thread? If it is, that would
explain why the form exits when the routine ends. If it is on a
background thread, there is no message pump and the form will exit when
the routine ends because the thread exits.

Here is three different methods of showing a form:

Option Strict On
Option Explicit On

Imports System
Imports System.Threading
Imports System.Runtime.InteropServices
Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()Private Sub
InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(0, 0)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(328, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Show Form On Current Thread (Form.Show)"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(0, 28)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(328, 23)
Me.Button2.TabIndex = 1
Me.Button2.Text = "Show Form On New Thread (Application.Run)"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(0, 56)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(328, 23)
Me.Button3.TabIndex = 2
Me.Button3.Text = "Show Form On New Thread (ShowDialog)"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(328, 82)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

<DllImport("kernel32")_
Public Shared Function GetCurrentThreadId() As IntPtr

End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim frm As New Form2
frm.Show()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim thrd As New Thread(AddressOf ShowFormAppRun)
thrd.ApartmentState = ApartmentState.STA
thrd.Start()
End Sub

Private Sub ShowFormAppRun()
Dim frm As New Form2
Application.Run(frm)
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click

Dim thrd As New Thread(AddressOf ShowFormDialog)
thrd.ApartmentState = ApartmentState.STA
thrd.Start()

End Sub

Private Sub ShowFormDialog()
Dim frm As New Form2
frm.ShowDialog()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.Text = String.Format("Main Form - Thread ID {0}",
Form1.GetCurrentThreadId())
End Sub
End Class

Public Class Form2
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()Private Sub
InitializeComponent()
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 30)
Me.Name = "Form2"
Me.Text = "Form2"

End Sub

#End Region

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.Text = String.Format("Child Form - Thread ID {0}",
Form1.GetCurrentThreadId())
End Sub
End Class
There are two forms here, so look out if you copy paste the code. If
you don't find this stuff usefull, maybe you can post some code that
demonstrates more of your problem...

--
Tom Shelton [MVP]

Jul 17 '06 #14

Simon Verona wrote:
<snip>
I have a class with a public shared method such as follows:

public shared sub myFunction
dim frm as new myFrm
dim t as new Threading.Thread(Addressof myFrm.myShowDialog
t.start
end sub

In the frm - the myShowDialog just does a "me.showdialog".

The idea is that this function will display a form, but then return control
back to the calling routine immediately without waiting for the form to be
closed.

It works, but I've had several reports of the form that is displayed
"crashing" as it is being displayed, with random errors (I've not got a
sample) or the whole application just disappearing (without error).
<snip>

It's an error to access the method of a class derived from Control
(which is the case of Form) from a thread other than the one where the
object was created. You must use the object's InvokeRequired method to
check if you're being called from another thread and if so
(InvokeRequired returned True), use Invoke to call the desired method.
The 'pattern' is somewhat like this:

'in myForm
'Must have the same signature of MyShowDialog
Delegate Sub MyShowDialogCallback(Param as Integer)
'...
Sub MyShowDialog(Param As Integer)
Static This As MyShowDialogCallback = AddressOf MyShowDialog
If InvokeRequired Then
BeginInvoke(This, New Object() {Param} )
Else
'Do your thing
'blah, blah, blah
ShowDialog
End If
End Sub

HTH.

Regards,

Branco.

Jul 18 '06 #15

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

Similar topics

65
by: Anthony_Barker | last post by:
I have been reading a book about the evolution of the Basic programming language. The author states that Basic - particularly Microsoft's version is full of compromises which crept in along the...
2
by: Egor Bolonev | last post by:
hi all my program terminates with error i dont know why it tells 'TypeError: run() takes exactly 1 argument (10 given)' =program==================== import os, os.path, threading, sys def...
77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
6
by: CK | last post by:
I have the following code in a windows service, when I start the windows service process1 and process2 work fine , but final process (3) doesnt get called. i stop and restart the windows service...
2
by: Vjay77 | last post by:
In this code: Private Sub downloadBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) If Not (Me.downloadUrlTextBox.Text = "") Then Me.outputGroupBox.Enabled = True...
11
by: Paul Sijben | last post by:
I am stumped by the following problem. I have a large multi-threaded server accepting communications on one UDP port (chosen for its supposed speed). I have been profiling the code and found...
17
by: OlafMeding | last post by:
Below are 2 files that isolate the problem. Note, both programs hang (stop responding) with hyper-threading turned on (a BIOS setting), but work as expected with hyper-threading turned off. ...
0
by: kingcrowbar.list | last post by:
Hello Everyone I have been playing a little with pyGTK and threading to come up with simple alert dialog which plays a sound in the background. The need for threading came when in the first...
7
by: Mike P | last post by:
I am trying to write my first program using threading..basically I am moving messages from an Outlook inbox and want to show the user where the process is up to without having to wait until it has...
126
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard:...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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
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
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
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
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...

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.