Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB.NET 2005 Multithreading / Cross threading operation not valid

Status
Not open for further replies.

hugh999

MIS
Nov 29, 2001
129
IE
Hi
I am trying to introduce Multithreading in to my program so that the program remains responsive but am getting the error Cross threading operation not valid.

My program will execute a batch file on a list of files that I have added to a ListBox. When the batch file finishes working on the first file in the list box I would like the program to increment a number on a label to show that the batch file has finished working on the file and then move on to the next file in the ListBox.

Here is the code I am working with under a button control:
Code:
Dim t As Threading.Thread
t = New Threading.Thread(AddressOf Me.Test1)
t.Start()

Here is the code I have under the Test1 sub:
Code:
Dim i As Integer
For i = 0 To ListBox1.Items.Count – 1
oWrite = IO.File.CreateText("c:\verify.bat")
‘Btach file commands
oWrite.Close()

Dim p As Process = New Process
p.StartInfo.FileName = ("c:\verify.bat")
p.StartInfo.UseShellExecute = False
p.StartInfo.CreateNoWindow = True
p.Start()
p.WaitForExit()

Dim down As Integer = Label5.Text + 1
Label5.Text = down
Label5.Refresh()
            
Next
I have been looking on the net and trying my hand at few examples but getting no where. Does anyone know of how are where I need to add cross threading code so that I can add a value to the label; control.

Thanks for your help

 
You need this in a separate function

Code:
private delegate setlabeldel()

private function setlabel()
  if label5.invokerequired then
    me.invoke(new setlabeldelegate(addressof setlabel,new object() {})
  else
    Dim down As Integer = Label5.Text + 1
    Label5.Text = down
    Label5.Refresh()
  end if
end function

Yhis code is untested so bound not to work, if it does then good for you.

Christiaan Baes
Belgium

My Blog
 
Thanks for the code. I copied it to my form but am getting the message Sub or Function expected after "delegate" for the line private delegate setlabeldel()

Is there a specific location where i should copy the code to.

Thanks
 
because it should be

private delegate function setlabeldel()

Christiaan Baes
Belgium

My Blog
 
Hi

I have declared it as a function private delegate function setlabeldel() but the line me.invoke(new setlabeldelgate(addressof setlabel,new object() {})is telling me that the text setlabeldelgate is not defined

I appreciate your help
 
I think you could have figured that one out by yourself but here goes.

me.invoke(new setlabeldel(addressof setlabel,new object() {})

Christiaan Baes
Belgium

My Blog
 
Hi

I have tried a few scenarios with the code but am getting the message
Setlabeldel is a delegate type and requires a single ‘addressof’ expression as the only argument to the constructor for the line of code me.invoke(new setlabeldel(addressof setlabel,new object() {})
 
Ok I was close.

me.invoke(new setlabeldel(addressof setlabel),new object() {})

very close.

Christiaan Baes
Belgium

My Blog
 
Hi Christiaan

Thanks for your help with this. One last question, where in my code should i copy this fucntion to or where in my code should i call the function.

Thanks
 
It is OK, i discoverd where to call the function from.

Thanks for your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top