示例#1
0
        private void showFiles_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(delegate()
            {
                selectedfilesPath = null;
                string[] fullPaths;
                if (IsFieldsCorrect())
                {
                    try
                    {
                        ConsoleWrite("Соединяемся...");
                        client.ConnectionToServer(ipTextbox.Text, Convert.ToInt32(portTextbox.Text));
                        ConsoleWrite("Соединение установлено");
                        ConsoleWrite("Скачиваем список файлов...");
                        filesPath = client.ReciveFilesList();
                        ConsoleWrite("Список файлов получен");
                        Invoke(new Action(() =>
                        {
                            toolStripProgressBar1.Value = 0;
                        }));
                        FillFileList();
                        //ff.ShowDialog();

                        Invoke(new Action(() =>
                        {
                            ff.ShowDialog();
                        }));
                    }
                    catch (Exception ex)
                    {
                        Invoke(new Action(() =>
                        {
                            toolStripProgressBar1.Value = 0;
                        }));
                        ConsoleWrite(ex.Message);
                        return;
                    }

                    if (selectedfilesPath != null)
                    {
                        fullPaths = GetFullPathsFromFileList(selectedfilesPath);
                        if (fullPaths.Length > 1)
                        {
                            try
                            {
                                ConsoleWrite("Cкачиваем...");
                                client.DownloadFiles(fullPaths, saveToPath);
                                ConsoleWrite("Файлы загружены в папку " + saveToPath);
                                Invoke(new Action(() =>
                                {
                                    toolStripProgressBar1.Value = 0;
                                }));
                                client.DisconnectFromServer();
                            }
                            catch (Exception ex)
                            {
                                Invoke(new Action(() =>
                                {
                                    toolStripProgressBar1.Value = 0;
                                }));
                                ConsoleWrite(ex.Message);
                            }
                        }
                        else if (fullPaths.Length == 1)
                        {
                            try
                            {
                                ConsoleWrite("Cкачиваем...");
                                client.DownloadFile(fullPaths[0], saveToPath);
                                ConsoleWrite("Файл загружен в папку " + saveToPath);
                                Invoke(new Action(() =>
                                {
                                    toolStripProgressBar1.Value = 0;
                                }));
                                client.DisconnectFromServer();
                            }
                            catch (Exception ex)
                            {
                                Invoke(new Action(() =>
                                {
                                    toolStripProgressBar1.Value = 0;
                                }));
                                ConsoleWrite(ex.Message);
                            }
                        }
                    }
                    else
                    {
                        client.DisconnectFromServer();
                    }
                }
            });

            t.SetApartmentState(ApartmentState.STA);
            t.Start();
        }