VB.net Cross-thread operation not valid [duplicate] - vb.net

This question already has answers here:
Crossthread operation not valid... - VB.NET
(8 answers)
Closed 8 years ago.
I am trying my best to figure out how to go about this error i am reciving:
Cross-thread operation not valid: Control 'ListView1' accessed from a thread other than the thread it was created on.
I have a backgroundworker thats extracting cells from an excel worksheet and placing them into the listview.
Upon form load i do this:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call createListView()
End Sub
Private Sub createListView()
ListView1.View = View.Details
ListView1.GridLines = True
ListView1.FullRowSelect = True
ListView1.HideSelection = False
ListView1.MultiSelect = False
ListView1.Columns.Add("Column Name", 150)
ListView1.Columns.Add("Column Number", 150)
End Sub
Then i call the backgroundworker after the user selects a file:
If openFileDialog1.ShowDialog() = DialogResult.OK Then
stFilePathAndName = openFileDialog1.FileName
ProgressBar1.Style = ProgressBarStyle.Marquee
BGWxml2excel = New System.ComponentModel.BackgroundWorker
BGWxml2excel.WorkerReportsProgress = True
BGWxml2excel.WorkerSupportsCancellation = True
BGWxml2excel.RunWorkerAsync()
End If
Then i process with getting the excel column count and values so that i can populate the listview with it:
Private Sub BGWxml2excel_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BGWxml2excel.DoWork
Call xml2Excel(stFilePathAndName)
End Sub
Private Sub xml2Excel(ByRef theDirOfFile As String)
Dim xlsApp As Excel.Application
Dim xlsWB As Excel.Workbook
Dim xlsSheet As Excel.Worksheet
Dim columnCount As Integer = 0
xlsApp = New Excel.Application
xlsApp.Visible = False
xlsApp.DisplayAlerts = False
xlsWB = xlsApp.Workbooks.OpenXML(Filename:=theDirOfFile, LoadOption:=XlXmlLoadOption.xlXmlLoadImportToList)
xlsSheet = xlsWB.Worksheets(1)
xlsSheet.Select()
columnCount = xlsSheet.UsedRange.Columns.Count
Dim lvi As New ListViewItem
Dim x As Integer = 1
Do Until x = columnCount + 1
lvi.Text = xlsSheet.Cells(1, x).value
lvi.SubItems.Add(x)
ListView1.Items.Add(lvi)
x = x + 1
Loop
'xlsSheet.SaveAs("c:\_tempExcelFile.xlsx", FileFormat:=51, CreateBackup:=False)
xlsWB.Close()
xlsApp.Quit()
End Sub
The error is on this line:
ListView1.Items.Add(lvi)
What can i do in order to correct this odd proglem?
Thanks!
David

