Hi rowe,
i had an idea. I would like to use a temporary table into DB to store
the result of a query.
Then i'll call the Sub that populate Listview reading data from the
table. The process could be much more time expensive, but I would not
have any problem accessing to Form Object from Async process.
What do you think?
Thanks
Here's another sample, It assumes a listview with the "listView1"
exists on the form. It'll also need the OleDb object properties set
properly before it'll work but other than that it should be good to
go.
Basically, it passes a DataTable around that will hold the values you
need from the long running OleDb actions. The sample uses the
OleDbDataAdapter to fill the table.
////////////////////
Option Strict On
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Private worker As WorkerDelegate
Public Delegate Sub WorkerDelegate(ByVal table As DataTable)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim table As New DataTable()
worker = New WorkerDelegate(AddressOf AsyncMethod)
worker.BeginInvoke(table, New AsyncCallback(AddressOf
AfterAsyncMethod), table)
End Sub
Private Sub AfterAsyncMethod(ByVal ar As IAsyncResult)
worker.EndInvoke(ar)
Dim table As DataTable = DirectCast(ar.AsyncState, DataTable)
If Me.InvokeRequired Then
Dim formWorker As New WorkerDelegate(AddressOf
UpdateListView)
Me.BeginInvoke(formWorker, table)
Else
UpdateListView(table)
End If
End Sub
Private Sub AsyncMethod(ByVal table As DataTable)
Using conn As New OleDbConnection("connection string")
Using com As OleDbCommand = conn.CreateCommand()
conn.Open()
com.CommandType = CommandType.StoredProcedure
com.CommandText = "getSomething"
com.Parameters.Add("@SomeParam",
OleDbType.VarChar).Value = "some value"
Using da As New OleDbDataAdapter(com)
da.Fill(table)
End Using
End Using
End Using
End Sub
Private Sub UpdateListView(ByVal table As DataTable)
Me.Text = "Updated"
For Each row As DataRow In table.Rows
'// add the items to the listview
Me.listView1.Items.Add(row(0).ToString())
Next
End Sub
End Class
///////////////////
Thanks,
Seth Rowe [MVP]