none
Compare two array (String) concurrently and highlight the differences RRS feed

  • Question

  • Hi there,

    I am creating a application to compare to text filees for example

    Test file one : The code is visual basic dot net

    Test file two: The code is visual basic dog com

    For the following code i have split them into an array of each as shown below. The content are extract from the rich text box and they are matched accordingly. Once it spoted a differece the rich text box will highlight the changes in yellow.

    However, how can i create such that the for loop can be run concurrently at the same time, as i will hit index out of bound no matter what.

          Dim s As String = RichTextBox1.Text()
          Dim t As String = RichTextBox2.Text()

          Dim original() As String = s.Split(New Char() {" "c})
          Dim other() As String = t.Split(New Char() {" "c})


           Dim word As String

            'For Each word In other
            Dim i As Integer
            For i = 0 To original.Length - 1

                If original(i).Contains(other(i)) = True Then
                    per += 1
                    MessageBox.Show(CStr(per))
                    MessageBox.Show(original(i).ToString())
                    MessageBox.Show(other(i).ToString())


                    RichTextBox2.Select(0, RichTextBox2.TextLength)
                    RichTextBox2.SelectionBackColor = Color.White

                Else
                    MessageBox.Show("Mismatch")
                    RichTextBox2.Select(0, RichTextBox2.TextLength)
                    RichTextBox2.SelectionBackColor = Color.Yellow
                End If
            Next


    Thursday, July 26, 2012 7:33 AM

Answers

  • Hi Eric,

    Welcome to the MSDN Forum.

    As Veerendra suggested: Thread pool is an option.

    And my suggestion is Backgroudworker:http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx 

    You can put the compare things in the backgroundworker, when it found a difference, invoke the UI thread to highlight the corresponding text.

    About how to invoke UI thread, please take a look at this sample code:

        AddHandler TestWorker.DoWork, AddressOf MainWork
    
        Private Sub MainWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
            'compare the array
            setLabelValue("End Main Work")
            'end compare the array
        End Sub
    
        Public Delegate Sub setLabelValueDelegate(ByVal stringValue As String)
        Private Sub setLabelValue(ByVal stringValue As String)
            If Me.InvokeRequired Then
                Me.Invoke(New setLabelValueDelegate(AddressOf setLabelValue), stringValue)
            Else
                Me.Label1.Text = stringValue
            End If
        End Sub

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, July 27, 2012 7:37 AM
    Moderator

All replies

  • Which version you are using? if it is .net 4.0 you can use parallel.for loop to achieve your scenario!

    If you have to do it in .net 3.5 and less, you could use thread pool to achieve it. Just delegate the comparision logic to function and with a waitforcallback.

    You can refer to this blog for more visibility.



    Please mark the replies as Answered if they help and Vote if you found them helpful.

    Thursday, July 26, 2012 9:58 AM
  • Hi Kumar,

    Are there any other ways of implementing ? as i am limited to just .net 3.5...

    Thanks!

    Friday, July 27, 2012 3:32 AM
  • Hi Eric,

    Welcome to the MSDN Forum.

    As Veerendra suggested: Thread pool is an option.

    And my suggestion is Backgroudworker:http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx 

    You can put the compare things in the backgroundworker, when it found a difference, invoke the UI thread to highlight the corresponding text.

    About how to invoke UI thread, please take a look at this sample code:

        AddHandler TestWorker.DoWork, AddressOf MainWork
    
        Private Sub MainWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
            'compare the array
            setLabelValue("End Main Work")
            'end compare the array
        End Sub
    
        Public Delegate Sub setLabelValueDelegate(ByVal stringValue As String)
        Private Sub setLabelValue(ByVal stringValue As String)
            If Me.InvokeRequired Then
                Me.Invoke(New setLabelValueDelegate(AddressOf setLabelValue), stringValue)
            Else
                Me.Label1.Text = stringValue
            End If
        End Sub

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, July 27, 2012 7:37 AM
    Moderator