Delegate Method.
-
I am doing a program that completely depend on serial port. While data receiving event I m getting following error. The calling thread cannot access this object because a different thread owns it. for that I used delegate method. Problem solved.
CP_Text.Dispatcher.Invoke(New DelegateUiText(AddressOf UpdateUiCP))
Delegate Sub DelegateUiText() Private Sub UpdateUiCP() CP\_Text.Content = Format(Convert.ToInt32(RxStr.Substring(1, 2), 16) / 100, "0.00") End Sub
But I am using 25 labels and 2 datagrids and 12 textboxs. so i need write above 5 line for all controls so. Is any method, i can handle above error in single method or event? thanks in advance Nanda.
-
I am doing a program that completely depend on serial port. While data receiving event I m getting following error. The calling thread cannot access this object because a different thread owns it. for that I used delegate method. Problem solved.
CP_Text.Dispatcher.Invoke(New DelegateUiText(AddressOf UpdateUiCP))
Delegate Sub DelegateUiText() Private Sub UpdateUiCP() CP\_Text.Content = Format(Convert.ToInt32(RxStr.Substring(1, 2), 16) / 100, "0.00") End Sub
But I am using 25 labels and 2 datagrids and 12 textboxs. so i need write above 5 line for all controls so. Is any method, i can handle above error in single method or event? thanks in advance Nanda.
You could reduce the delegate-invoked code to one pair of methods for each property you want to update by doing something like:
Delegate Sub delSetText(ByVal theControl As Control, ByVal st As String) Sub doSetText(ByVal theControl As Control, ByVal st As String) theControl.Text = st End Sub Sub InvokeSetText(ByVal theControl As Control, ByVal st As String) theControl.Invoke(New delSetText(AddressOf doSetText), theControl, st) End Sub
then to set the text on a happyButton to "George", you would simply say InvokeSetText(happyButton, "George"). Nice and clean and easy.