示例#1
0
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 ventanaProducto = new Form2();

            ventanaProducto.ShowDialog();
        }
示例#2
0
        private void офАлфавитToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();

            f2.ShowDialog();
        }
        public int SendFile(string namefile)
        {
            try{
                byte[] msg      = new byte[1024];
                byte[] buffer   = new byte[1024 * 1024];
                byte[] conferma = new byte[1024];



                FileInfo f = new FileInfo(namefile);
                // Console.WriteLine("ho trovato un file di dimensione" + f.Length);
                nomefile = f.Name;
                dim      = f.Length;
                if (dim > (100 * 1024 * 1024))
                {
                    DialogResult result = MessageBox.Show("You try to copy or cut a big file bigger than 100 MB , are you sure? the operation may take a long time", "Warning",
                                                          MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
                    if (result == DialogResult.Cancel)
                    {
                        return(-2);
                    }
                }
                msg = Encoding.ASCII.GetBytes("F");
                clipsocket.Send(msg);

                byte[] fileNameByte = Encoding.ASCII.GetBytes(this.nomefile);
                byte[] fileNameLen  = BitConverter.GetBytes(fileNameByte.Length);
                byte[] filedim      = BitConverter.GetBytes(this.dim);
                //   Console.WriteLine("sto creando le informazioni da mandare al client");
                byte[] clientData = new byte[4 + fileNameByte.Length + filedim.Length];
                fileNameLen.CopyTo(clientData, 0);
                fileNameByte.CopyTo(clientData, 4);
                filedim.CopyTo(clientData, 4 + fileNameByte.Length);

                clipsocket.Send(clientData);


                clipsocket.Receive(conferma);

                string confermastringa = Encoding.ASCII.GetString(conferma);
                if (confermastringa.IndexOf("+OK") > -1)
                {
                    //      Console.WriteLine(" inizio lo stream del file");

                    FileStream fileStream = new FileStream(namefile, FileMode.Open, FileAccess.Read);

                    int length = (int)fileStream.Length;  // get file length
                    // create buffer
                    int    count;
                    double d;         // actual number of bytes read
                    long   sum = dim; // total number of bytes read
                    // Thread.Sleep(100);
                    // io questo coso non è che lo abbia tanto capito.
                    if (dim > (1024 * 1024))
                    {
                        d = (((double)dim) / (double)(1024 * 1024));
                    }
                    else
                    {
                        d = 1;
                    }

                    /************************************ ISTANZIO LA FORM PER LA SEND DEL FILE *****************/
                    // questa inzialmente era fatta allocando un altro thread che si occupava di aggiornare la
                    // barra. Da rivedere è l'utilizzo della percentuale. Da mettere Lato client. Ma al ricevitore?

                    Form2 form2 = new Form2();
                    //
                    err = false;
                    erroreTrasferimento = false;
                    backgroundThread    = new Thread(
                        new ThreadStart(() =>
                    {
                        try
                        {
                            while (((count = fileStream.Read(buffer, 0, 1024 * 1024)) > 0) && !err)
                            {
                                // form2.SetBar((int)sum);
                                clipsocket.Send(buffer, count, SocketFlags.None);
                                dim = dim - count;
                                //  Console.WriteLine(dim);
                                long count2 = count;
                                form2.UpdateProgress(count, (int)sum, nomefile);

                                /*** aggiorno la progress bar***/
                            }

                            fileStream.Close();

                            // Close the dialog if it hasn't been already
                            if (form2.InvokeRequired)
                            {
                                form2.BeginInvoke(new Action(() => { form2.showdialogset(true); form2.Close(); form2.showdialogset(false); }));
                            }
                            Console.WriteLine("sto ammazzando il thread().");

                            //clipsocket.Send(Encoding.ASCII.GetBytes("-ERR\r\n"));
                            // Reset the flag that indicates if a process is currently running
                        }
                        catch (SocketException ex)
                        {
                            erroreTrasferimento = true;
                            // fileStream.Close();
                            if (form2.InvokeRequired)
                            {
                                form2.BeginInvoke(new Action(() => { form2.showdialogset(true); form2.Close(); form2.showdialogset(false); }));
                            }
                            if (clipsocket != null)
                            {
                                clipsocket.Close();
                            }
                            Console.WriteLine("sto ammazzando il thread().");
                            fileStream.Close();
                            return;
                        }
                        catch (Exception ex)
                        {
                            erroreTrasferimento = true;
                            // fileStream.Close();
                            if (form2.InvokeRequired)
                            {
                                form2.BeginInvoke(new Action(() => { form2.Close(); }));
                            }

                            Console.WriteLine("errore");
                            Console.WriteLine(ex.ToString() + ex.Message);
                        }
                    }
                                        ));

                    // Start the background process thread
                    backgroundThread.Start();

                    // form2.ShowDialog();
                    // qui sto curando il caso in cui chiudo la finestra del trasferimento.
                    // backgroundThread.Join();
                    if (form2.ShowDialog() == DialogResult.OK)
                    {
                        err = true;
                        return(-1);
                    }
                    backgroundThread.Join();
                    if (erroreTrasferimento)
                    {
                        err = true;
                        return(-1);
                    }
                }

                return(1);
            }catch (IOException ex) {
                MessageBox.Show("Devi selezionare prima un file");
                return(-1);
            }
        }