473,795 Members | 3,231 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ShowDialog and asynchronous code execution

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.WaitCur sor 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.Show Dialog() 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, FunctionToExecu te as FuncGenericDele gate). FuncGenericDele gate
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 FunctionToExecu te. 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.Show AndExecute(Curr entForm,Address Of
FunctionIWantTo Execute)

When this method executes, the status form is shown (using ShowDialog,
which ties it to the form from which I'm calling the method),
FunctionIWantTo Execute() 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
4 4394
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(ByV al message As String)
Private WithEvents frm2 As Form2
Private MyThread As System.Threadin g.Thread
Private Sub Form1_Load(ByVa l sender As Object, _
ByVal e As System.EventArg s) 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.EventArg s)
TextBox1.Text = (CInt(TextBox1. Text) + 1).ToString
DirectCast(send er, System.Windows. Forms.Timer).En abled = True
End Sub
Private Sub Button1_Click(B yVal sender As System.Object, _
ByVal e As System.EventArg s) 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.Threadin g.Thread(Addres sOf 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(Add ressOf 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(ByVa l message As String)
Friend itstop As Integer
Friend itsleft As Integer
Private Sub Form2_Activated (ByVal sender As Object, _
ByVal e As System.EventArg s) Handles MyBase.Activate d
Me.Left = itsleft
Me.Top = itstop
Me.BringToFront ()
Dim timenext As DateTime = Now.Add(TimeSpa n.FromSeconds(1 0))
Do While timenext > Now
TextBox1.Text = Now.TimeOfDay.T oString
Application.DoE vents() 'to show the time
Threading.Threa d.Sleep(50)
Me.Opacity -= 0.004
Loop
RaiseEvent form2ready(Now. TimeOfDay.ToStr ing)
End Sub
Private Sub Form2_Closing(B yVal sender As Object, ByVal _
e As System.Componen tModel.CancelEv entArgs) Handles MyBase.Closing
e.Cancel = True
End Sub
///
I hope this helps a little

Cor
Nov 21 '05 #2
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********@gma il.com> wrote in message
news:10******** **************@ c13g2000cwb.goo glegroups.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.WaitCur sor 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.Show Dialog() 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, FunctionToExecu te as FuncGenericDele gate). FuncGenericDele gate
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 FunctionToExecu te. 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.Show AndExecute(Curr entForm,Address Of
FunctionIWantTo Execute)

When this method executes, the status form is shown (using ShowDialog,
which ties it to the form from which I'm calling the method),
FunctionIWantTo Execute() 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
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
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(M e)
BusyWindow.Show ("Searching..." )
Nov 21 '05 #5

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

Similar topics

5
4825
by: Marty McDonald | last post by:
I create and start several threads, each thread executes the same method - within the method, a web service is invoked. I find that the more threads I use, the longer it takes for all of the threads to finish. The threads are asynchronous, correct? And I thought each would be able to use it's own version of the web service. Am I wrong, does the web service force only one thread at a time to execute? Here is code if interested... I'm...
6
15975
by: Sharon | last post by:
How to catch an asynchronous delegate invocation exception? The code: --------------------------------------- String message = “my message”; foreach( MyDelegate handler in m_MyDelegate.GetInvocationList() ) { try { handler.BeginInvoke(message, null, null);
1
19074
by: NanoWizard | last post by:
Enclosed below is a class that contains one member item called _frm. It is just a standard System.Windows.Forms.Form class defined elsewhere (just disregard the definition of the object). My question is if I want to start a System.Windows.Forms.Form via ShowDialog in another thread what is the best way to do it? Basically, I want to start a form, then continue executing elsewhere and occasionally posting data to the form. I know you...
0
1034
by: Carly | last post by:
Hi, I am not sure I understand what is the meaning of "Execution of asynchronous code is not supported in Windows Forms threading model" - since I am able to create threads and run asynchronous code if I create a Windows form application. Also what would be the diferrence between the threading model of Windows forms and ASP.NET. Please enlighten.
5
2304
by: Ryan Liu | last post by:
Hi, I read Microsoft SDK, ms-help://MS.NETFrameworkSDKv1.1/cpguidenf/html/cpovrasynchronousprogramming overview.htm there are 4 ways to call EndInvoke: The code in this topic demonstrates four common ways to use BeginInvoke and EndInvoke to make asynchronous calls. After calling BeginInvoke you can:
3
2098
by: =?Utf-8?B?bWs=?= | last post by:
Hi everyone, I need to refactor some of our processes using the asynchronous programming model. I have defined a couple of arrays of delegates to pipline the asynchronous invocations through different stages of execution. However I was wondering if there was any information available regarding the limitations of the asynchronous model, in particular maximum numbers of asynchronous elements, and limitations of multiple requests potentially...
3
4754
by: =?Utf-8?B?Sm9obg==?= | last post by:
Hi, I need to write asynchronous HTTPModule which will sometimes execute long job. I've written it using AddOnBeginRequestAsync but it still executes synchronously - I am checking performance counters but after running X requests which executes this "asynchronous" module all other requests go to application queue - so it executes synchronously. Code is based on the following article...
2
3437
by: Nicolas Le Gland | last post by:
Hello everyone here. This is my first post in this newsgroup, I hope I won't be to much off-topic. Feel free to redirect me to any better group. I am getting strange timing issues when failing to asynchronously connect sockets on closed or filtered ports, but I'm quite unsure if this is a PHP issue or my misunderstanding, as it seems that socket streams only wrap around <sys/socket.h>.
14
6804
by: shark | last post by:
Hi, Does Form.ShowDialog() start new thread ? If yes how is solved cross-thread operations? Thx
0
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10163
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9037
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5436
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.