3 Replies - 2695 Views - Last Post: 15 April 2010 - 09:25 AM Rate Topic: -----

#1 p2kpradeep  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-April 10

Posting data to a RichTextBox on a Form from external objects

Posted 13 April 2010 - 12:42 AM

Background: My VB .Net application is a back-end solution that works with a proprietary Linux-based middle-ware messaging architecture developed by our company. My application references to the middle-ware DLLs, obtains connectivity, and subscribes to specific broadcasts with the middle-ware. When the broadcasts are received, it handles processes them and inserts/updates certain tables in a database. To do so, it has specific notification classes to handle each type of broadcast. These classes adhere to the inheritance model imposed by the middle-ware DLLs.

Requirement: The application has a startup console form with a RichTextBox control where each notification handler needs to post status messages as and when broadcast are received and are being processed.

First attempt: My first attempt was to keep a global reference to my console form, and use that in each notification handler class to access the RichTextBox in the form to write the status messages to it. It failed with Illegal Cross-threaded Operation exception.

Second attempt: After searching the web for a viable solution, my second attempt was to simply set the CheckForIllegalCrossThreadCalls property of the form to False and continue with the above method of access. It worked.

Problem: Though my second attempt seemed to work under test conditions, it seemed to have a great impact on speed. In production environment, it absolutely slowed down my application! Real-time broadcast handling was impossible due to the time-lag.

Request: Could someone help me out in the given scenario? A point to be noted here is that the objects (or threads?) that need to post the messages are not directly spawned from the console form.

Many thanks.

Warm regards,
Pradeep K.

Is This A Good Question/Topic? 0
  • +

Replies To: Posting data to a RichTextBox on a Form from external objects

#2 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 177
  • View blog
  • Posts: 648
  • Joined: 03-February 10

Re: Posting data to a RichTextBox on a Form from external objects

Posted 13 April 2010 - 06:54 AM

In order to change values across threads you need to use delegates. I have not worked much in the area that you are working but I have worked with the Backgroundworker and Delegates.

Simple use of delegates

And here's one in DreamInCode
Was This Post Helpful? 0
  • +
  • -

#3 p2kpradeep  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-April 10

Re: Posting data to a RichTextBox on a Form from external objects

Posted 15 April 2010 - 02:54 AM

Hi demausdauth,

Many thanks for your response. Your first link seems more suitable considering my requirements. However, my understanding of that resolution provided in that example may not be very clear because I have not practically worked with the Backgroundworker and Delegates. Would you be kind enough to review what I have gathered from the example:

The original code attempts to post the value from the ScannerInput string onto a textbox on the form, which fails. The resolution suggests use of Backgroundworker and Delegates. The modified (resolved) code uses the Delegate (which I understand is nothing but a method signature), and adds a method to the form to change the contents of the text box with the value supplied as parameter. Then it proceeds to call the RunWorkerAsync() method of an instance of a Backgroundworker (this is done at the point where it was earlier attempting to directly write the text to the textbox). Finally, the DoWork event holds a complicated routine that involves invoking the delegate with the address of the method and the actual data to be written as parameters.


If my above understanding is right, then I have a few observations:

1. Although the delegate and the actual method are programmed to accept the string parameter that needs to be displayed on the textbox, data for this parameter cannot be passed when calling the RunWorkerAsync() method.

2. Owing to the above point, the developer has been forced change the scope of the ScannerInput string. It had to be made global to the class instead of local to RecieveScannerData().



Coming to my specific case, I could set up a Backroundworker in my console form, create a method to write text to the RichTextBox, and also create a Delegate. But my only worry is that my input data would not come from the same class as shown in this example. For very certain reasons I cannot even change the scope of my variables. Firstly, I will need to make them global to the whole project, which I am not keen on doing. Secondly, it would have undesirable impact on the functionality of my application.


If my understanding has been inaccurate, please correct me.



Many thanks.

Warm regards,
Pradeep K.



View Postdemausdauth, on 13 April 2010 - 05:54 AM, said:

In order to change values across threads you need to use delegates. I have not worked much in the area that you are working but I have worked with the Backgroundworker and Delegates.

Simple use of delegates

And here's one in DreamInCode

This post has been edited by p2kpradeep: 15 April 2010 - 03:30 AM

Was This Post Helpful? 0
  • +
  • -

#4 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 177
  • View blog
  • Posts: 648
  • Joined: 03-February 10

Re: Posting data to a RichTextBox on a Form from external objects

Posted 15 April 2010 - 09:25 AM

I think you got things a little confused. The Backgroundworker is a separate thread, the RunWorkerAsync() call basically says start a new thread and in the new thread perform the code in the DoWorker() . What I believe your looking for is the delegate method signature and the invocation of the delegate.

This from post #2 in the first link that I listed. This is the main point of using a delegate

Quote

Basically it is the delegate that carries the method call across the thread boundary, thus allowing it to be executed on the main (UI) thread.


So on your form you will have...

Public Delegate WriteToRichTextBoxDelegate(ByVal StringForText As String)

Private Sub SetRichTextBox(ByVal StringForText As String)
     RichTextBox1.Text = StringForText
End Sub




So at some point you are trying to assign a value to the .Text property of the richtextbox control. It is here that you call your delegate.
ReferenceToFormWithRichTextBox.Invoke(New WriteToRichTextBoxDelegate(AddressOf SetRichTextBox), "Your Text Here")



And this maybe more simplified than it actually will be to implement.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1