PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2010 Using multi threading with copy-VBForums
Results 1 to 6 of 6

Thread: Using multi threading with copy

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    455

    Using multi threading with copy

    Dear all,

    Learning something about multi threading we are busty with script that will use all cores with the copy activity.

    If you look to the script, we have the idea that not all cores are used: Thread1.Join()

    How can we change the script so all cores are used.

    Thanks for reading and any help.

    Nice regards,
    Michelle.
    Attached Files Attached Files

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,313

    Re: Using multi threading with copy

    You do not make much sense. When posting please provide a full and clear description of your issue. This includes us not downloading files. With nearly 500 posts surely you know how we work?

  3. #3
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Using multi threading with copy

    Your code is only going to perform one of those tasks at a time because it will stop and wait on Thread1.Join() for that task to finish, before it moves on to the next task. So you are gaining no multi threading benefit there. What is the point of having the Thread1.Join call? You're just starting a thread and then waiting for it to finish then not doing anything else. Why not just start the thread and let it go off and do its work then start another thread. Why do you need to wait for one to finish before you start another?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Using multi threading with copy

    I'd do it something like this:

    vb.net Code:
    1. Module Module1
    2.  
    3.     Const dirTarget As String = "C:\Temp\target\"
    4.     Const dirSource As String = "C:\Temp\source\"
    5.  
    6.     Sub Main()
    7.         For Each FilePath As String In IO.Directory.GetFiles(dirSource)
    8.             Threading.ThreadPool.QueueUserWorkItem(AddressOf CopyFile, FilePath)
    9.         Next
    10.     End Sub
    11.  
    12.     Private Sub CopyFile(FilePath As Object)
    13.         IO.File.Copy(CStr(FilePath), IO.Path.Combine(dirTarget, IO.Path.GetFileName(CStr(FilePath))))
    14.     End Sub
    15.  
    16. End Module
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    455

    Re: Using multi threading with copy

    Dear all,

    Thanks for the information.

    Michelle.

  6. #6
    New Member
    Join Date
    Dec 2014
    Posts
    3

    Re: Using multi threading with copy

    You could get better results if you used delegates (you can check this blog for source code of a sample app: http://ireksdotnetcorner.blogspot.co...threading.html) How many files you process? If it's more than 10 you should create a queue of files and a pool/array of threads(3-5) that communicate with delegates and pop the next file until the queue is empty (worker method on the thread should have a loop with a call to the main thread for the next file - it could be function like this "CopyUpdate(sLastCopiedFile as string, iResult as integer) sNextFile as string")

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width