By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,191 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,191 IT Pros & Developers. It's quick & easy.

ShowDialog and asynchronous code execution

P: n/a
Greetings!

First, I'm not 100% sure where to post this question. I use VB.NET for
this project, but it's really a design question (a question on which
method to use when solving this problem).

In this medium sized (30 or so forms) application, our users have
requested a more visual notification of when the client (this app) is
communicating with the server, or for other lenghty processes.
Disabling the current form and changing the cursor to
Cursors.WaitCursor is a good start, but we also want to add a small
status form, that shows whenever the client performs a lengthy
operation.

The basic problem is that I want to do StatusForm.ShowDialog() and have
code (in the calling form) execute at the same time. Three approaches
tested so far:

1. Asynchronous execution using BeginInvoke and MethodInvoker.

The status form is invoked using a custom method ShowAndExecute(Owner
as Form, FunctionToExecute as FuncGenericDelegate). FuncGenericDelegate
is an object of the type GenericDelegate, which is defined as a generic
delegate (in the status form), in this case a sub with no parameters.
The method uses BeginInvoke and MethodInvoker to start another
(private) sub which in turn executes the function sent to the method
(using AddressOf <function>) in FunctionToExecute. Right after
BeginInvoke, a ShowDialog() is executed, making the form appear on
screen. In the sub, invoked by BeginInvoke, the last instruction to
execute is Hide(), making the form go away again.

Let's say I invoke the ShowAndExecute method this way:

StatusForm.ShowAndExecute(CurrentForm,AddressOf
FunctionIWantToExecute)

When this method executes, the status form is shown (using ShowDialog,
which ties it to the form from which I'm calling the method),
FunctionIWantToExecute() is executed and when it exits, the status form
is hidden automatically.

This way I can have the code (that should be executed) in the relevant
form (and not in the status form), and still being able to do
ShowDialog() on the status form.

I started to run into problems when my subroutines needed to show error
dialogs (when something went wrong on the server level). When this
happens I want the status form to disappear, but since the Me.Hide() is
executed after the "delegated function" is executed, this can't happen.

I know, it's a bit confusing. I'm not that good at describing this
method. I still get lost a bit when thinking of it. ("Who is executing
what?")

It's something like this post: http://tinyurl.com/4fmpw

(I know what you're thinking, why not show the code? Well, heh, I kinda
went away from this method and started to used method 3 below before I
could check it into sourcesafe.)

2. Multithreading

Couldn't make this work at all. Could have something to do with the
fact that my multithreading experiences in VB.NET are pretty much
nonexistent. I've done multithreaded work in other programming
languages, so I understand the basic concepts.

3. Caving in and using an ordinary Show()

Just do a Show() on the status form and then carry on executing the
code. This way I don't have to separate all server communication in to
small subroutines (like in method 1). The downside is that the status
form is not tied to the form that executes the code (since the status
form is not invoked using ShowDialog). I can disable the calling form
and make the status form TopMost, which pretty much takes care of
everything, but I can still manage to get other applications in between
the calling form and the status form. OK, maybe I should mention that
I'm a bit pedantic. ;)

Phew, are you still reading? ;) Anyway, any comments, sugestions etc
would be appreciated. This is not by any means a showstopper issue, but
it's been bugging me for a week or so now.

TIA!

Best regards,

/dempa

Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Dennis,

I made this sample a week ago, look through it, it uses two methods to show
to seperated splash screens.

(I saw yesterday that the eventhandlers where missing so I sand it today
again to somebody, however that will be not on Google, otherwise had given
you a link on Google, this is for others who can compliain I sent to much
code).

:-)

However showdialog will not work because it direct blocks your mainform
waiting for an answer.

