Sujoy A

Sujoy A

  • NA
  • 3
  • 1.6k

multithreading in VB.NET

Apr 27 2012 12:06 AM

Below is the code which i have used to access same method using array of threads.
But the time taken for a single excel to finish comparison is 7 min, for two its 14 or 15 min and 3 it is taking more than 21min. Time is not reducing even if using multiple threads. By using threads even if multiple excel is set for comparison, time taken for the entire process should be 7 only right.  what could be the prob in below code
Public Class Form2
    Inherits System.Windows.Forms.Form
      Public Shared Property xlTmp3 As Excel.Application
    Public Shared Property xlWb As Excel.Workbook
    Private Threads(256) As Threading.Thread
    Private CompareClassObjects(256) As CompareClass2
    Private Sub Button4_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim cr As Integer
      Dim CheckedItemArray1(2), CheckedItemArray2(2)  As String
        Dim start As Date = Date.Now
        Dim totalTime As TimeSpan
     
        CheckedItemArray1(0) = "C:\excel1.xlsx"
        CheckedItemArray2(0) = "C:\here\excel2.xlsx"
        CheckedItemArray1(1) = "C:\ excel11.xlsx"
        CheckedItemArray2(1) = "C:\here\ excel21.xlsx"
             xlTmp3 = CreateObject("Excel.Application")
        xlWb = xlTmp3.Workbooks.Add
        xlTmp3.Visible = True
        For cr = 0 To 1
            CompareClassObjects(cr) = New CompareClass2(cr, CheckedItemArray1(cr), CheckedItemArray2(cr))
            Threads(cr) = New Threading.Thread(AddressOf CompareClassObjects(cr).CompareStart)
            Threads(cr).IsBackground = True
            Threads(cr).Start()
        Next
        For cr = 0 To 1
            Threads(cr).Join()
        Next
        Dim end1 As Date = Date.Now
        totalTime = end1.Subtract(start)
        MsgBox("COMPARISON PERFORMED!!!  " & vbCrLf & "Total Time Taken : " & totalTime.Duration.ToString, vbOKOnly, "Status")
    End Sub
End Class

Public Class CompareClass2
    Public xlTmp1 As Excel.Application
    Public xlTmp2 As Excel.Application

    Public xlSht1 As Excel.Worksheet
    Public xlSht2 As Excel.Worksheet

    Public R1, DiffCount, SheetCnt As Long
Private value As Integer
    Private firstexcel, secondexcel  As String
    Public Sub New(ByVal number As Integer, ByVal text1 As String, ByVal text2 As String)
        value = number
        firstexcel = text1
        secondexcel = text2
        Form2.xlWb.Worksheets.Add()
        Form2.xlWb.Worksheets(1).Name = value + 1
        Form2.xlWb.Worksheets(1).Cells.ClearContents()
    End Sub

    Public Sub CompareStart()
        R1 = 3' just a variable used to notate as Rownumebr to put the output result in a new excel
        DiffCount = 0 ' it will increment inside TestCompareWorksheets fn as and when mismatches are found
        SheetCnt = 0 'to notate the sheet count
        Call OpenExcels(value)
        Call TestCompareWorksheets()
        Call QuitExcels()
    End Sub
    Public Sub OpenExcels(ByVal i As Integer)
'here code for opening the excel
    End Sub
    Public Sub TestCompareWorksheets()
            For Each w In xlTmp1.Worksheets
                Call CompareWorksheets()
            Next w
        End If
    End Sub
    Public Sub CompareWorksheets()
'Actual code to compare the excel sheets
    End Sub
    Public Sub QuitExcels()
'code to closing the excels
    End Sub
End Class

Answers (3)