Asynchronous Message displayed on dataset.fill

soccerwiz13

Member
Joined
Jun 19, 2005
Messages
5
Location
Stateline, NV
Programming Experience
3-5
I have a client that connects to a remote SQL Server database. When I call dataset.fill(ds), it can take a couple of minutes to retrieve all of the data. I would like to open a form or message with an animated .gif that says "Downloading Data", or something of that nature. Similar to a file copy dialog box when large files are transfered in windows. I have tried using asynchronous events, but they don't seem to work. The code runs completely, and then dies on the form definition. And the UI never comes back. The following is my code. If anyone has any idea, please let me know.

Thanks in advance.

Code:

PrivateSub button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button.Click


Dim state AsObject = NewObject
Dim cb As AsyncCallback = New AsyncCallback(AddressOf RemindResult)

Dim dlgt AsNew aSyncDel(AddressOf filldataset)

dlgt.BeginInvoke(cb, state)

frmdownload.showdialog()

EndSub

PublicSub filldataset()
Try

SqlDAVoter.Fill(DSVC.VoterConnections)

Catch ex As Exception

MsgBox(ex.Message)

EndTry

EndSub

PublicSub RemindResult(ByVal ar As IAsyncResult)

Dim strErrors() AsString

Dim StudCb As aSyncDel

Dim flResult AsBoolean

Dim objResult As System.Runtime.Remoting.Messaging.AsyncResult

' Extract the delegate from the AsyncResult

objResult = CType(ar, System.Runtime.Remoting.Messaging.AsyncResult)

StudCb = objResult.AsyncDelegate

' Obtain the result

StudCb.EndInvoke(ar)

frmDownload.Close()

' Output results

EndSub

PrivateDelegateSub aSyncDel()

 
If ever you want to maintain a responsive UI while performing a time-consuming task, think multi-threading. Display your progress dialogue and have it start a new thread to execute the query. When the worker thread completes, it closes the dialogue.
 
I agree with you, my question is how. I am using the above code, and it seems to fill the dataset on a seperate thread, which is what I want. Once that thread is done running, it comes back to my remind result method(asyncCallBack), which runs. Once that is done running, the system freezes. It seems like it does not come back to the UI thread properly.
 
You'll have to excuse me. I've not used your method before so I was speaking from a slightly uninformed perspective. I would have just used Thread.Start to start a new thread from the progress dialogue and then set the form's DialogResult property as the last action of that thread. Without having tested it specifically, I'd think that should work. If you have a Cancel button on your dialogue, you would probably want to use its Click event handler to end the worker thread gracefully.
 
I found some code that says it is the background worker class for .NET 2005. I implemented this code, and it worked perfectly. I could not get my above code to function properly however, as it never came back to the original thread. The background code works only if I set my datagrid.datasource = nothing, fill the ds, then rebind my datagrid to the datasource in the UI thread. The code I used can be found here:
http://www.codeproject.com/vb/net/backgroundworker.asp

Thanks for your help.
 
Given that .NET 2.0 is still in beta, so .NET 1.1 is still the standard, I think it would be a good idea to mention that you are using VS 2005. Although I have never used one, I am well aware of the BackgroundWorker class. I didn't mention it because I didn't know that you were using .NET 2.0.

I'm not 100% sure of this but I think that the only drawback with the BackgroundWorker class is that it cannot affect the UI, so it cannot update progress bars and the like. If this is not an issue, the BackgroundWorker should be perfect.
 
I am actually still using .NET 1.1, I just added the background worker class to my project and I'm able to use that.

Speaking of progress bars, is there anyway to determine how much of a dataset has been downloaded to the system, and the total size of the dataset? I would like to display a progress bar of the work being, but I cannot determine how large my data is, or how much of it has been downloaded.
 
I'm intrigued. Do you mean the BackgroundWorker that is part of .NET 2.0, but you are using it in a .NET 1.1 app? Or is it a class from some other source?

As for progress, if you are performing an long, synchronous operation like a Filling a DataTable or DataSet from a data adapter, your best bet is to use a never-ending progress bar. It doesn't show the actual progress because, as you're aware, you don't know the size of the data or what stage the operation is up to. At least it does give the user an indication that something is happening, though. The standard ProgressBar has some additional functionality in .NET 2.0 to provide this. For .NET 1.1 there are various third party options available from various developer sites, or you can create your own.
 
I downloaded the source code from the above mentioned web site and added it to my project. So I assume its just a class that I have added to my project, but it works great.

Are there any plans in the future from microsoft to include more information about the data being downloaded to the system with SQL 2005 or .NET 2005? I'm probably just use an animated .gif file, similar to the folder copy image in Windows.
 
I misunderstood one of your previous posts. It read like you had found an article that said you could use the BackgroundWorker class because you were using VS 2005.

SqlDataAdapter.Fill() blocks until it either succeeds or fails. If you want to provide real progress data you would have to use an SqlDataReader instead. The extra work it would require to fill the DataSet is probably not worth the reward of being able to give your user the progress.
 
Back
Top