Click here to Skip to main content
16,005,178 members
Home / Discussions / Visual Basic
   

Visual Basic

 
Questiongetting complete name of account NT server Pin
sal216-May-10 10:00
sal216-May-10 10:00 
AnswerRe: getting complete name of account NT server Pin
Stinebaugh6-May-10 16:30
Stinebaugh6-May-10 16:30 
GeneralRe: getting complete name of account NT server Pin
sal216-May-10 20:33
sal216-May-10 20:33 
GeneralRe: getting complete name of account NT server Pin
Stinebaugh7-May-10 6:22
Stinebaugh7-May-10 6:22 
GeneralRe: getting complete name of account NT server Pin
sal217-May-10 11:56
sal217-May-10 11:56 
QuestionMultithreading slower? [modified] Pin
NikWing6-May-10 5:36
NikWing6-May-10 5:36 
AnswerRe: Multithreading slower? Pin
Luc Pattyn6-May-10 5:52
sitebuilderLuc Pattyn6-May-10 5:52 
GeneralRe: Multithreading slower? Pin
NikWing6-May-10 6:33
NikWing6-May-10 6:33 
Hello Luc,

thanks for replying Smile | :)
Now I have a little problem. I never learned VB.net, (somehow I even wonder how I was able to write a program like that, lol) I browsed the www and looked for the infos I need, experimented with code snippets and everything works like a charm, except the current problem where I want to make it a bit faster using 2 threads.
I updated the start post before I saw your reply.

