D
Dennis Sjogren
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
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?

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