Advertisement
Guest User

Untitled

a guest
May 7th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using System.IO;
  12. using System.Runtime.InteropServices;
  13.  
  14. namespace MyComponent
  15. {
  16.     //public static class ModifyProgressBarColor
  17.     //{
  18.     //    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
  19.     //    static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr w, IntPtr l);
  20.     //    public static void SetState(this ProgressBar pBar, int state)
  21.     //    {
  22.     //        SendMessage(pBar.Handle, 1040, (IntPtr)state, IntPtr.Zero);
  23.     //    }
  24.     //}
  25.  
  26.     public partial class Form1 : Form
  27.     {
  28.         public Form1()
  29.         {
  30.             InitializeComponent();
  31.             numberComponent1.GroupTitle = "Copy file";
  32.             numberComponent1.sendMsg = receiveMsg;
  33.       //           progressBar1.SetState(2);
  34.  
  35.         }
  36.  
  37.         public void updateProgressBar(int cislo)
  38.         {
  39.             progressBar1.Value = cislo;
  40.         }
  41.         //   ModifyProgressBarColor.SetState(progressBar1, 2);
  42.  
  43.  
  44.         FileOperation.SendMsgCnt s = null;
  45.  
  46.         // public delegate void SetValueIntCallBack(int v);
  47.  
  48.         public void ZapisData(int cislo)
  49.         {
  50.             if (progressBar1.InvokeRequired)
  51.             {
  52.                 s = updateProgressBar;
  53.                 this.Invoke(s, new object[] { cislo });
  54.             }
  55.             else
  56.                 progressBar1.Value = cislo;
  57.         }
  58.  
  59.         //  public FileOperation.SendMsgCnt s = null;
  60.  
  61.         public SendMsgStop sendMsgStop = null;
  62.  
  63.         //   public delegate void ThreadStart();
  64.  
  65.         private void receiveMsg()
  66.         {
  67.             try
  68.             {
  69.                 //   string z = numberComponent1.Zdroj;
  70.                 //   string c = numberComponent1.Cil;  
  71.             }
  72.  
  73.             catch (Exception)
  74.             {
  75.                 MessageBox.Show("01", "Chyba");
  76.             }
  77.         }
  78.  
  79.         private void button1_Click(object sender, EventArgs e)
  80.         {
  81.             try
  82.             {
  83.                 string z = numberComponent1.Zdroj;
  84.                 string c = numberComponent1.Cil;
  85.  
  86.                 FileOperation operace = new FileOperation(z, c);
  87.                 // s(30);
  88.  
  89.  
  90.  
  91.  
  92.                 //           DelegateCopy objektDelegata = new DelegateCopy(operace.Copy);
  93.                 System.Threading.Thread t = new System.Threading.Thread(operace.Copy);
  94.                 t.Start();
  95.                 //progressBar1.Value = FileOperation.updateProgressBar(5);
  96.             }
  97.  
  98.             catch (NezadanaCestaExpection ex)
  99.             {
  100.                 MessageBox.Show(ex.Message, "Chyba");
  101.             }
  102.             catch (NezadanyCilExpection ex)
  103.             {
  104.                 MessageBox.Show(ex.Message, "Chyba");
  105.             }
  106.             catch (Exception)
  107.             {
  108.                 // MessageBox.Show("{0}\n{1}", "Během zpracování souboru došlo k chybě.", e.Message);
  109.             }
  110.  
  111.  
  112.  
  113.         }
  114.  
  115.         private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  116.         {
  117.  
  118.  
  119.             try
  120.             {
  121.                 string z = numberComponent1.Zdroj;
  122.                 string c = numberComponent1.Cil;
  123.  
  124.                 FileOperation operace = new FileOperation(z, c);
  125.                 //operace.Copy(progressBar1);
  126.                 for (int i = 0; i <= 100; i++)
  127.                 {
  128.                     if (backgroundWorker1.CancellationPending)
  129.                     {
  130.                         File.Delete(c);
  131.                         e.Cancel = true;
  132.                         MessageBox.Show("Kopírování bylo přerušeno!");
  133.                         break;
  134.                     }
  135.  
  136.                     else
  137.                     {
  138.                         kopirovani();
  139.                         // int pocetZkopirovanych = FileOperation.CharCount(operace.FILENAMEIN) / FileOperation.CharCount(operace.FILENAMEIN);
  140.                         backgroundWorker1.ReportProgress(i);
  141.                     }
  142.                 }
  143.                 operace.Copy();
  144.  
  145.  
  146.  
  147.             }
  148.             catch (Exception ex) { MessageBox.Show(ex.Message); e.Cancel = true; };
  149.         }
  150.  
  151.         //   FileOperation.Char
  152.         private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
  153.         {
  154.             progressBar1.Value = e.ProgressPercentage;
  155.             lbStavProcent.Text = e.ProgressPercentage.ToString() + " %";
  156.         }
  157.  
  158.         private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  159.         {
  160.             if (e.Cancelled)
  161.             {
  162.                 progressBar1.Value = 0;
  163.                 lbStavProcent.Text = "";
  164.             }
  165.             else
  166.             {
  167.                 MessageBox.Show("Kopírování úspěšně dokončeno");
  168.                 lbStavProcent.Text = "";
  169.                 progressBar1.Value = 0;
  170.  
  171.             }
  172.         }
  173.         private void kopirovani()
  174.         {
  175.             Thread.Sleep(10);
  176.         }
  177.  
  178.         private void button1_Click_1(object sender, EventArgs e)
  179.         {
  180.             backgroundWorker1.RunWorkerAsync();
  181.  
  182.         }
  183.  
  184.         private void button2_Click(object sender, EventArgs e)
  185.         {
  186.             backgroundWorker1.CancelAsync();
  187.         }
  188.  
  189.     }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement