1 Attachment(s)
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.
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?
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?
Re: Using multi threading with copy
I'd do it something like this:
vb.net Code:
Module Module1
Const dirTarget As String = "C:\Temp\target\"
Const dirSource As String = "C:\Temp\source\"
Sub Main()
For Each FilePath As String In IO.Directory.GetFiles(dirSource)
Threading.ThreadPool.QueueUserWorkItem(AddressOf CopyFile, FilePath)
Next
End Sub
Private Sub CopyFile(FilePath As Object)
IO.File.Copy(CStr(FilePath), IO.Path.Combine(dirTarget, IO.Path.GetFileName(CStr(FilePath))))
End Sub
End Module
Re: Using multi threading with copy
Dear all,
Thanks for the information.
Michelle.
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")