My computer's a quadcore/iQ6600.
While the 2 threads are working (even if it's the single-thread methode) my hdd-LED doesn't flash much, I mean it's only flickering now and then, so I guess it doesn't put much load on the hdd.

hm I hope it's ok to paste the code here:

Private hashdups_m As Thread
Private hashdups_t1 As Thread
Private hashdups_t2 As Thread

Private Sub hash_folder()
    Dim di As New IO.DirectoryInfo(src_cmp_fldr)
    Dim aryFi As IO.FileInfo() = di.GetFiles("*.*")
    If di.GetFiles("*.*").Length.ToString <> 0 Then
       di_hashdups = New IO.DirectoryInfo(src_cmp_fldr)
       aryFi_hashdups = di_hashdups.GetFiles("*.*")
       hashdups_m = New Thread(AddressOf hashdups_main)
       hashdups_m.Start()
    End If
End Sub

Private Sub hashdups_main()
    progb2val_a = 0
    progb2val_b = 0
    hashdups_t1 = New Thread(AddressOf hashdups_hash1)
    hashdups_t2 = New Thread(AddressOf hashdups_hash2)
    If aryFi_hashdups.Count = 1 Then
        progb2val_b = 50
        hashdups_t1.Start()
        hashdups_t1.Join()
    End If
    If aryFi_hashdups.Count > 1 Then
        hashdups_t1.Start()
        hashdups_t2.Start()
        hashdups_t1.Join()
        hashdups_t2.Join()
    End If

    If hashdups_t1.IsAlive = False And hashdups_t2.IsAlive = False Then
        ds_db_cmp_fldr.Tables.Add("db_cmp_fldr")
        dt_db_cmp_fldr = ds_db_cmp_fldr.Tables("db_cmp_fldr")

        Dim col_fs_dec As DataColumn = New DataColumn("filesize")
        col_fs_dec.DataType = System.Type.GetType("System.Decimal")
        dt_db_cmp_fldr.Columns.Add(col_fs_dec)

        Dim col_sha1_string As DataColumn = New DataColumn("sha256hash")
        col_sha1_string.DataType = System.Type.GetType("System.String")
        dt_db_cmp_fldr.Columns.Add(col_sha1_string)

        Dim col_fn_string As DataColumn = New DataColumn("filename")
        col_fn_string.DataType = System.Type.GetType("System.String")
        dt_db_cmp_fldr.Columns.Add(col_fn_string)

        If ds_db_cmp_fldr.Tables.Contains("db_cmp_fldr_a") = True Then
        For Each drSource_a As DataRow In dt_db_cmp_fldr_a.Rows
            dt_db_cmp_fldr.ImportRow(drSource_a)
        Next
        ds_db_cmp_fldr.Tables.Remove("db_cmp_fldr_a")
        End If
        If ds_db_cmp_fldr.Tables.Contains("db_cmp_fldr_b") = True Then
        For Each drSource_b As DataRow In dt_db_cmp_fldr_b.Rows
            dt_db_cmp_fldr.ImportRow(drSource_b)
        Next
        ds_db_cmp_fldr.Tables.Remove("db_cmp_fldr_b")
        End If
        Me.Invoke(New upd_hashdups_m(AddressOf upd_hashdups_m_val), 1)
    End If
End Sub


Private Sub hashdups_hash1()
    ds_db_cmp_fldr.Tables.Add("db_cmp_fldr_a")
    dt_db_cmp_fldr_a = ds_db_cmp_fldr.Tables("db_cmp_fldr_a")

    Dim col_fs_dec_a As DataColumn = New DataColumn("filesize")
    col_fs_dec_a.DataType = System.Type.GetType("System.Decimal")
    dt_db_cmp_fldr_a.Columns.Add(col_fs_dec_a)

    Dim col_sha1_string_a As DataColumn = New DataColumn("sha256hash")
    col_sha1_string_a.DataType = System.Type.GetType("System.String")
    dt_db_cmp_fldr_a.Columns.Add(col_sha1_string_a)

    Dim col_fn_string_a As DataColumn = New DataColumn("filename")
    col_fn_string_a.DataType = System.Type.GetType("System.String")
    dt_db_cmp_fldr_a.Columns.Add(col_fn_string_a)

    Dim myNewRow As DataRow
    Dim aryficnt_a As Integer = Math.Ceiling(aryFi_hashdups.Count / 2)
    For r1 = 0 To aryficnt_a - 1
        result = Nothing
        hashclass.HashFile(aryFi_hashdups(r1).FullName)
        myNewRow = dt_db_cmp_fldr_a.NewRow()
        myNewRow("filesize") = aryFi_hashdups(r1).Length
        myNewRow("sha256hash") = result
        myNewRow("filename") = aryFi_hashdups(r1).Name
        dt_db_cmp_fldr_a.Rows.Add(myNewRow)
        progb2val_a = (50 * (r1 + 1) / aryficnt_a)
        Me.Invoke(New upd_progbar2_hash(AddressOf progbar2_val_hash), Convert.ToString(0))
    Next
    Me.Invoke(New upd_progbar2_hash(AddressOf progbar2_val_hash), Convert.ToString(0))
End Sub



Private Sub hashdups_hash2()
    ds_db_cmp_fldr.Tables.Add("db_cmp_fldr_b")
    dt_db_cmp_fldr_b = ds_db_cmp_fldr.Tables("db_cmp_fldr_b")

    Dim col_fs_dec_b As DataColumn = New DataColumn("filesize")
    col_fs_dec_b.DataType = System.Type.GetType("System.Decimal")
    dt_db_cmp_fldr_b.Columns.Add(col_fs_dec_b)

    Dim col_sha1_string_b As DataColumn = New DataColumn("sha256hash")
    col_sha1_string_b.DataType = System.Type.GetType("System.String")
    dt_db_cmp_fldr_b.Columns.Add(col_sha1_string_b)

    Dim col_fn_string_b As DataColumn = New DataColumn("filename")
    col_fn_string_b.DataType = System.Type.GetType("System.String")
    dt_db_cmp_fldr_b.Columns.Add(col_fn_string_b)

    Dim myNewRow As DataRow
    Dim aryficnt_b As Integer = Math.Ceiling(aryFi_hashdups.Count / 2)

    For r2 = aryficnt_b To aryFi_hashdups.Count - 1
        result = Nothing
        hashclass.HashFile(aryFi_hashdups(r2).FullName)
        myNewRow = dt_db_cmp_fldr_b.NewRow()
        myNewRow("filesize") = aryFi_hashdups(r2).Length
        myNewRow("sha256hash") = result
        myNewRow("filename") = aryFi_hashdups(r2).Name
        dt_db_cmp_fldr_b.Rows.Add(myNewRow)
        progb2val_b = (50 * (r2 - aryficnt_b + 1) / aryficnt_b)
        Me.Invoke(New upd_progbar2_hash(AddressOf progbar2_val_hash), Convert.ToString(0))
    Next
    Me.Invoke(New upd_progbar2_hash(AddressOf progbar2_val_hash), Convert.ToString(0))
End Sub


the hashclass:

Public Class hashclass

    Public Shared Function HashFile(ByVal File As String) As String
        Dim FN As New FileStream(File, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
        Dim HashValue(0) As Byte
        Dim Tmp As String = ""

        Dim SHA256 As New SHA256Managed
        SHA256.ComputeHash(FN)
        HashValue = SHA256.Hash
        FN.Close()

        For i As Integer = 0 To HashValue.Length - 1
            Tmp = Hex(HashValue(i))
            If Len(Tmp) = 1 Then Tmp = "0" & Tmp
            result += Tmp
        Next
        Return result
    End Function

End Class


Maybe there's an easier way than I found, but it works Big Grin | :-D
The single thread version nearly looks the same, it skips making 2 tables and later reading both into a new table. Also the FOR NEXT loop is just from 0 to aryFi_hashdups.Count - 1

I just don't see why it varies like I wrote in the startpost.
If you need any other infos, please ask Smile | :)

Thanks,
Nik
GeneralRe: Multithreading slower? Pin
Luc Pattyn6-May-10 7:00
sitebuilderLuc Pattyn6-May-10 7:00 
GeneralRe: Multithreading slower? Pin
NikWing6-May-10 7:45
NikWing6-May-10 7:45 
GeneralRe: Multithreading slower? Pin
Luc Pattyn6-May-10 8:34
sitebuilderLuc Pattyn6-May-10 8:34 
GeneralRe: Multithreading slower? Pin
Luc Pattyn6-May-10 12:38
sitebuilderLuc Pattyn6-May-10 12:38 
GeneralRe: Multithreading slower? [modified] Pin
NikWing6-May-10 13:23
NikWing6-May-10 13:23 
General!!!DO NOT DUPLICATE CODE!!! Pin
Luc Pattyn6-May-10 13:37
sitebuilderLuc Pattyn6-May-10 13:37 
GeneralRe: !!!DO NOT DUPLICATE CODE!!! Pin
NikWing6-May-10 14:03
NikWing6-May-10 14:03 
GeneralRe: !!!DO NOT DUPLICATE CODE!!! Pin
Luc Pattyn6-May-10 14:12
sitebuilderLuc Pattyn6-May-10 14:12 
GeneralRe: !!!DO NOT DUPLICATE CODE!!! Pin
NikWing6-May-10 14:57
NikWing6-May-10 14:57 
GeneralRe: !!!DO NOT DUPLICATE CODE!!! Pin
Dave Kreskowiak6-May-10 16:57
mveDave Kreskowiak6-May-10 16:57 
GeneralRe: !!!DO NOT DUPLICATE CODE!!! Pin
NikWing7-May-10 0:43
NikWing7-May-10 0:43 
GeneralRe: Multithreading slower? Pin
Luc Pattyn6-May-10 13:57
sitebuilderLuc Pattyn6-May-10 13:57 
GeneralRe: Multithreading slower? Pin
NikWing6-May-10 14:21
NikWing6-May-10 14:21 
AnswerRe: Multithreading slower? Pin
Luc Pattyn6-May-10 6:14
sitebuilderLuc Pattyn6-May-10 6:14 
AnswerRe: Multithreading slower? Pin
supercat96-May-10 6:54
supercat96-May-10 6:54 
GeneralRe: Multithreading slower? Pin
NikWing6-May-10 7:56
NikWing6-May-10 7:56 
GeneralRe: Multithreading slower? Pin
supercat96-May-10 9:11
supercat96-May-10 9:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.