\\\this sample needs on form 1 one button and three textboxes
Private WithEvents frm1 As Form2
Private Delegate Sub Frm1Handler(ByVal message As String)
Private WithEvents frm2 As Form2
Private MyThread As System.Threading.Thread
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim timer1 As New System.Windows.Forms.Timer
AddHandler timer1.Tick, AddressOf mytimer1
TextBox1.Text = "0"
timer1.Enabled = True
timer1.Interval = 400
Dim timer2 As New System.Windows.Forms.Timer
End Sub
Private Sub mytimer1(ByVal sender As Object, _
ByVal e As System.EventArgs)
TextBox1.Text = (CInt(TextBox1.Text) + 1).ToString
DirectCast(sender, System.Windows.Forms.Timer).Enabled = True
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
frm1 = New Form2
frm1.itstop = Me.Top
frm1.itsleft = Me.Left + 200
AddHandler frm1.ready, AddressOf Frm1Ready
frm1.Text = "Extra thread"
MyThread = New System.Threading.Thread(AddressOf frm1.Show)
MyThread.Start()
frm2 = New Form2
frm2.itstop = Me.Top
frm2.itsleft = Me.Left + 400
frm2.Text = "In own thread"
AddHandler frm1.ready, AddressOf Frm2Ready
frm2.Show()
End Sub
Private Sub Frm1Ready(ByVal message As String)
Me.BeginInvoke(New Frm1Handler(AddressOf Frm1HandlerSub), New
Object() {message})
End Sub
Private Sub Frm1HandlerSub(ByVal message As String)
TextBox2.Text = message
frm1.Close()
MyThread.Abort()
End Sub
Private Sub frm2ready(ByVal message As String)
TextBox3.Text = message
frm2.Dispose()
End Sub
///
\\\Needs a form2 with one textbox
Friend Event form2ready(ByVal message As String)
Friend itstop As Integer
Friend itsleft As Integer
Private Sub Form2_Activated(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Activated
Me.Left = itsleft
Me.Top = itstop
Me.BringToFront()
Dim timenext As DateTime = Now.Add(TimeSpan.FromSeconds(10))
Do While timenext > Now
TextBox1.Text = Now.TimeOfDay.ToString
Application.DoEvents() 'to show the time
Threading.Thread.Sleep(50)
Me.Opacity -= 0.004
Loop
RaiseEvent form2ready(Now.TimeOfDay.ToString)
End Sub
Private Sub Form2_Closing(ByVal sender As Object, ByVal _
e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
e.Cancel = True
End Sub
///
I hope this helps a little

Cor
Nov 21 '05 #2

P: n/a
Hi,

The procedure that uses showdialog to display a form will wait for
the form to close before continuing running the code in the procedure. Use
show to display the form.
Ken
-------------------
"Dennis Sjogren" <la********@gmail.com> wrote in message
news:10**********************@c13g2000cwb.googlegr oups.com...
Greetings!

First, I'm not 100% sure where to post this question. I use VB.NET for
this project, but it's really a design question (a question on which
method to use when solving this problem).

In this medium sized (30 or so forms) application, our users have
requested a more visual notification of when the client (this app) is
communicating with the server, or for other lenghty processes.
Disabling the current form and changing the cursor to
Cursors.WaitCursor is a good start, but we also want to add a small
status form, that shows whenever the client performs a lengthy
operation.

The basic problem is that I want to do StatusForm.ShowDialog() and have
code (in the calling form) execute at the same time. Three approaches
tested so far:

1. Asynchronous execution using BeginInvoke and MethodInvoker.

The status form is invoked using a custom method ShowAndExecute(Owner
as Form, FunctionToExecute as FuncGenericDelegate). FuncGenericDelegate
is an object of the type GenericDelegate, which is defined as a generic
delegate (in the status form), in this case a sub with no parameters.
The method uses BeginInvoke and MethodInvoker to start another
(private) sub which in turn executes the function sent to the method
(using AddressOf <function>) in FunctionToExecute. Right after
BeginInvoke, a ShowDialog() is executed, making the form appear on
screen. In the sub, invoked by BeginInvoke, the last instruction to
execute is Hide(), making the form go away again.

Let's say I invoke the ShowAndExecute method this way:

StatusForm.ShowAndExecute(CurrentForm,AddressOf
FunctionIWantToExecute)

When this method executes, the status form is shown (using ShowDialog,
which ties it to the form from which I'm calling the method),
FunctionIWantToExecute() is executed and when it exits, the status form
is hidden automatically.

This way I can have the code (that should be executed) in the relevant
form (and not in the status form), and still being able to do
ShowDialog() on the status form.

I started to run into problems when my subroutines needed to show error
dialogs (when something went wrong on the server level). When this
happens I want the status form to disappear, but since the Me.Hide() is
executed after the "delegated function" is executed, this can't happen.

I know, it's a bit confusing. I'm not that good at describing this
method. I still get lost a bit when thinking of it. ("Who is executing
what?")

It's something like this post: http://tinyurl.com/4fmpw

(I know what you're thinking, why not show the code? Well, heh, I kinda
went away from this method and started to used method 3 below before I
could check it into sourcesafe.)

2. Multithreading

Couldn't make this work at all. Could have something to do with the
fact that my multithreading experiences in VB.NET are pretty much
nonexistent. I've done multithreaded work in other programming
languages, so I understand the basic concepts.

3. Caving in and using an ordinary Show()

Just do a Show() on the status form and then carry on executing the
code. This way I don't have to separate all server communication in to
small subroutines (like in method 1). The downside is that the status
form is not tied to the form that executes the code (since the status
form is not invoked using ShowDialog). I can disable the calling form
and make the status form TopMost, which pretty much takes care of
everything, but I can still manage to get other applications in between
the calling form and the status form. OK, maybe I should mention that
I'm a bit pedantic. ;)

Phew, are you still reading? ;) Anyway, any comments, sugestions etc
would be appreciated. This is not by any means a showstopper issue, but
it's been bugging me for a week or so now.

TIA!

Best regards,

/dempa
Nov 21 '05 #3

P: n/a
Ken Tucker [MVP] wrote:
The procedure that uses showdialog to display a form will wait for
the form to close before continuing running the code in the procedure. Use
show to display the form.


Uhm, yes. I don't want to sound rude, but did you read my post,
especially method 1? That method enables me to use ShowDialog() and
still be able to execute code at the same time (by using delegates,
BeginInvoke and MethodInvoker). However, using this method becomes
difficult when I need to show various information and error dialogs from
within the delegated code. Also, having a generic delegate that takes no
arguments is not optimal in all cases. That's why I switched to method 3
(using Show()). This method has it's own set of shortcomings tho'.

/dempa
Nov 21 '05 #4

P: n/a
Cor,
I made this sample a week ago, look through it, it uses two methods to show
to seperated splash screens.


Looking at your code inspired me to try the multithreaded approach
again. After much gnashing of teeth, I finally have a working solution.
It uses a wrapper class that manages my status windows. It has a Show()
method that creates a new status form in it's own thread. The wrapper
also has a Hide() method which uses Invoke (which should be safe in a
threaded environment) to call the Close method of the status form in
question.

I've used Thread.Name and Debug to verify that no method calls are
beging done over thread boundaries.

This is how I use it:

Private BusyWindow as New frmBusyWindow(Me)
BusyWindow.Show("Searching...")
Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.