0

while scanning thru the datable I want the cursor to be a wait cursor and showing the progress of the scan

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Cursor.Current = Cursors.WaitCursor
    For k = 0 To vCountries.Count - 1
        If k < DgView1.FirstDisplayedScrollingRowIndex OrElse k > DgView1.FirstDisplayedScrollingRowIndex +
                                                                  DgView1.DisplayedColumnCount(False) Then
            DgView1.FirstDisplayedScrollingRowIndex = k
        End If
        DgView1.Rows(k).Cells(CoName).Style.BackColor = Color.GreenYellow
        Application.DoEvents()
        ScannerPageOne(vCountries.Item(k).Row)
        DgView1.Rows(k).Cells(CoName).Style.BackColor = DgView1.DefaultCellStyle.BackColor
    Next
    Cursor.Current = Cursors.Default
End Sub

The cursor exits the wait because of the Application.DoEvents but when I remove it nothing is showing on the screen.

How can I have a WaitCursor and the DatagridView showing Progress?

As suggested I used the BW. Here is the code:

WithEvents BW As New BackgroundWorker With {.WorkerReportsProgress = True}
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Cursor.Current = Cursors.WaitCursor
        BW.RunWorkerAsync(vCountries)
    End Sub
    Private Sub BW_DoWork(sender As Object, e As DoWorkEventArgs) Handles BW.DoWork
        Dim bw As BackgroundWorker = CType(sender, BackgroundWorker)
        Dim vCountries As DataView = e.Argument
        For k = 0 To vCountries.Count - 1
            bw.ReportProgress(k)
            ScannerPageOne(vCountries.Item(k).Row)
        Next
    End Sub
    Private Sub BW_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) _
        Handles BW.RunWorkerCompleted
        Cursor.Current = DefaultCursor
    End Sub
    Private Sub BW_ProgressChanged(ByVal sender As System.Object, ByVal e As ProgressChangedEventArgs) _
        Handles BW.ProgressChanged
        If e.ProgressPercentage > 0 Then
            DgView1.Rows(e.ProgressPercentage - 1).Cells(CoName).Style.BackColor = DgView1.DefaultCellStyle.BackColor
        End If
        DgView1.Rows(e.ProgressPercentage).Cells(CoName).Style.BackColor = Color.GreenYellow
    End Sub

Using the BW painted the lines in DGV but the cursor was in normal state.

shemmy
  • 7
  • 4
  • 1
    You could use a BackgroundWorker, use the [`RunWorkerAsync([object])`](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.backgroundworker.runworkerasync) overload to pass to the `DoWork` event your `vCountries` list, loop the list to call `ScannerPageOne(vCountries.Item(k).Row)` and updated the DataGridView passing to the BGW's [ProgressChanged](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.backgroundworker.progresschanged) event the results of the current elaboration. Do use `Application.DoEvents()`. – Jimi Feb 08 '20 at 21:50
  • 1
    You can then set `Cursor.Current = Cursors.Default` in the [RunWorkerCompleted](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.backgroundworker.runworkercompleted) event. Btw, `Cursor.Current = Cursors.WaitCursor` should change the Cursor immediately, the loop should begin after the cursor has changed. What Framework version are you using? – Jimi Feb 08 '20 at 22:00
  • Thanks for the replies but it didn't help. I use Framework .7.2 – shemmy Feb 09 '20 at 09:29
  • In another application, with similar logic it works. The big difference is the number of columns in the table. This app has 32 columns as the other has 5. Could it be the reason? – shemmy Feb 09 '20 at 09:43
  • I assume you meant .Net Framework 4.7.2. What *works* or *doesn't work* is undefined, since you didn't explain what does or does not *work*. If you have modified your code, you have to update your question with the new code, The BackGroundWorker is a well-know, overly-tested, perfectly-working tool. It's as if I suggested to *use a hammer* and you're saying *it doesn't work*. The hammer *works*, if it doesn't *work* for you, there's a good chance you're using it the wrong way. But you're not showing how you're using this tool. – Jimi Feb 09 '20 at 13:10
  • I am working at Framework 4.7.2. I tried the background worker as you suggested and the lines are painted as the work progressed however the cursor doesn't show a wait status. I would like to add the code but it exceeds the allowed. – shemmy Feb 09 '20 at 13:52
  • You don't have to post your entire project. Just the lines of code that reproduce this behavior: where/when/how you set the Cursor (of course, not inside the `DoWork` event), where/when/how you create and call `.RunWorkerAsync([object])`, how you raise and use the ProgressChanged event. BTW, the number of Columns/Rows of a DGV is irrelevant. – Jimi Feb 09 '20 at 13:58
  • My compliments :) You're using the BackGroundWorker correctly (well, from what I can see: I don't know what `ScannerPageOne()` is doing). Anyway, in this use case, set `Application.UseWaitCursor = true` after `RunWorkerAsync()` and of course `Application.UseWaitCursor = false` in the `BW_RunWorkerCompleted` handler. If you're using a Button to run the BGW, disable the Button before running the BGW and re-enable it when the procedure is complete. – Jimi Feb 09 '20 at 15:57

0 Answers0