Results 1 to 22 of 22

Thread: Progress bar - filling dataset

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2004
    Posts
    43

    Lightbulb Progress bar - filling dataset

    Hi,

    Is there a way to use a progressbar while filling a dataset with a dataadapter?

    In the application there is a dataadapter which fills a dataset. The problem is that there are sometimes >10000 records to be filled in the dataset. this takes some time. After the fillup of the dataset then a datagrid has to be filled with the dataset

    Therefore I would like to use a progressbar to show the user that the application is processing the request.

    The problem is that I don't know how to set the progressvalue of the progressbar, because the min and max values depend on the number of rows in the dataset and you can't know this before it is filled!!!!!!

    Any ideas?

    jinte

  2. #2
    Fanatic Member ZeBula8's Avatar
    Join Date
    Oct 2002
    Posts
    548
    what i have done in situations such as this is to let the progress continually update and by that i mean:


    Code:
    progressbar.Min = 0
    progressbar.Max = 500
    
    if iX = 450 then iX = 0
    
    
    progressbar.value = iX
    in this manner the progress will begin again - at least this shows that something is going on to the user.

  3. #3
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    I've done that also when you either can't or don't want to figure out the actual percentage.

    In your case, though, can't you just get the Count of the data rows in your data table?

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I don't believe that the dataadapter provides any method of callback to show progress. You could fill it one row at a time but then you'd slow the whol process down. It may be easier to just show some form of animation like the copy dialog that runs while it is working but doesn't actually show the progress.

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2004
    Posts
    43
    thanks,


    @zebula8 : good idea to show something.

    @Mike Hildner
    I get the values of an excelsheet. The dataadapter selectcommand is based on the values the user inputs (eg hitword,...). So this isn't a case where I can count the rows in the datatable because it doesn't exist untill the user creates the select command. The number of rows depends on the selectcommand.

    @edneeis : same idea as zebula8, and I agree that slowing down the application can't be a good choice.

    Any other ideas?

    Thanx

  6. #6
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Haven't used Excel as a data source, but would it be possible to do something like a SELECT COUNT (*) FROM MYTABLE...?

    Edneeis is right about slowing the app down, but depending on what you're doing, this can be OK sometimes - albeit maybe not in this case.

    Perceived speed versus actual speed is something to consider. A progress bar of any sort will be slower than no progress bar, but to the user, the application will appear fast.

    As suggested, just showing that something is going on can be good enough.

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Yes mine and ZeBula8 are similar except I meant something more of an animation sytle then an actual progressbar. Here is an example.

  8. #8

    Thread Starter
    Member
    Join Date
    Jan 2004
    Posts
    43
    Thanks Edneeis,

    I appreciate your posting, but I think it's a pretty complicated for me because I consider myself still as a newbie. Delegates and asynchronous programming isn't my best. But I gave it a try and adopted your code to my application:

    VB Code:
    1. Public Class FrmSearchCorresp2
    2.     Public Delegate Sub FillHandler(ByVal DSExcel As DataSet)
    3.     Private distraction As FrmDistraction
    4.     Private filled As Boolean
    5.  
    6.    
    7.     Public Function GetSourcePath(ByVal number As String) As String
    8.         Dim FileName As String
    9.         'alle info bevindt zich in nummer.xls
    10.         FileName = "\\Epsilon2\vol3\dok\" & number & "\corresp\" & number & "Nummer.xls"
    11.         Return FileName
    12.     End Function
    13.  
    14. 'GetDataFromExcel creates a dataset from an excel file on the server in my company
    15.     Public Function GetDataFromExcel(ByVal FileName As String, ByVal RangeName As String) As System.Data.DataSet
    16.        
    17.         Dim SQLString As String
    18.         Dim hitword, firma, afzender, bestemmeling As Boolean
    19.  
    20.         If Me.TextBoxHitword.Text <> "" Then hitword = True Else hitword = False
    21.         If Me.TextBoxFirma.Text <> "" Then firma = True Else firma = False
    22.         If Me.TextBoxAfzender.Text <> "" Then afzender = True Else afzender = False
    23.         If Me.TextBoxBestemmeling.Text <> "" Then bestemmeling = True Else bestemmeling = False
    24.  
    25.         Try
    26.             Dim strConn As String = _
    27.             "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    28.             "Data Source=" & FileName & "; Extended Properties=Excel 8.0;"
    29.  
    30.             DataConnectionCorresp = New OleDbConnection(strConn)
    31.  
    32.             DataConnectionCorresp.Open()
    33.             'maakt een selectcommand aan
    34.             'alle combinaties dienen bekeken te worden : 4*4 combinaties
    35.             SQLString = "SELECT * FROM " & RangeName
    36.             'controle alle mogelijke variaties met hitword = true
    37.             If (hitword = True And firma = False And afzender = False And bestemmeling = False) Then
    38.                 SQLString = SQLString & " WHERE [korte inhoud] like '%" & Me.TextBoxHitword.Text & "%'"
    39.                 SQLString = SQLString & " OR Firma like '%" & Me.TextBoxHitword.Text & "%'"
    40.                 SQLString = SQLString & " OR Afzender like '%" & Me.TextBoxHitword.Text & "%'"
    41.                 SQLString = SQLString & " OR Bestemmeling like '%" & Me.TextBoxHitword.Text & "%'"
    42.                 'SQLString = SQLString & " OR Doc#nr# like '%" & Me.TextBoxHitword.Text & "%'"
    43.             ElseIf (hitword = True And firma = True And afzender = False And bestemmeling = False) Then
    44.                 SQLString = SQLString & " WHERE [korte inhoud] like '%" & Me.TextBoxHitword.Text & "%'"
    45.                 SQLString = SQLString & " AND (Firma like '%" & Me.TextBoxFirma.Text & "%'"
    46.                 SQLString = SQLString & " OR Afzender like '%" & Me.TextBoxHitword.Text & "%'"
    47.                 SQLString = SQLString & " OR Bestemmeling like '%" & Me.TextBoxHitword.Text & "%')"
    48.             ElseIf (hitword = True And firma = True And afzender = True And bestemmeling = False) Then
    49.                 SQLString = SQLString & " WHERE [korte inhoud] like '%" & Me.TextBoxHitword.Text & "%'"
    50.                 SQLString = SQLString & " AND Firma like '%" & Me.TextBoxFirma.Text & "%'"
    51.                 SQLString = SQLString & " AND (Afzender like '%" & Me.TextBoxAfzender.Text & "%'"
    52.                 SQLString = SQLString & " OR Bestemmeling like '%" & Me.TextBoxHitword.Text & "%')"
    53.             ElseIf (hitword = True And firma = True And afzender = False And bestemmeling = True) Then
    54.                 SQLString = SQLString & " WHERE [korte inhoud] like '%" & Me.TextBoxHitword.Text & "%'"
    55.                 SQLString = SQLString & " AND Firma like '%" & Me.TextBoxFirma.Text & "%'"
    56.                 SQLString = SQLString & " AND (Bestemmeling like '%" & Me.TextBoxBestemmeling.Text & "%'"
    57.                 SQLString = SQLString & " OR Afzender like '%" & Me.TextBoxHitword.Text & "%')"
    58.             ElseIf (hitword = True And firma = True And afzender = True And bestemmeling = True) Then
    59.                 SQLString = SQLString & " WHERE [korte inhoud] like '%" & Me.TextBoxHitword.Text & "%'"
    60.                 SQLString = SQLString & " AND Firma like '%" & Me.TextBoxFirma.Text & "%'"
    61.                 SQLString = SQLString & " AND Afzender like '%" & Me.TextBoxAfzender.Text & "%'"
    62.                 SQLString = SQLString & " AND Bestemmeling like '%" & Me.TextBoxBestemmeling.Text & "%'"
    63.                 'controle van alle mogelijke variaties met hitword = false
    64.             ElseIf (hitword = False And firma = True And afzender = False And bestemmeling = False) Then
    65.                 SQLString = SQLString & " WHERE Firma like '%" & Me.TextBoxFirma.Text & "%'"
    66.             ElseIf (hitword = False And firma = False And afzender = True And bestemmeling = False) Then
    67.                 SQLString = SQLString & " WHERE Afzender like '%" & Me.TextBoxAfzender.Text & "%'"
    68.             ElseIf (hitword = False And firma = False And afzender = False And bestemmeling = True) Then
    69.                 SQLString = SQLString & " WHERE Bestemmmeling like '%" & Me.TextBoxBestemmeling.Text & "%'"
    70.             ElseIf (hitword = False And firma = True And afzender = True And bestemmeling = False) Then
    71.                 SQLString = SQLString & " WHERE Firma like '%" & Me.TextBoxFirma.Text & "%'"
    72.                 SQLString = SQLString & " AND Afzender like '%" & Me.TextBoxAfzender.Text & "%'"
    73.             ElseIf (hitword = False And firma = True And afzender = False And bestemmeling = True) Then
    74.                 SQLString = SQLString & " WHERE Firma like '%" & Me.TextBoxFirma.Text & "%'"
    75.                 SQLString = SQLString & " AND Bestemmeling like '%" & Me.TextBoxBestemmeling.Text & "%'"
    76.             ElseIf (hitword = False And firma = False And afzender = True And bestemmeling = False) Then
    77.                 SQLString = SQLString & " WHERE Afzender like '%" & Me.TextBoxAfzender.Text & "%'"
    78.             ElseIf (hitword = False And firma = False And afzender = False And bestemmeling = True) Then
    79.                 SQLString = SQLString & " WHERE Bestemmeling like '%" & Me.TextBoxBestemmeling.Text & "%'"
    80.             ElseIf (hitword = False And firma = True And afzender = True And bestemmeling = True) Then
    81.                 SQLString = SQLString & " WHERE Firma like '%" & Me.TextBoxFirma.Text & "%'"
    82.                 SQLString = SQLString & " AND Bestemmeling like '%" & Me.TextBoxBestemmeling.Text & "%'"
    83.                 SQLString = SQLString & " AND Afzender like '%" & Me.TextBoxAfzender.Text & "%'"
    84.             End If
    85.  
    86.             'MsgBox("bestemmeling=" & bestemmeling & "firma=" & firma)
    87.             'MsgBox("afzender=" & afzender & "trefwoord=" & hitword)
    88.  
    89.             Dim SelectCommand As New OleDbCommand(SQLString, DataConnectionCorresp)
    90.             Dim DAExcel As New OleDbDataAdapter
    91.             DAExcel.SelectCommand = SelectCommand
    92.             ' vul dataset
    93.             DAExcel.Fill(DSExcel, "Excel")
    94.             DataConnectionCorresp.Close()
    95.  
    96.             filled = True
    97.  
    98.              Return DSExcel
    99.         Catch
    100.             'eventuele error handling
    101.             Return Nothing
    102.         End Try
    103.     End Function
    104.  
    105.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    106.         DSExcel.Clear()
    107.         'Here I create the delegate
    108.  
    109.         Dim del As New FillHandler(AddressOf FillDatasetThread)
    110.         del.BeginInvoke(DSExcel, AddressOf CallBack, DSExcel)
    111.         distraction = New FrmDistraction
    112.         distraction.ShowDialog()
    113.  
    114.     End Sub
    115.  
    116.     Public Sub FillDatasetThread(ByVal DSExcel As DataSet)
    117.         Dim filenameNummer As String = GetSourcePath(Me.TextBoxNummer.Text)
    118.  
    119.         If File.Exists(filenameNummer) = True Then
    120.             GetDataFromExcel(GetSourcePath(Me.TextBoxNummer.Text), "[blad1$A3:F3000]")
    121.         Else
    122.             MsgBox("Dossier " & Me.TextBoxNummer.Text & " bestaat niet.  Gelieve een ander dossiernr. in te geven", MsgBoxStyle.Critical, "")
    123.         End If
    124.     End Sub
    125.  
    126.     Public Sub CallBack(ByVal ar As IAsyncResult)
    127.         Dim DS As DataSet = CType(ar.AsyncState, DataSet)
    128.         If filled = True Then
    129.             distraction.DialogResult = DialogResult.OK
    130.             If DS.Tables(0).Rows.Count > 0 Then
    131.                 Me.createstyles()
    132.                 Me.dataGrid1.DataSource = DS
    133.                 Me.dataGrid1.DataMember = "Excel"
    134.             Else
    135.                 MsgBox("No results", MsgBoxStyle.Critical, "")
    136.             End If
    137.         End If
    138.     End Sub

    The actual distraction animation is in the FrmDistraction and the code is the same as in your sample.

    The problem is the following: when the user hits the button1, then the distractionanimation appears and starts working. After a few seconds (I suppose after the dataset DSExcel is created) the animation stops working and disappears but my datagrid1 isn't filled up with the results however it should because it's in my CallBack Sub.

    I think there is a problem with the different threads (one for the distraction animation and one for filling up the dataset) and the fact that the dataset necessary for filling up the datagrid is created or passed to the wrong thread.

    Am I correct? Or am I doing something wrong?

    Any ideas?

    Thanx in advance...

    Jinte

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You are right it is because of the way you are handling the dataset. Instead of passing one into the method on the other thread you are letting it grab one from the other thread and then trying to return it as a function result but not grabbing the result. You almost have it. I made a few slight changes give this a try:
    VB Code:
    1. Public Class FrmSearchCorresp2
    2.     Public Delegate Sub FillHandler(ByVal ds As DataSet)
    3.     Private distraction As FrmDistraction
    4.     Private filled As Boolean
    5.  
    6.  
    7.     Public Function GetSourcePath(ByVal number As String) As String
    8.         Dim FileName As String
    9.         'alle info bevindt zich in nummer.xls
    10.         FileName = "\\Epsilon2\vol3\dok\" & number & "\corresp\" & number & "Nummer.xls"
    11.         Return FileName
    12.     End Function
    13.  
    14.     'GetDataFromExcel creates a dataset from an excel file on the server in my company
    15.     Public Sub GetDataFromExcel(ByVal FileName As String, ByVal RangeName As String, ByVal ds As System.Data.DataSet)
    16.  
    17.         Dim SQLString As String
    18.         Dim hitword, firma, afzender, bestemmeling As Boolean
    19.  
    20.         If Me.TextBoxHitword.Text <> "" Then hitword = True Else hitword = False
    21.         If Me.TextBoxFirma.Text <> "" Then firma = True Else firma = False
    22.         If Me.TextBoxAfzender.Text <> "" Then afzender = True Else afzender = False
    23.         If Me.TextBoxBestemmeling.Text <> "" Then bestemmeling = True Else bestemmeling = False
    24.  
    25.         Try
    26.             Dim strConn As String = _
    27.             "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    28.             "Data Source=" & FileName & "; Extended Properties=Excel 8.0;"
    29.  
    30.             DataConnectionCorresp = New OleDbConnection(strConn)
    31.  
    32.             DataConnectionCorresp.Open()
    33.             'maakt een selectcommand aan
    34.             'alle combinaties dienen bekeken te worden : 4*4 combinaties
    35.             SQLString = "SELECT * FROM " & RangeName
    36.             'controle alle mogelijke variaties met hitword = true
    37.             If (hitword = True And firma = False And afzender = False And bestemmeling = False) Then
    38.                 SQLString = SQLString & " WHERE [korte inhoud] like '%" & Me.TextBoxHitword.Text & "%'"
    39.                 SQLString = SQLString & " OR Firma like '%" & Me.TextBoxHitword.Text & "%'"
    40.                 SQLString = SQLString & " OR Afzender like '%" & Me.TextBoxHitword.Text & "%'"
    41.                 SQLString = SQLString & " OR Bestemmeling like '%" & Me.TextBoxHitword.Text & "%'"
    42.                 'SQLString = SQLString & " OR Doc#nr# like '%" & Me.TextBoxHitword.Text & "%'"
    43.             ElseIf (hitword = True And firma = True And afzender = False And bestemmeling = False) Then
    44.                 SQLString = SQLString & " WHERE [korte inhoud] like '%" & Me.TextBoxHitword.Text & "%'"
    45.                 SQLString = SQLString & " AND (Firma like '%" & Me.TextBoxFirma.Text & "%'"
    46.                 SQLString = SQLString & " OR Afzender like '%" & Me.TextBoxHitword.Text & "%'"
    47.                 SQLString = SQLString & " OR Bestemmeling like '%" & Me.TextBoxHitword.Text & "%')"
    48.             ElseIf (hitword = True And firma = True And afzender = True And bestemmeling = False) Then
    49.                 SQLString = SQLString & " WHERE [korte inhoud] like '%" & Me.TextBoxHitword.Text & "%'"
    50.                 SQLString = SQLString & " AND Firma like '%" & Me.TextBoxFirma.Text & "%'"
    51.                 SQLString = SQLString & " AND (Afzender like '%" & Me.TextBoxAfzender.Text & "%'"
    52.                 SQLString = SQLString & " OR Bestemmeling like '%" & Me.TextBoxHitword.Text & "%')"
    53.             ElseIf (hitword = True And firma = True And afzender = False And bestemmeling = True) Then
    54.                 SQLString = SQLString & " WHERE [korte inhoud] like '%" & Me.TextBoxHitword.Text & "%'"
    55.                 SQLString = SQLString & " AND Firma like '%" & Me.TextBoxFirma.Text & "%'"
    56.                 SQLString = SQLString & " AND (Bestemmeling like '%" & Me.TextBoxBestemmeling.Text & "%'"
    57.                 SQLString = SQLString & " OR Afzender like '%" & Me.TextBoxHitword.Text & "%')"
    58.             ElseIf (hitword = True And firma = True And afzender = True And bestemmeling = True) Then
    59.                 SQLString = SQLString & " WHERE [korte inhoud] like '%" & Me.TextBoxHitword.Text & "%'"
    60.                 SQLString = SQLString & " AND Firma like '%" & Me.TextBoxFirma.Text & "%'"
    61.                 SQLString = SQLString & " AND Afzender like '%" & Me.TextBoxAfzender.Text & "%'"
    62.                 SQLString = SQLString & " AND Bestemmeling like '%" & Me.TextBoxBestemmeling.Text & "%'"
    63.                 'controle van alle mogelijke variaties met hitword = false
    64.             ElseIf (hitword = False And firma = True And afzender = False And bestemmeling = False) Then
    65.                 SQLString = SQLString & " WHERE Firma like '%" & Me.TextBoxFirma.Text & "%'"
    66.             ElseIf (hitword = False And firma = False And afzender = True And bestemmeling = False) Then
    67.                 SQLString = SQLString & " WHERE Afzender like '%" & Me.TextBoxAfzender.Text & "%'"
    68.             ElseIf (hitword = False And firma = False And afzender = False And bestemmeling = True) Then
    69.                 SQLString = SQLString & " WHERE Bestemmmeling like '%" & Me.TextBoxBestemmeling.Text & "%'"
    70.             ElseIf (hitword = False And firma = True And afzender = True And bestemmeling = False) Then
    71.                 SQLString = SQLString & " WHERE Firma like '%" & Me.TextBoxFirma.Text & "%'"
    72.                 SQLString = SQLString & " AND Afzender like '%" & Me.TextBoxAfzender.Text & "%'"
    73.             ElseIf (hitword = False And firma = True And afzender = False And bestemmeling = True) Then
    74.                 SQLString = SQLString & " WHERE Firma like '%" & Me.TextBoxFirma.Text & "%'"
    75.                 SQLString = SQLString & " AND Bestemmeling like '%" & Me.TextBoxBestemmeling.Text & "%'"
    76.             ElseIf (hitword = False And firma = False And afzender = True And bestemmeling = False) Then
    77.                 SQLString = SQLString & " WHERE Afzender like '%" & Me.TextBoxAfzender.Text & "%'"
    78.             ElseIf (hitword = False And firma = False And afzender = False And bestemmeling = True) Then
    79.                 SQLString = SQLString & " WHERE Bestemmeling like '%" & Me.TextBoxBestemmeling.Text & "%'"
    80.             ElseIf (hitword = False And firma = True And afzender = True And bestemmeling = True) Then
    81.                 SQLString = SQLString & " WHERE Firma like '%" & Me.TextBoxFirma.Text & "%'"
    82.                 SQLString = SQLString & " AND Bestemmeling like '%" & Me.TextBoxBestemmeling.Text & "%'"
    83.                 SQLString = SQLString & " AND Afzender like '%" & Me.TextBoxAfzender.Text & "%'"
    84.             End If
    85.  
    86.             'MsgBox("bestemmeling=" & bestemmeling & "firma=" & firma)
    87.             'MsgBox("afzender=" & afzender & "trefwoord=" & hitword)
    88.  
    89.             Dim SelectCommand As New OleDbCommand(SQLString, DataConnectionCorresp)
    90.             Dim DAExcel As New OleDbDataAdapter
    91.             DAExcel.SelectCommand = SelectCommand
    92.             ' vul dataset
    93.             DAExcel.Fill(ds, "Excel")
    94.             DataConnectionCorresp.Close()
    95.  
    96.             filled = True
    97.  
    98.             'Return DSExcel
    99.         Catch
    100.             'eventuele error handling
    101.             'Return Nothing
    102.         End Try
    103.     End Sub
    104.  
    105.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    106.         'I assume dsexcel is already created somewhere
    107.         'DSExcel.Clear()
    108.         'for testing I just created a new dataset which shouldn't matter since it isn't a typed dataset
    109.         Dim ds As New DataSet
    110.         'Here I create the delegate
    111.  
    112.         Dim del As New FillHandler(AddressOf FillDatasetThread)
    113.         'del.BeginInvoke(DSExcel, AddressOf CallBack, DSExcel)
    114.         del.BeginInvoke(ds, AddressOf CallBack, ds)
    115.         distraction = New FrmDistraction
    116.         distraction.ShowDialog()
    117.  
    118.     End Sub
    119.  
    120.     Public Sub FillDatasetThread(ByVal ds As DataSet)
    121.         Dim filenameNummer As String = GetSourcePath(Me.TextBoxNummer.Text)
    122.  
    123.         If File.Exists(filenameNummer) = True Then
    124.             GetDataFromExcel(GetSourcePath(Me.TextBoxNummer.Text), "[blad1$A3:F3000]", ds)
    125.         Else
    126.             MsgBox("Dossier " & Me.TextBoxNummer.Text & " bestaat niet.  Gelieve een ander dossiernr. in te geven", MsgBoxStyle.Critical, "")
    127.         End If
    128.     End Sub
    129.  
    130.     Public Sub CallBack(ByVal ar As IAsyncResult)
    131.         Dim DS As DataSet = CType(ar.AsyncState, DataSet)
    132.         If filled = True Then
    133.             distraction.DialogResult = DialogResult.OK
    134.             If DS.Tables(0).Rows.Count > 0 Then
    135.                 Me.createstyles()
    136.                 Me.dataGrid1.DataSource = DS
    137.                 Me.dataGrid1.DataMember = "Excel"
    138.             Else
    139.                 MsgBox("No results", MsgBoxStyle.Critical, "")
    140.             End If
    141.         End If
    142.     End Sub
    143.  
    144. End Class

  10. #10

    Thread Starter
    Member
    Join Date
    Jan 2004
    Posts
    43
    Thanks Edneeis,

    I'll try your suggestion tomorrow on my work, I don't have the application on my laptop at home (I live in Europe=> time difference).

    I'll keep you informed.

    Jinte

  11. #11

    Thread Starter
    Member
    Join Date
    Jan 2004
    Posts
    43
    @Edneeis,

    Well I said I would keep you informed. Here I am.

    The application still isn't working correct. The distraction is showing up and disappearing (after the filling up has completed I suppose) but the datagrid isn't filled up. So there is a problem with the passing of the dataset I think.

    Here you can find the code I used, with some comments in it and some questions.

    VB Code:
    1. Public Class FrmSearchCorresp2
    2.     Inherits BeheerCDs.Form1
    3.     Private WithEvents dataGrid1 As DataGrid
    4.     Public DSExcel As New System.Data.DataSet
    5.     Public DataConnectionCorresp As OleDbConnection
    6.     Public Delegate Sub FillHandler(ByVal ds As DataSet)
    7.     Private distraction As FrmDistraction
    8.     Private filled As Boolean = False
    9.  
    10.  
    11.     Public Function GetSourcePath(ByVal number As String) As String
    12.         Dim FileName As String
    13.         'alle info bevindt zich in nummer.xls
    14.         FileName = "\\Epsilon2\vol3\dok\" & number & "\corresp\" & number & "Nummer.xls"
    15.         Return FileName
    16.     End Function
    17.  
    18.     'GetDataFromExcel creates a dataset from an excel file on the server in my company
    19.     Public Sub GetDataFromExcel(ByVal FileName As String, ByVal RangeName As String, ByVal ds As System.Data.DataSet)
    20.  
    21.         Dim SQLString As String
    22.         Dim hitword, firma, afzender, bestemmeling As Boolean
    23.  
    24.         If Me.TextBoxHitword.Text <> "" Then hitword = True Else hitword = False
    25.         If Me.TextBoxFirma.Text <> "" Then firma = True Else firma = False
    26.         If Me.TextBoxAfzender.Text <> "" Then afzender = True Else afzender = False
    27.         If Me.TextBoxBestemmeling.Text <> "" Then bestemmeling = True Else bestemmeling = False
    28.  
    29.         Try
    30.             Dim strConn As String = _
    31.             "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    32.             "Data Source=" & FileName & "; Extended Properties=Excel 8.0;"
    33.  
    34.             DataConnectionCorresp = New OleDbConnection(strConn)
    35.  
    36.             DataConnectionCorresp.Open()
    37.            
    38.             SQLString = "SELECT * FROM " & RangeName
    39.  
    40.             If (hitword = True And firma = False And afzender = False And bestemmeling = False) Then
    41.                 SQLString = SQLString & " WHERE [korte inhoud] like '%" & Me.TextBoxHitword.Text & "%'"
    42.                 SQLString = SQLString & " OR Firma like '%" & Me.TextBoxHitword.Text & "%'"
    43.                 SQLString = SQLString & " OR Afzender like '%" & Me.TextBoxHitword.Text & "%'"
    44.                 SQLString = SQLString & " OR Bestemmeling like '%" & Me.TextBoxHitword.Text & "%'"
    45.             ElseIf (hitword = True And firma = True And afzender = False And bestemmeling = False) Then
    46.                 SQLString = SQLString & " WHERE [korte inhoud] like '%" & Me.TextBoxHitword.Text & "%'"
    47.                 SQLString = SQLString & " AND (Firma like '%" & Me.TextBoxFirma.Text & "%'"
    48.                 SQLString = SQLString & " OR Afzender like '%" & Me.TextBoxHitword.Text & "%'"
    49.                 SQLString = SQLString & " OR Bestemmeling like '%" & Me.TextBoxHitword.Text & "%')"
    50.             ElseIf (hitword = True And firma = True And afzender = True And bestemmeling = False) Then
    51.                 SQLString = SQLString & " WHERE [korte inhoud] like '%" & Me.TextBoxHitword.Text & "%'"
    52.                 SQLString = SQLString & " AND Firma like '%" & Me.TextBoxFirma.Text & "%'"
    53.                 SQLString = SQLString & " AND (Afzender like '%" & Me.TextBoxAfzender.Text & "%'"
    54.                 SQLString = SQLString & " OR Bestemmeling like '%" & Me.TextBoxHitword.Text & "%')"
    55.             ElseIf (hitword = True And firma = True And afzender = False And bestemmeling = True) Then
    56.                 SQLString = SQLString & " WHERE [korte inhoud] like '%" & Me.TextBoxHitword.Text & "%'"
    57.                 SQLString = SQLString & " AND Firma like '%" & Me.TextBoxFirma.Text & "%'"
    58.                 SQLString = SQLString & " AND (Bestemmeling like '%" & Me.TextBoxBestemmeling.Text & "%'"
    59.                 SQLString = SQLString & " OR Afzender like '%" & Me.TextBoxHitword.Text & "%')"
    60.             ElseIf (hitword = True And firma = True And afzender = True And bestemmeling = True) Then
    61.                 SQLString = SQLString & " WHERE [korte inhoud] like '%" & Me.TextBoxHitword.Text & "%'"
    62.                 SQLString = SQLString & " AND Firma like '%" & Me.TextBoxFirma.Text & "%'"
    63.                 SQLString = SQLString & " AND Afzender like '%" & Me.TextBoxAfzender.Text & "%'"
    64.                 SQLString = SQLString & " AND Bestemmeling like '%" & Me.TextBoxBestemmeling.Text & "%'"
    65.             ElseIf (hitword = False And firma = True And afzender = False And bestemmeling = False) Then
    66.                 SQLString = SQLString & " WHERE Firma like '%" & Me.TextBoxFirma.Text & "%'"
    67.             ElseIf (hitword = False And firma = False And afzender = True And bestemmeling = False) Then
    68.                 SQLString = SQLString & " WHERE Afzender like '%" & Me.TextBoxAfzender.Text & "%'"
    69.             ElseIf (hitword = False And firma = False And afzender = False And bestemmeling = True) Then
    70.                 SQLString = SQLString & " WHERE Bestemmmeling like '%" & Me.TextBoxBestemmeling.Text & "%'"
    71.             ElseIf (hitword = False And firma = True And afzender = True And bestemmeling = False) Then
    72.                 SQLString = SQLString & " WHERE Firma like '%" & Me.TextBoxFirma.Text & "%'"
    73.                 SQLString = SQLString & " AND Afzender like '%" & Me.TextBoxAfzender.Text & "%'"
    74.             ElseIf (hitword = False And firma = True And afzender = False And bestemmeling = True) Then
    75.                 SQLString = SQLString & " WHERE Firma like '%" & Me.TextBoxFirma.Text & "%'"
    76.                 SQLString = SQLString & " AND Bestemmeling like '%" & Me.TextBoxBestemmeling.Text & "%'"
    77.             ElseIf (hitword = False And firma = False And afzender = True And bestemmeling = False) Then
    78.                 SQLString = SQLString & " WHERE Afzender like '%" & Me.TextBoxAfzender.Text & "%'"
    79.             ElseIf (hitword = False And firma = False And afzender = False And bestemmeling = True) Then
    80.                 SQLString = SQLString & " WHERE Bestemmeling like '%" & Me.TextBoxBestemmeling.Text & "%'"
    81.             ElseIf (hitword = False And firma = True And afzender = True And bestemmeling = True) Then
    82.                 SQLString = SQLString & " WHERE Firma like '%" & Me.TextBoxFirma.Text & "%'"
    83.                 SQLString = SQLString & " AND Bestemmeling like '%" & Me.TextBoxBestemmeling.Text & "%'"
    84.                 SQLString = SQLString & " AND Afzender like '%" & Me.TextBoxAfzender.Text & "%'"
    85.             End If
    86.  
    87.             Dim SelectCommand As New OleDbCommand(SQLString, DataConnectionCorresp)
    88.             Dim DAExcel As New OleDbDataAdapter
    89.             DAExcel.SelectCommand = SelectCommand
    90.             DAExcel.Fill(ds, "Excel")
    91.             DataConnectionCorresp.Close()
    92.  
    93.             'after the connection is closed we can state that the process has ended so, filled can be set true
    94.             filled = True
    95.  
    96.         Catch
    97.  
    98.         End Try
    99.     End Sub
    100.  
    101.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    102.         'DSExcel is a private variable declared
    103.         DSExcel.Clear()
    104.         'creation of an instance of the delegate
    105.         Dim del As New FillHandler(AddressOf FillDatasetThread)
    106.         'I think Asyncstate must be set to nothing, and not to DSExcel?? Or am i wrong?
    107.         'Can I use the DSExcel to pass as a variable?
    108.         del.BeginInvoke(DSExcel, AddressOf CallBack, Nothing)
    109.         'show the distraction form
    110.         'the showing of the form is thread 1 (the synchronous operation)
    111.         distraction = New FrmDistraction
    112.         distraction.ShowDialog()
    113.  
    114.     End Sub
    115.  
    116.     ' this method is the start of the second thread : the asynchronous operation.  Am I correct?
    117.     Public Sub FillDatasetThread(ByVal ds As DataSet)
    118.         Dim filenameNummer As String = GetSourcePath(Me.TextBoxNummer.Text)
    119.  
    120.         'here there is something wrong: suppose the user inputs a filenamenummer that doesn't exists,
    121.         'then the msgbox appears after the distraction form opens.
    122.         'When is the delegate method returning a value?
    123.         If File.Exists(filenameNummer) = True Then
    124.             GetDataFromExcel(GetSourcePath(Me.TextBoxNummer.Text), "[blad1$A3:F3000]", ds)
    125.         Else
    126.             MsgBox("Dossier " & Me.TextBoxNummer.Text & " bestaat niet.  Gelieve een ander dossiernr. in te geven", MsgBoxStyle.Critical, "")
    127.         End If
    128.     End Sub
    129.  
    130.     'creation of the callback method
    131.     Public Sub CallBack(ByVal ar As IAsyncResult)
    132.         Dim DS As DataSet = CType(ar.AsyncState, DataSet)
    133.         If filled = True Then
    134.             'why do I need this dialogresult method?  Can't I detect when the dataset filling operation has
    135.             'finished with the filled boolean or with the Endinvoke method of the delegate?
    136.             distraction.DialogResult = DialogResult.OK
    137.             If DS.Tables(0).Rows.Count > 0 Then
    138.                 'the datagrid is never filled up with the results of the dataset DSExcel
    139.                 'but the frmDistraction disappears so the filling up of the dataset has ended
    140.                 'only the results aren't passed
    141.                 Me.createstyles()
    142.                 Me.dataGrid1.DataSource = DS
    143.                 Me.dataGrid1.DataMember = "Excel"
    144.             Else
    145.                 MsgBox("No results", MsgBoxStyle.Critical, "")
    146.             End If
    147.         End If
    148.     End Sub

    Could you please take a look? These delegates aren't simple to use. Are any good tutorials on delegates and asynchronous operations?

    Many thanks, I really appreciate it.

  12. #12
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    What actually gets passed back is the asyncState parameter which is the last parameter in this line:

    del.BeginInvoke(DSExcel, AddressOf CallBack, Nothing)

    Try changing that to:

    del.BeginInvoke(DSExcel, AddressOf CallBack, DSExcel)

    The first item is just a parameter required by the method, the 2nd is the callback address, the third is the state object which can be retrieved at any point in the async call and is retrieved to return the result in the end call (Callback method):

    Dim DS As DataSet = CType(ar.AsyncState, DataSet)

    That is probably why it wasn't returning anything. If by chance its not that it must be something very simple that has been overlooked by both of us.

    The signature of a BeginInvoke call essentially goes like this:
    BeginInvoke([Any Parameters for the Sub that the delegate represents],[Where to call when the Invoked method is done],[AsyncState object which is just a generic object that we can optionally include that can be retrieved at any point in the async call])

  13. #13

    Thread Starter
    Member
    Join Date
    Jan 2004
    Posts
    43
    @Edneeis:

    it still isn't working but we are close. I inserted :

    MsgBox(DS.Tables(0).Rows.Count)

    in the callback method before me.createstyles and if the application runs it gives the correct number of rows in the dataset. So this means the dataset is filled but the datagrid still isn't filled.

    I included the code in the zip-file.

    Thanks for your help.
    Attached Files Attached Files

  14. #14
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I guess I'm getting old, but here is the fixed code:
    VB Code:
    1. Public Class FrmSearchCorresp2
    2.         Inherits BeheerCDs.Form1
    3.         Private WithEvents dataGrid1 As DataGrid
    4.         Public DSExcel As New System.Data.DataSet
    5.         Public DataConnectionCorresp As OleDbConnection
    6.         Public Delegate Sub FillHandler(ByVal ds As DataSet)
    7.         Private distraction As FrmDistraction
    8.  
    9.  
    10.         Public Function GetSourcePath(ByVal number As String) As String
    11.             Dim FileName As String
    12.             'alle info bevindt zich in nummer.xls
    13.             FileName = "\\Epsilon2\vol3\dok\" & number & "\corresp\" & number & "Nummer.xls"
    14.             Return FileName
    15.         End Function
    16.  
    17.         'GetDataFromExcel creates a dataset from an excel file on the server in my company
    18.         Public Sub GetDataFromExcel(ByVal FileName As String, ByVal RangeName As String, ByVal ds As System.Data.DataSet)
    19.  
    20.             Dim SQLString As String
    21.             Dim hitword, firma, afzender, bestemmeling As Boolean
    22.  
    23.             If Me.TextBoxHitword.Text <> "" Then hitword = True Else hitword = False
    24.             If Me.TextBoxFirma.Text <> "" Then firma = True Else firma = False
    25.             If Me.TextBoxAfzender.Text <> "" Then afzender = True Else afzender = False
    26.             If Me.TextBoxBestemmeling.Text <> "" Then bestemmeling = True Else bestemmeling = False
    27.  
    28.             Try
    29.                 Dim strConn As String = _
    30.                 "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    31.                 "Data Source=" & FileName & "; Extended Properties=Excel 8.0;"
    32.  
    33.                 DataConnectionCorresp = New OleDbConnection(strConn)
    34.  
    35.                 DataConnectionCorresp.Open()
    36.  
    37.                 SQLString = "SELECT * FROM " & RangeName
    38.  
    39.                 If (hitword = True And firma = False And afzender = False And bestemmeling = False) Then
    40.                     SQLString = SQLString & " WHERE [korte inhoud] like '%" & Me.TextBoxHitword.Text & "%'"
    41.                     SQLString = SQLString & " OR Firma like '%" & Me.TextBoxHitword.Text & "%'"
    42.                     SQLString = SQLString & " OR Afzender like '%" & Me.TextBoxHitword.Text & "%'"
    43.                     SQLString = SQLString & " OR Bestemmeling like '%" & Me.TextBoxHitword.Text & "%'"
    44.                 ElseIf (hitword = True And firma = True And afzender = False And bestemmeling = False) Then
    45.                     SQLString = SQLString & " WHERE [korte inhoud] like '%" & Me.TextBoxHitword.Text & "%'"
    46.                     SQLString = SQLString & " AND (Firma like '%" & Me.TextBoxFirma.Text & "%'"
    47.                     SQLString = SQLString & " OR Afzender like '%" & Me.TextBoxHitword.Text & "%'"
    48.                     SQLString = SQLString & " OR Bestemmeling like '%" & Me.TextBoxHitword.Text & "%')"
    49.                 ElseIf (hitword = True And firma = True And afzender = True And bestemmeling = False) Then
    50.                     SQLString = SQLString & " WHERE [korte inhoud] like '%" & Me.TextBoxHitword.Text & "%'"
    51.                     SQLString = SQLString & " AND Firma like '%" & Me.TextBoxFirma.Text & "%'"
    52.                     SQLString = SQLString & " AND (Afzender like '%" & Me.TextBoxAfzender.Text & "%'"
    53.                     SQLString = SQLString & " OR Bestemmeling like '%" & Me.TextBoxHitword.Text & "%')"
    54.                 ElseIf (hitword = True And firma = True And afzender = False And bestemmeling = True) Then
    55.                     SQLString = SQLString & " WHERE [korte inhoud] like '%" & Me.TextBoxHitword.Text & "%'"
    56.                     SQLString = SQLString & " AND Firma like '%" & Me.TextBoxFirma.Text & "%'"
    57.                     SQLString = SQLString & " AND (Bestemmeling like '%" & Me.TextBoxBestemmeling.Text & "%'"
    58.                     SQLString = SQLString & " OR Afzender like '%" & Me.TextBoxHitword.Text & "%')"
    59.                 ElseIf (hitword = True And firma = True And afzender = True And bestemmeling = True) Then
    60.                     SQLString = SQLString & " WHERE [korte inhoud] like '%" & Me.TextBoxHitword.Text & "%'"
    61.                     SQLString = SQLString & " AND Firma like '%" & Me.TextBoxFirma.Text & "%'"
    62.                     SQLString = SQLString & " AND Afzender like '%" & Me.TextBoxAfzender.Text & "%'"
    63.                     SQLString = SQLString & " AND Bestemmeling like '%" & Me.TextBoxBestemmeling.Text & "%'"
    64.                 ElseIf (hitword = False And firma = True And afzender = False And bestemmeling = False) Then
    65.                     SQLString = SQLString & " WHERE Firma like '%" & Me.TextBoxFirma.Text & "%'"
    66.                 ElseIf (hitword = False And firma = False And afzender = True And bestemmeling = False) Then
    67.                     SQLString = SQLString & " WHERE Afzender like '%" & Me.TextBoxAfzender.Text & "%'"
    68.                 ElseIf (hitword = False And firma = False And afzender = False And bestemmeling = True) Then
    69.                     SQLString = SQLString & " WHERE Bestemmmeling like '%" & Me.TextBoxBestemmeling.Text & "%'"
    70.                 ElseIf (hitword = False And firma = True And afzender = True And bestemmeling = False) Then
    71.                     SQLString = SQLString & " WHERE Firma like '%" & Me.TextBoxFirma.Text & "%'"
    72.                     SQLString = SQLString & " AND Afzender like '%" & Me.TextBoxAfzender.Text & "%'"
    73.                 ElseIf (hitword = False And firma = True And afzender = False And bestemmeling = True) Then
    74.                     SQLString = SQLString & " WHERE Firma like '%" & Me.TextBoxFirma.Text & "%'"
    75.                     SQLString = SQLString & " AND Bestemmeling like '%" & Me.TextBoxBestemmeling.Text & "%'"
    76.                 ElseIf (hitword = False And firma = False And afzender = True And bestemmeling = False) Then
    77.                     SQLString = SQLString & " WHERE Afzender like '%" & Me.TextBoxAfzender.Text & "%'"
    78.                 ElseIf (hitword = False And firma = False And afzender = False And bestemmeling = True) Then
    79.                     SQLString = SQLString & " WHERE Bestemmeling like '%" & Me.TextBoxBestemmeling.Text & "%'"
    80.                 ElseIf (hitword = False And firma = True And afzender = True And bestemmeling = True) Then
    81.                     SQLString = SQLString & " WHERE Firma like '%" & Me.TextBoxFirma.Text & "%'"
    82.                     SQLString = SQLString & " AND Bestemmeling like '%" & Me.TextBoxBestemmeling.Text & "%'"
    83.                     SQLString = SQLString & " AND Afzender like '%" & Me.TextBoxAfzender.Text & "%'"
    84.                 End If
    85.  
    86.                 Dim SelectCommand As New OleDbCommand(SQLString, DataConnectionCorresp)
    87.                 Dim DAExcel As New OleDbDataAdapter
    88.                 DAExcel.SelectCommand = SelectCommand
    89.                 DAExcel.Fill(ds, "Excel")
    90.                 DataConnectionCorresp.Close()
    91.             Catch
    92.  
    93.             End Try
    94.         End Sub
    95.  
    96.         Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    97.             'DSExcel is a private variable declared
    98.             DSExcel.Clear()
    99.             'creation of an instance of the delegate
    100.             Dim del As New FillHandler(AddressOf FillDatasetThread)
    101.             'Can I use the DSExcel to pass as a variable?
    102.             del.BeginInvoke(DSExcel, AddressOf CallBack, DSExcel)
    103.             'show the distraction form
    104.             'the showing of the form is thread 1 (the synchronous operation)
    105.             distraction = New FrmDistraction
    106.             distraction.ShowDialog()
    107.  
    108.             'execution continues here when the dataset is filled and displayed
    109.         End Sub
    110.  
    111.         ' this method is the start of the second thread : the asynchronous operation.  Am I correct?
    112.         Public Sub BeginFill(ByVal ds As DataSet)
    113.             Dim filenameNummer As String = GetSourcePath(Me.TextBoxNummer.Text)
    114.  
    115.             'here there is something wrong: suppose the user inputs a filenamenummer that doesn't exists,
    116.             'then the msgbox appears after the distraction form opens.
    117.             'When is the delegate method returning a value?
    118.             If File.Exists(filenameNummer) = True Then
    119.                 GetDataFromExcel(GetSourcePath(Me.TextBoxNummer.Text), "[blad1$A3:F3000]", ds)
    120.             Else
    121.                 MsgBox("Dossier " & Me.TextBoxNummer.Text & " bestaat niet.  Gelieve een ander dossiernr. in te geven", MsgBoxStyle.Critical, "")
    122.             End If
    123.         End Sub
    124.  
    125.         ' this method is the end of the second thread : the asynchronous operation.  Am I correct?
    126.         Public Sub EndFill(ByVal ds As DataSet)
    127.             'close the distraction dialog
    128.             distraction.DialogResult = DialogResult.OK
    129.             'handle results
    130.             If ds.Tables(0).Rows.Count > 0 Then
    131.                 Me.createstyles()
    132.                 Me.dataGrid1.DataSource = ds
    133.                 Me.dataGrid1.DataMember = "Excel"
    134.             Else
    135.                 MsgBox("No results", MsgBoxStyle.Critical, "")
    136.             End If
    137.         End Sub
    138.  
    139.         'creation of the callback method
    140.         Public Sub CallBack(ByVal ar As IAsyncResult)
    141.             Dim DS As DataSet = CType(ar.AsyncState, DataSet)
    142.             Dim del As New FillHandler(AddressOf EndFill)
    143.             'marshal back to the UI thread with the results
    144.             Me.Invoke(del, New Object() {DS})
    145.         End Sub

    The problem was that the callback method wasn't on the UI thread so it couldn't display the dataset. To solve it you just use Me.Invoke to marshal back to the UI thread.

  15. #15
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    To answer some of your questions:
    I think Asyncstate must be set to nothing, and not to DSExcel?? Or am i wrong? Can I use the DSExcel to pass as a variable?
    AsyncState is our return value, similiar to passing a variable to a sub ByRef to change the value. You can use DSExcel although there is no need to use a variable with full form level scope, but if you want to then that is fine too.

    Why do I need this dialogresult method? Can't I detect when the dataset filling operation has finished with the filled boolean or with the Endinvoke method of the delegate?
    Setting the dialogresult to ok is what closes the distraction form. Using Close should work too, but setting the value to a dialogresult will allow you to catch that result if its needed. There is no need for the filled variable either since the callback method will not be called until the operation is complete. If you want to double check the operation you can check the ar.IsComplete property. Also we never call EndInvoke we just let it fall out of scope. EndInvoke blocks a thread until the operation is complete which is not what we want.

    Also note: End Fill only needs the code to display the UI for the results. Execution will continue back at the line following distraction.ShowDialog() in the button.click event after endfill is done.

  16. #16

    Thread Starter
    Member
    Join Date
    Jan 2004
    Posts
    43
    thanks for taking a look at my code.

    I have only one remark:

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.             'DSExcel is a private variable declared
    3.             DSExcel.Clear()
    4.             'creation of an instance of the delegate
    5.             Dim del As New FillHandler(AddressOf FillDatasetThread)
    6.             'Can I use the DSExcel to pass as a variable?
    7.             del.BeginInvoke(DSExcel, AddressOf CallBack, DSExcel)
    8.             'show the distraction form
    9.             'the showing of the form is thread 1 (the synchronous operation)
    10.             distraction = New FrmDistraction
    11.             distraction.ShowDialog()
    12.  
    13.             'execution continues here when the dataset is filled and displayed
    14.         End Sub

    shouldn't this be:

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.             'DSExcel is a private variable declared
    3.             DSExcel.Clear()
    4.             'creation of an instance of the delegate
    5. 'replace Filldatasetthread by beginfill
    6.             Dim del As New FillHandler(AddressOf BeginFill)
    7.             'Can I use the DSExcel to pass as a variable?
    8.             del.BeginInvoke(DSExcel, AddressOf CallBack, DSExcel)
    9.             'show the distraction form
    10.             'the showing of the form is thread 1 (the synchronous operation)
    11.             distraction = New FrmDistraction
    12.             distraction.ShowDialog()
    13.  
    14.             'execution continues here when the dataset is filled and displayed
    15.         End Sub

    tomorrow i'll test the code and i'll keep you informed.

    kind regards

    inte

  17. #17
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Yes it should, good catch!

  18. #18

    Thread Starter
    Member
    Join Date
    Jan 2004
    Posts
    43
    @ Edneeis,

    As promised I tested the code you suggested and it works fine. However there is still something strange going on. If the user clicks button1 the first time, then the dataset is filled while the distraction form is appearing. After the fillup of the dataset, the distraction form disappears and the datagrid is filled up with the correct data. So far so good.

    The problem arises when the user clicks the button a second time (after the datagrid is filled). The distraction form appears and the dataset is filled up. Also the datagrid is filled up, again with the correct results, but the distraction form doesn't disappear (even after the filling up of the datagrid). Because of this the program crashes (you can't do anything anymore).

    Any idea why this is so. I think it has something to do with me.invoke(..) but I can't figure out what.

    Can you help me?

    Thanks

    Jinte (a desperate newby)

  19. #19
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I'm not sure it works fine for me. I tried several times but could not reproduce the problem. I was able to successfully run the fill bit several times. Here is my test example (see attached). I had it exactly like yours, using a dataadapter and connection created at designtime but switched to an xml file to make it more portable.

    Me.Invoke simply invokes the delegate passed in on the same thread as the form (ME).

  20. #20
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Here is a better animation if you don't want to use the clock. Just replace the clock image with this one on your distraction form.


  21. #21
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Hey I was thinking, this example and the code exposed came about to make it appear to show progress on a lengthy process, but I wanted to point out that the same code (minus the distraction form) will allow the user to continue using your app and complete the process in the background. If you want the user to be able to interact and work in your application while the dataset is being filled then just don't show the distraction form. The only thing in the current code stopping the user from interaction is the fact that the distraction form is shown modally (ShowDialog). So I figured I'd let you know that you have that option if you prefer it.

  22. #22

    Thread Starter
    Member
    Join Date
    Jan 2004
    Posts
    43
    @Edneeis

    I compiled your solution and it worked like it should. I'll try to find the difference with my solution, so hopefully it will work then.

    Thanks for your time,

    Jinte

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width