c# transfer files in parallel

Advertisement

Komatozzz
Joined:
Posts:
1
Location:
Rus

c# transfer files in parallel

Hello ewary one.
I have a problem with transfer files in parallel. When i do that - this look all ok, but indeed nope. If i start one task - File's transfer worked OK, but if i try do more then 1 session in parallel - session.FileTransferProgress in all Task return 100%, but File's loads in not all session in fact. What wrong i do?
PS Without parallel - all works fine.
await Task.Run(() => Parallel.ForEach(storeList, store =>
    {
        if (store.CheckBox == true)
        {
            parallel_options.CancellationToken.ThrowIfCancellationRequested();
 
            using (Session session = new Session())
            {
                options.HostName = store.IpForFiles;
                session.Timeout = TimeSpan.FromSeconds(10);
                session.FileTransferProgress += SessionFileTransferProgress;
                session.Open(options);
                Console.Write("\n" + store.Num + " START!");
                try
                {
                    foreach (var path in FilesList)
                    {
                        bool fileExists = session.FileExists("test_for_upload/");
                        if (fileExists == true)
                        {
                            var transferResult = session.PutFiles(path.PathFile, remoutePath);
                            Console.Write("\n" + store.Num + " EXISTS");
                        }
                        else
                        {
                            session.CreateDirectory("test_for_upload/");
                            var transferResult = session.PutFiles(path.PathFile, remoutePath);
                            Console.Write("\n" + store.Num + " CREATER");
                        }
                    }
                    Console.Write("\n" + store.Num + " FINISH!");
                    session.Close();
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                }
                finally
                {
 
                }
 
            }
        }
        else
        {
 
        }
 
    }));

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,468
Location:
Prague, Czechia

Re: c# transfer files in parallel

Show us your SessionFileTransferProgress, and add some logging to it. And show us an overall console output of your code.

Reply with quote

Guest

Hi, Martin!
Thanks for the help, I took a closer look at the log file from SessionLogPath and found the problem.
Although I'm in the line options.HostName = store.IpForFiles; I set the host for the connection - it happens on a different ip.
Solved the problem with this
lock (session.SessionLogPath)
{
    options.HostName = store.IpForFiles;
    session.Open (options);
}
Now everything works out correctly.

Reply with quote

Advertisement

You can post new topics in this forum