i want to show a form as long as work is done
if i show that form in the start of a work and then it should close after when work is done.
for example if i have a function like this
Code:
LoadTree()
End Sub
when this function is started executing then this form should display and when this work is performed on the end of procedure then that form should close.
thanks.
If you want an example on how a routine could put out a from to tell user to wait (or to show a progressbar) while a job is in progress and close it bycode when the job is ended, here is a small example (you can do it in different ways, and you have to adapt it to your code: in this example I put a main module as startup of app, and show a form from there)
I have done it with Net 2008, but there is nothing of 3.5 in it , so it should work also with earlier versions , but you might have to grab the code from modules and recreate the solution and the project)
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
if i use a progress bar on my form1 and then use this method in buttons click event then progress bar should start and when this methos did all work then this progress bar should stop.
progress bar should displayed as long photos are being loaded and after successfull loading progress bar should stop.
the load tree method is as follow
Code:
Private Sub LoadTree()
Try
' Start with clear tree
treeAlbum.Nodes.Clear()
' If the connection is closed, open it.
If SqlConn.State = ConnectionState.Closed Then
SqlConn.Open()
End If
' Just to be sure make sure it was opened
If SqlConn.State = ConnectionState.Open Then
' Create the stored proc command to retrieve the records
Using sqlCmd As New SqlCommand("sp_GetPhotoAlbums", SqlConn)
sqlCmd.CommandType = CommandType.StoredProcedure
' Now execute the command
Using sqlPhotoAlbum As SqlDataReader = sqlCmd.ExecuteReader()
Dim nAlbumID As Integer = 0
' Keep track of the current album
Dim nAlbumIndex As Integer = -1
' Keep track of the tree index
' Keep reading all the records returned
Dim imgList As New ImageList()
'Add image to represent an Album from resources
Dim albumImg As Image = My.Resources.IMG
imgList.Images.Add("Album", albumImg)
imgList.ColorDepth = ColorDepth.Depth32Bit
While (sqlPhotoAlbum.Read())
'Populate all images
Dim data As Byte() = CType(sqlPhotoAlbum("photo"), Byte())
Dim ms As New System.IO.MemoryStream(data, 0, data.Length)
Dim photo As Image = Image.FromStream(ms) ''
imgList.Images.Add(sqlPhotoAlbum("Photo").ToString(), photo)
treeAlbum.ImageList = imgList
' Check if a new album needs to be added to the tree
InsertAlbum(sqlPhotoAlbum("Album").ToString(), sqlPhotoAlbum("Photo").ToString(), CInt(sqlPhotoAlbum("AlbumID")), sqlPhotoAlbum("Album_Desc").ToString(), sqlPhotoAlbum("PhotoID").ToString())
End While
sqlPhotoAlbum.Close()
End Using
End Using
If (SqlConn.State = ConnectionState.Closed) Then
SqlConn.Open()
End If
Using sqlCmd2 As New SqlCommand("sp_GetPhotoAlbums", SqlConn)
Using sqlMatchPhoto As SqlDataReader = sqlCmd2.ExecuteReader()
While (sqlMatchPhoto.Read())
Dim node As TreeNode
For Each node In treeAlbum.Nodes
If (node.Name = sqlMatchPhoto("Album").ToString()) Then
Dim i As Integer
Dim childNode As TreeNode
For Each childNode In node.Nodes
If childNode.Name = sqlMatchPhoto("Photo").ToString() Then
For i = 0 To treeAlbum.ImageList.Images.Count - 1
If treeAlbum.ImageList.Images.Keys(i) = sqlMatchPhoto("Photo").ToString() Then
childNode.ImageIndex = i
Exit For
End If
Next
Exit For
End If
Next
End If
Next
End While
sqlMatchPhoto.Close()
End Using
End Using
End If
Catch e As SqlException
'MessageBox.Show(e.Message)
End Try
End Sub
While (sqlPhotoAlbum.Read())
'Populate all images
Dim data As Byte() = CType(sqlPhotoAlbum("photo"), Byte())
Dim ms As New System.IO.MemoryStream(data, 0, data.Length)
Dim photo As Image = Image.FromStream(ms) ''
imgList.Images.Add(sqlPhotoAlbum("Photo").ToString(), photo)
treeAlbum.ImageList = imgList
' Check if a new album needs to be added to the tree
InsertAlbum(sqlPhotoAlbum("Album").ToString(), sqlPhotoAlbum("Photo").ToString(), CInt(sqlPhotoAlbum("AlbumID")), sqlPhotoAlbum("Album_Desc").ToString(), sqlPhotoAlbum("PhotoID").ToString())
End While
To keep track of this one while going on (=if you want to show a progress of this
partial operation) , you should know how many you have before. but as it is a
SqlDataReader, it is a reader forward only, thus it cannot tell you how many record it has.
One solution is to make a query to get the recordcount,
first, then use that value to divide a counter inside the reading cycle like in:
Code:
'inside your cycle
counter= counter+1
progressbar.value= counter*100/recordcount
the second main Cylce has some nested cycles:
Code:
While (sqlMatchPhoto.Read())
Dim node As TreeNode
For Each node In treeAlbum.Nodes
If (node.Name = sqlMatchPhoto("Album").ToString()) Then
Dim i As Integer
Dim childNode As TreeNode
For Each childNode In node.Nodes
If childNode.Name = sqlMatchPhoto("Photo").ToString() Then
For i = 0 To treeAlbum.ImageList.Images.Count - 1
If treeAlbum.ImageList.Images.Keys(i) = sqlMatchPhoto("Photo").ToString() Then
childNode.ImageIndex = i
Exit For
End If
Next
Exit For
End If
Next
End If
Next
End While
at end is like the firsty: you do operation for each record you find in a sqlDataReader
You could consider using same strategy as above. ou could correct the value of progressbar
to count corrected loaded item and discarding not loaded one from both the counter and the
recodcount, but I do belive it would not add any good to a simpler
"recordProcessed *100 / recordcount" formula....
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.