The problem is that only the UI thread can update the UI. Because you are adding an item to the ListView from your worker, you are getting this exception.
To fix this, you could store items that you want to add to the listview in a shared variable (one to which both the UI thread and your worker have access), put a timer on your form (so the UI threads hits the tick event handler) and add the items within the tick handler.
Watered-down example (in C#, because it's a lot faster for me :-) ):
private List<ListViewItem> _itemsToBeAdded = new List<ListViewItem>();
private readonly object _lockObject = new object();
// worker method:
private void xml2Excel(string input)
{
// do some processing...
ListViewItem lvi = new ListViewItem();
// set up lvi
lock(_lockObject)
{
_itemsToBeAdded.Add(lvi);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
lock(_lockObject)
{
foreach(var item in _itemsToBeAdded)
{
ListView1.Add(item);
}
}
}

Only the UI thread can access the UI. Your background worker is on another thread. You'll need to use .InvokeRequired/.Invoke to get back to the UI thread.
http://msdn.microsoft.com/en-us/library/ms171728(v=vs.80).aspx
The event handler's for the background worker will be raised on the main thread; you can safely alter the UI there. But actually in the background thread, you'll have to invoke. Like this:
Delegate Sub SetTextCallback([text] As String)
Private Sub SetText(ByVal [text] As String)
' InvokeRequired required compares the thread ID of the
' calling thread to the thread ID of the creating thread.
' If these threads are different, it returns true.
If Me.textBox1.InvokeRequired Then
Dim d As New SetTextCallback(AddressOf SetText)
Me.Invoke(d, New Object() {[text]})
Else
Me.textBox1.Text = [text]
End If
End Sub

Related

VB Simple Threading using Delegates

I understand the concept of threading. I understand the concept of delegates but I am having trouble combining the two concepts. I followed a tutorial and I was able to make two counters start at the same time using multiple threads on my form. I was getting the cross threading error and I used the Me.CheckForIllegalCrossThreadCalls = False work around. I know my current method isnt ideal and I was wondering how I would use delegates to produce the same results. I have been at it all day and still cant seem to grasp the idea. How would I add delegates to the code below to allow two counters to work simultaneously on my form?
Public Class Form1
Dim i As Integer = 0
Dim i2 As Integer = 0
'declare two threads
'thread 1
Dim thread As System.Threading.Thread
'thread 2
Dim thread2 As System.Threading.Thread
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'replace countup() with, this will assign the countup method to thread 1
thread = New System.Threading.Thread(AddressOf countup)
thread.Start()
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
' countup2()
thread2 = New System.Threading.Thread(AddressOf countup2)
thread2.Start()
End Sub
Private Sub countup()
Do Until i = 100000
i = i + 1
Label1.Text = i
'We wont be able to see the label unless we refresh the form
Me.Refresh()
Loop
End Sub
Private Sub countup2()
Do Until i2 = 100000
i2 = i2 + 1
Label2.Text = i2
'We wont be able to see the label unless we refresh the form
Me.Refresh()
Loop
End Sub
End Class
I would love to see the code using delegates but what I would really like is to have the understanding of whats going on.
Thanks guys
Not sure if this is exactly what you're looking for, but here's my best shot at it:
Module Module1
Dim i As Integer = 0
Dim i2 As Integer = 0
Public Delegate Sub counting()
Sub Main()
Dim del2 As counting = AddressOf countup2
Dim callback2 As IAsyncResult = del2.BeginInvoke(Nothing, Nothing)
Dim del1 As counting = AddressOf countup
Dim callback1 As IAsyncResult = del1.BeginInvoke(Nothing, Nothing)
del2.EndInvoke(callback2)
del1.EndInvoke(callback1)
Console.ReadLine()
End Sub
Private Sub countup()
Do Until i = 100000
i = i + 1
Loop
Console.WriteLine("i = " & i)
End Sub
Private Sub countup2()
Do Until i2 = 100000
i2 = i2 + 1
Loop
Console.WriteLine("i2 = " & i2)
End Sub
End Module
Sorry I have the first and second parts reversed and it's a console app instead of a form, but I figured the important part was to demonstrate delegates...
As a note, I'm not sure how familiar you are with delegates, but I included the EndInvoke to make sure the program wouldn't terminate prior to the delegates finishing their operations. They are used to return any values or exceptions from the method call as well as making the program wait. (In this case, since it's a sub there is no return value, so I didn't bother worrying about it)
One should use Control.Invoke to execute a specified delegate on the thread that owns the control's underlying window handle. Also, replace Me.Refresh() with Thread.Sleep(1) to ensure that other threads get some execution time.
Private Sub countup()
For i As Integer = 0 To 100000
Me.Invoke(Sub() Me.Label1.Text = i.ToString())
Thread.Sleep(1)
Next
End Sub
Here's an example.
' n=0 n=1
Private threads As Thread() = New Thread(2 - 1) {Nothing, Nothing}
Private Sub ButtonsClick(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
Dim n As Integer = -1
If (sender Is Me.Button1) Then
n = 0
ElseIf (sender Is Me.Button2) Then
n = 1
End If
If (n <> -1) Then
If (Me.threads(n) Is Nothing) Then
'Start new thread.
Me.threads(n) = New System.Threading.Thread(Sub() Me.CountUp(n))
Me.threads(n).Start()
Else
'Abort thread.
Me.threads(n).Abort()
Me.threads(n) = Nothing
End If
End If
End Sub
Public Sub DisplayCount(n As Integer, text As String)
'Inside UI thread.
If (n = 0) Then
Me.Label1.Text = text
ElseIf (n = 1) Then
Me.Label2.Text = text
End If
End Sub
Private Sub CountUp(n As Integer)
'Inside worker thread.
Try
If ((n < 0) OrElse (n > 1)) Then
Throw New IndexOutOfRangeException()
End If
For i As Integer = 0 To 100000
Me.Invoke(Sub() Me.DisplayCount(n, i.ToString()))
Thread.Sleep(1)
Next
Catch ex As ThreadAbortException
Me.Invoke(Sub() Me.DisplayCount(n, "Cancelled"))
Thread.Sleep(1)
Catch ex As Exception
'TODO: Handle other exceptions.
End Try
End Sub
using Me.CheckForIllegalCrossThreadCalls = False is not the right approach.
Basically, Cross-thread operation not valid exception is raised when a control is being updated from a thread other than the thread it was created on.
Each control exposes a InvokeRequired property that allows it to be updated in a thread-safe manner.
Therefore the right way to update the label is to use code like -
Private Delegate Sub UpdateLabelDelegate(i As Integer)
Private Sub UpdateLabel(i As Integer)
If Label1.InvokeRequired Then
Dim del As New UpdateLabelDelegate(AddressOf UpdateLbl)
Label1.Invoke(del, New Object() {i})
'Me.Refresh()
Else
' this is UI thread
End If
End Sub
Private Sub UpdateLbl(i As Integer)
Label1.Text = i.ToString()
End Sub
Delegate.BeginInvoke will execute the method on a thread pool thread. Once the method returns, the thread is returned to the pool.
So basically instead of starting a new thread, you will asynchronously execute the method using Delegate.BeginInvoke

after using background worker also my application get stuck,,how i can resolve

I am working on windows form application :
in my form am filling my datagrid view in some interval.so some time my application getting stuck..so i used back ground worker and timer
in back ground worker i am calling my function to fill the my data grid view.i set Timer Interval as 10000. in background worker i given code like this:
Private Sub BackgroundWorker1_DoWork
Call Fetch_Info()
End Sub
in Timer click event i given code like thise:
If Not BackgroundWorker1.IsBusy Then
BackgroundWorker1.RunWorkerAsync()
End If
my Fetch_Info() function like this:
Dim cnt As Integer
Dim tbarcodedgv As String
Dim totaltbarcode As String
cnt = DGVall.RowCount
Dim tbar As String
Dim locTable As New DataTable
locTable.Columns.Add("carid", GetType(String))
If cnt > 0 Then
For i = 0 To cnt - 2
tbarcodedgv = DGVall.Rows(i).Cells(0).Value
locTable.Rows.Add(tbarcodedgv)
Next
End If
Dim flag As Boolean = False
Dim dcnt As Integer = DGVall.RowCount
Dim trid As Integer
Dim tbarcode As String
Dim keyloc As String
Dim cmd23 As New SqlCommand("IBS_fetchrequested", con.connect)
cmd23.CommandType = CommandType.StoredProcedure
cmd23.Parameters.Add("#tid", SqlDbType.Int).Value = tid
If cnt > 1 Then
Dim tvp1 As SqlParameter = cmd23.Parameters.AddWithValue("#Tbaroced", locTable)
tvp1.SqlDbType = SqlDbType.Structured
tvp1.TypeName = "dbo.TBarcode"
End If
dr = cmd23.ExecuteReader
While dr.Read
flag = False
tbarcode = dr("TBarcode")
If flag = False Then
If dr("keyloc") Is DBNull.Value Then
keyloc = ""
Else
keyloc = dr("keyloc")
End If
Dim row0 As String() = {tbarcode, keyloc, "", "Release"}
DGVall.Rows.Add(row0)
AxWindowsMediaPlayer1.URL = "C:\Beep.mp3"
End If
End While
dr.Close()
con.disconnect()
While your background worker runs in another thread than your GUI you are manipulating the Datagridview that's running in the GUI's thread. This should usually not work at all but it is probably the reason, why your GUI hangs while the BGW is running.
Try splitting the work: The time consuming fetching of data from the database is carried out in the Backgroundworker's DoWork event handler and you set the results as the e.Result value of the EventArgs variable in the DoWork function.
Then you handle the Backgroundworker's RunWorkerCompleted event and there you quickly update your datagridview with the results you set in the DoWork method. That way your GUI has nothing to do with the actual time consuming task and will only be affected by the quick update of your datagridview.
The code example for this is:
Public Class Form1
Private WithEvents LazyBGW As New System.ComponentModel.BackgroundWorker
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'This code runs in the UI-Thread
LazyBGW.RunWorkerAsync()
End Sub
Private Sub LazyBGW_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles LazyBGW.DoWork
'This code runs in the BGW-Thread
Dim a As Integer = 0
For i = 1 To 5
a += 1
'I'm a lazy worker, so after this hard work I need to...
Threading.Thread.Sleep(1000) 'This locks up the BGW-Thread, not the UI-thread
Next
'Work is done, put results in the eventargs-variable for further processing
e.Result = a
End Sub
Private Sub LazyBGW_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles LazyBGW.RunWorkerCompleted
'This code runs in the UI-Thread
Dim results As Integer = CInt(e.Result) 'e.Result contains whatever you put into it in the DoWork() method
MessageBox.Show("Finally the worker is done and our result is: " & results.ToString)
End Sub
End Class

How can I respond to events raised by programmatically created UI elements?

I'm creating a board game for a piece of coursework. For the board, I'm using some nested For loops running through a 2D array to generate a "Space" object at each square.
The Space object contains a picturebox and some data about that space.
How can I handle events caused by clicking on the generated picturebox without having to hard-code it for each space?
I noticed this question seems to address this, but it's in C# and I couldn't translate it to VB.Net.
Edit:
This is how the board is generated
Dim board(23, 24) As Space
Private Sub GenerateBoard()
Dim spaceSize As New Size(30, 30)
Dim spaceLocation As New Point
Dim validity As Boolean
For Y = 0 To 24
For X = 0 To 23
spaceLocation.X = 6 + (31 * X)
spaceLocation.Y = 6 + (31 * Y)
If validSpaces(Y).Contains(X + 1) Then
validity = True
Else
validity = False
End If
board(X, Y) = New Space(validity, spaceSize, spaceLocation)
Me.Controls.Add(board(X, Y).imageBox)
board(X, Y).imageBox.BackColor = Color.Transparent
board(X, Y).imageBox.BringToFront()
Next
Next
End Sub
Space Class:
Public Class Space
Dim _active As Boolean
Dim _imageBox As PictureBox
Public Sub New(ByVal activeInput As Boolean, ByVal size As Size, ByVal location As Point)
_active = activeInput
_imageBox = New PictureBox
With _imageBox
.Size = size
.Location = location
.Visible = False
End With
End Sub
Property active As Boolean
Get
Return _active
End Get
Set(value As Boolean)
_active = value
End Set
End Property
Property imageBox As PictureBox
Get
Return _imageBox
End Get
Set(value As PictureBox)
_imageBox = value
End Set
End Property
Public Sub highlight()
With _imageBox
.Image = My.Resources.Highlighted_slab
.Visible = True
End With
End Sub
End Class
First all controls created by designer(textbox, label...) a generated by code too, but VisualStudio write this for you. If you open Designer file(yourForm.Designer.vb), then you can see all code how to generate a controls.
If you want a create event handler for your pictureBox , then:
//Initialize control
Private WithEvents _imageBox as PictureBox
Then create a event handler method:
Private Sub imageBox_Click(sender as Object, e as EventArgs)
//Your code
End Sub
Then in VB.NET you can assign a Event handler to the Event in two ways
first: In class constructor after you created a pictureBox( New PictureBox()) add
AddHandler Me._imageBox, AddressOf Me.imageBox_Click
second: On line we you created a event handler add next:
Private Sub imageBox_Click(sender as Object, e as EventArgs) Handles _imageBox.Click
//Your code
End Sub
And remember add your pictureBox to form controls YourForm.Controls.Add(spaceInstance.ImageBox)

How to pass listbox into background worker in VB .Net 2010

I am trying to loop through a listbox which contains filenames and upload them to an FTP server with a background worker. I am getting a cross-thread exception at my for loop when I attempt to access Listbox1.Items.Count within background worker (obviously because it's on a different thread) so I'm curious how I can pass the listbox into my background worker to execute the code they way I have written it below?
Private Sub bgw_upAllFiles_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw_upAllFiles.DoWork
Dim i
Dim toPath As String = MyForms.MoveOutFTPFormDir & PDFVar_PHOTO_URL_NUM & "/"
For i = 0 To e.Argument.Items.Count - 1
Try
retryDL:
My.Computer.Network.UploadFile(ListBox1.Items(i).ToString, toPath & IO.Path.GetFileName(ListBox1.Items(i).ToString), MyForms.MoveOutFTPUser, MyForms.MoveOutFTPPwd)
Catch ex As Exception
If ex.ToString.Contains("error: (550)") Then
'MsgBox("Need to create FTP folder")
Try
Dim myftprequest As Net.FtpWebRequest = CType(Net.FtpWebRequest.Create(toPath), System.Net.FtpWebRequest)
myftprequest.Credentials = New System.Net.NetworkCredential("JeffreyGinsburg", "andy86")
myftprequest.Method = System.Net.WebRequestMethods.Ftp.MakeDirectory
myftprequest.GetResponse()
GoTo retryDL
Catch ex2 As Exception
ex2.ToString()
End Try
Else
MsgBox(ex.ToString)
End If
MDIParent1.StatusStrip.Items.Item(2).Text = "Upload Complete"
End Try
Next
End Sub
When you call RunWorkerAsync, you are able to pass an object as a parameter. you could use this object and pass in your DDL.
Then, in the DoWork event, you can make use of the DDL like so:
Dim ddl = CType(e.Arugment, DropDownList)
BackgroundWorker.RunWorkerAsync Method
Pass the items to the backgroundworker as a string array:
BackgroundWorker1.RunWorkerAsync(ListBox1.Items.Cast(Of String).ToArray)
Then iterate that array in the dowork sub:
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim files As String() = DirectCast(e.Argument, String())
For Each file As String In files
'My.Computer.Network.UploadFile(file, ......
Next
End Sub
you have two choices:
Run on a different thread:
worker.RunWorkerAsync(Listbox1.Items.Cast().ToList())
then use:
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
var items = e.Argument as List<string>;
}
or you call the action on the main thread:
ListBox1.Invoke(new Action(() =>
{
var items = ListBox1.Items;
}));

DatagridView crashes App when updating causes scrollbars to appear

My vb.net 4.0 project is in-house trading platform.
A separate form called MsgPad contains a Datagridview called MsgPadDG.
The Form-load sub does two things: formats the datagridview and defines a timer for the gridview refresh.
A second Sub, UpdateMyMsg, receives updates from a threaded UIUpdaterEngine by means of NewMessage events, and updates the MyMessages Object (fundamentally a datatable).
A third sub, UpdateMdgPadDGW, updates/refreshes the datagridview using the invoke method (probably not necessary, because the timer is defined in the UI thread).
Finally The Datagridview properties: rows are defined as not user-editable, AutoSizeRowsMode = Displayedcells and ScrollBars = Both.
My problem is that the entire App crashes when the updating process adds rows beyond the limit of the datagridview, causing scrollbars to appear.
I tried without the timer (invoking datasource update directly in the UpdateMyMsg Sub), with the timer, but the problem is always there.
Weird thing: The Try-Catch block doesn't catch anything.
Thanks in advance for your replays.
Edoardo
HERE COMES THE CODE:
Public Class MsgPad
Private WithEvents _MyUIUpdaterEngine As MyUIUpdaterEngine = MyUIUpdaterEngine.Instance
Private MyMessages As New Messages
Private LastUpdate As DateTime = TimeValue("08:00:00")
Private ObjLock As New Object
Private NewMessages As Boolean = False
Dim UpdateDGWTimer As New System.Timers.Timer
Private Sub MsgPad_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Try
'--
MsgPadDG.DataSource = MyMessages.DTable
'--
With MsgPadDG
'--
.Columns("Msg_ID").Visible = False
.Columns("I_CommandID").Visible = False
.Columns("B_ID").Visible = False
.Columns("F_ID").Visible = False
.Columns("T_TV").Visible = False
.Columns("T_Sign").Visible = False
.Columns("T_Mde").Visible = False
.Columns("T_QU").Visible = False
.Columns("T_QI").Visible = False
.Columns("T_QF").Visible = False
.Columns("T_PI").Visible = False
.Columns("T_PF").Visible = False
'--
.Columns("Msg_RecDate").HeaderText = "Date"
.Columns("Msg_RecDate").Width = 50
.Columns("Msg_RecDate").DefaultCellStyle.Format = "HH:mm:ss"
.Columns("I_Symbol").HeaderText = "Symbol"
.Columns("I_Symbol").Width = 80
.Columns("I_Des").HeaderText = "Des"
.Columns("I_Des").Width = 180
.Columns("Msg_Text").HeaderText = "Message"
.Columns("Msg_Text").Width = 250
'--
End With
'--Crea timer per l'Update del DAtagridView
AddHandler UpdateDGWTimer.Elapsed, AddressOf UpdateMsgPadDGW
UpdateDGWTimer.Interval = 3500
UpdateDGWTimer.Enabled = True
.....
.....
End Sub
Private Sub UpdateMyMsg(ByVal CRec As OrderRecord, ByVal Msg As String) Handles _MyUIUpdaterEngine.NewMessage
Try
SyncLock ObjLock
'--Aggiorna la tabella MyMessages
MyMessages.Add(CRec, Msg)
NewMessages = True
'--Parla messaggio
Dim synth As New SpeechSynthesizer
Dim TalkMsg As String = Msg
If CRec.I_Des <> Nothing Then TalkMsg += " su: " + CRec.I_Des
synth.SpeakAsync(TalkMsg)
End SyncLock
.....
.....
End Sub
Private Sub UpdateMsgPadDGW(source As Object, e As ElapsedEventArgs)
'--Aggiorna MesssagePad
Try
SyncLock ObjLock
If NewMessages Then
MsgPadDG.ControlInvoke(Sub(l)
MsgPadDG.DataSource = MyMessages.DTable
MsgPadDG.Refresh()
MsgPadDG.ClearSelection()
End Sub)
NewMessages = False
End If
End SyncLock
.....
.....
End Sub
'--Aggiorna la tabella MyMessages
MyMessages.Add(CRec, Msg)
NewMessages = True
That's a fatal bug, the DGV is bound to MyMessages. So calling Add() in a worker thread causes the control to be updated from the wrong thread. You normally get an IllegalOperationException from accessing a control from the wrong thread but that unfortunately doesn't work for bound data.
You'll need to do this differently, have the worker add messages to a List instead. Then in the invoked code update MyMessages with these new messages.
Also note that your Timer is not a safe timer like you assumed in your question. Only a System.Windows.Forms.Timer is a safe one whose Tick event handler will run on the UI thread. You then also don't need to invoke anymore.