Windows Develop Bookmark and Share   
 index > Windows Forms General > cross thread & forms
 

cross thread & forms

Hi

I know this has been talked about 1000 times, but I still have some issues on how to use this Invoke method to update forms from a cross thread.

I have some class running ina thread. Whenever a display update is required the class thread will call the delegate provided during class creation. Unfortunately I get the cross thread exception and I wasn't able to successfully apply the Invoke method to solve my problem. This is probably because I don't knowhow to apply Invoke. I was trying to apply samples given throughout the forums but no success. Is there anybody that can help?

Best regards

daniel

In my class B I have somehting like this

Public Delegate Sub delegate_updatedisplay(byval s as string)

class B

sub New(x as delegate_updatedisplay)

..... register disply update callback

end sub

end class

So, in my form A, I have:

dim x as new B(addressof updatedisplay)

public sub updatedisplay(byval s as string)

.... do my form update here

end sub

dsula  Thursday, July 20, 2006 10:01 PM

I know how to do this in C# but not VB.....

maybe someone would like to convert it to VB?

 

However I can give you a link to an article, even though it maybe C# code its not hard to convert to VB.NET, I hope this helps: 

http://msdn.microsoft.com/msdnmag/issues/03/02/Multithreading/ 

 

take a look at this, the last post has the code to pretty much do what you are asking. Take a look at the invoke methods/delegates

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=212767&SiteID=1

 

specifically:

 

Delegate Sub writetext(ByVal text As String)

Public Sub Working()

For i As Integer = 0 To 10

Me.Writing(i.ToString())

Thread.Sleep(sleeper)

Next i

End Sub

Public Sub Writing(ByVal text As String)

If (Me.rtb.InvokeRequired) Then

Dim f As writetext = New writetext(AddressOf Writing)

rtb.Invoke(f, text)

Else

Me.rtb.AppendText(text & vbNewLine)

End If

End Sub

 

 

To call, make sure you do Me.Writing(string) in this example.

ahmedilyas  Thursday, July 20, 2006 10:40 PM

I know how to do this in C# but not VB.....

maybe someone would like to convert it to VB?

 

However I can give you a link to an article, even though it maybe C# code its not hard to convert to VB.NET, I hope this helps: 

http://msdn.microsoft.com/msdnmag/issues/03/02/Multithreading/ 

 

take a look at this, the last post has the code to pretty much do what you are asking. Take a look at the invoke methods/delegates

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=212767&SiteID=1

 

specifically:

 

Delegate Sub writetext(ByVal text As String)

Public Sub Working()

For i As Integer = 0 To 10

Me.Writing(i.ToString())

Thread.Sleep(sleeper)

Next i

End Sub

Public Sub Writing(ByVal text As String)

If (Me.rtb.InvokeRequired) Then

Dim f As writetext = New writetext(AddressOf Writing)

rtb.Invoke(f, text)

Else

Me.rtb.AppendText(text & vbNewLine)

End If

End Sub

 

 

To call, make sure you do Me.Writing(string) in this example.

ahmedilyas  Thursday, July 20, 2006 10:40 PM

Hi,

Thank you verymuch for the articles. I havent's seen those yet. I have seen and followed all the exampled of others post, though. To take the example from above. I make some modifications.

What I want is:

1. To be able to provide a callback to function 'Working'

2. The callback function should not need to know if it is called by othere threads. It should just work, and the threading "Invoke" stuff should happen in "Working"

Delegate Sub writetext(ByVal text As String)

Public Sub info(ByVal richbox As RichTextBox, ByVal sleeping As Integer)

Me.rtb = richbox

Me.sleeper = sleeping

End Sub

// next line changed

Public Sub Working(byval w as writetext)

For i As Integer = 0 To 10

// next line changed

w(i.ToString())

Thread.Sleep(sleeper)

Next i

End Sub

Public Sub Writing(ByVal text As String)

// I don't want to deal with this"invoke" here.I'd like to hide it into function"working"

If (Me.rtb.InvokeRequired) Then

Dim f As writetext = New writetext(AddressOf Writing)

rtb.Invoke(f, text)

Else

Me.rtb.AppendText(text & vbNewLine)

End If

End Sub

dsula  Thursday, July 20, 2006 11:00 PM

You can use google to search for other answers

Custom Search

More Threads

• Listview problem when modifying a item(or subitem)
• combobox issue
• what are the best practices to display multiple choice questions on windows forms in terms of userinterface?
• tooltips disappear at design-time
• web browser control (WebBrowserControl) and history.
• Regarding SendMessage() question posted before...
• Center a panel in a sizeable form
• Problem with notify icon
• Avoid Dowloading Flash files in webbroser
• Scrollbar class