Community discussion forum

Multithreading in VB.NET

This is a comment thread discussing Multithreading in VB.NET
  • 10 years ago

    This thread is for discussions of Multithreading in VB.NET.

  • 3 years ago

    Volatility means a lot more than is described in the article. It doesn't just affect the variable which has the volatile modifed; it affects the whole memory model for sections of code involving access to that variable.


    I agree that there's no use of volatile which can't be semantically achieved using locks, and indeed I generally prefer to use locks myself. However, there is a speed difference between acquiring a lock and just using a volatile variable - in a very few cases it might be significant. I'm surprised VB.NET doesn't have any way of specifying this.


    See the volatility page of my C# threading article for more information.


    Jon Skeet

  • 3 years ago

    Yes, but I decided not to go into detail since it's a vb.net article and you can't use it

  • 2 years ago

    As somewhat of an advanced "newbie" I have read many articles on Multithreading, and am at a loss

    Do I need to have a delegate and call the delegate when I am using threading ?

     

    Lets say I have a datatable with 5 million rows and I need to iterate through each row - grab some info , process that info (translate it) and then save it somewhere else. For example insert it inot a completely different database (and maybe even RDBMS - Architectur)

    Now I currently for each row - but that takes a lot of time.

    I would like to spawn a (Flexible Number of threads) to do the row processing - Will my For each move on as each thread runs. So I start Thread(1) will the next trigger, and I have a new row and can start thread(2) and so on. Also my Subroutine, must

  • 1 year ago

    Bundle of thanks to the author, it is very help full Yes
     

  • 4 months ago

    I need to lock the Log file once someone is using it and second person shouldn't able to write in log file. The code is shown below :

    Imports System Imports System.IO

    Module FileTransfer

    Sub Main()
        Dim dtStart As Date = Now(), dtEnd As Date = Now()
        Dim intNoofFiles As Integer = 0
        Dim strLogFileName As String = "C:\Log\FileTransferLog_" + dtStart.Year.ToString().PadLeft(4, "0") + dtStart.Month.ToString().PadLeft(2, "0") + dtStart.Day.ToString().PadLeft(2, "0") + ".txt"
        Dim oWrite As StreamWriter = File.AppendText(strLogFileName)
    
    
        oWrite.WriteLine("=================================================================================================")
        oWrite.WriteLine("Starting PRESTO File Transfer Job. Start Date: " + dtStart.ToString())
        oWrite.WriteLine("=================================================================================================")
    
        ' Call main job 
        subMoveFiles("C:\Presto\Inbound\", "c:\PRESTO_FILES\", oWrite, intNoofFiles)
    
        dtEnd = Now()
        oWrite.WriteLine("=================================================================================================")
        oWrite.WriteLine("Completed PRESTO File Transfer Job. End Date: " + dtEnd.ToString())
        oWrite.WriteLine("Total number of files transferred: " + intNoofFiles.ToString())
        oWrite.WriteLine("=================================================================================================")
    
        oWrite.Close()
    End Sub
    
    Public Sub subMoveFiles(ByVal strSrcDirectory As String, ByVal strDstDirectory As String, ByRef oWrite As StreamWriter, ByRef intNoofFiles As Integer)
        If strDstDirectory(strDstDirectory.Length - 1) <> Path.DirectorySeparatorChar Then
            strDstDirectory += Path.DirectorySeparatorChar
        End If
    
        If Not Directory.Exists(strDstDirectory) Then
            Directory.CreateDirectory(strDstDirectory)
        End If
    
        Dim strFileSystemEntries As String() = Directory.GetFileSystemEntries(strSrcDirectory)
        Dim strFileSystemEntry As String = ""
        For Each strFileSystemEntry In strFileSystemEntries
            If (Directory.Exists(strFileSystemEntry)) Then
                'Sub directories
                subMoveFiles(strFileSystemEntry, strDstDirectory + Path.GetFileName(strFileSystemEntry), oWrite, intNoofFiles)
            Else
                ' get the created date / last written date
                If DateAdd(DateInterval.Minute, 5, File.GetLastWriteTime(strFileSystemEntry)) < Now() Then
                    oWrite.WriteLine("Transferring File: " + strFileSystemEntry)
                    ' First Copy the file to the destination and then Delete
                    File.Copy(strFileSystemEntry, strDstDirectory + Path.GetFileName(strFileSystemEntry), True)
                    File.Delete(strFileSystemEntry)
                    intNoofFiles = intNoofFiles + 1
                    oWrite.WriteLine("Completed Transferring File: " + strFileSystemEntry)
                End If
            End If
        Next
    End Sub
    

    End Module

Post a reply

Enter your message below

Sign in or Join us (it's free).

We'd love to hear what you think! Submit ideas or give us feedback