Esempio n. 1
0
        //开始 按钮按下
        private void button3_Click(object sender, EventArgs e)
        {
            if (button3.Text == "开始")
            {
                //选择文件夹检查
                if (listBox1.Items.Count == 0)
                {
                    MessageBox.Show("请选择文件夹");
                    return;
                }

                for (int a = 0; a < listBox1.Items.Count; a++)
                {
                    if (!Directory.Exists(listBox1.Items[a].ToString()))
                    {
                        MessageBox.Show(listBox1.Items[a].ToString() + "不存在");
                        return;
                    }
                }

                //结果一览清空
                this.listView1.Items.Clear();

                //按钮非活性
                this.button1.Enabled = false;
                this.button2.Enabled = false;
                this.button3.Text    = "结束";
                this.button4.Enabled = false;
                this.button5.Enabled = false;
                this.button6.Enabled = false;
                this.button7.Enabled = false;
                this.button8.Enabled = false;

                samePercent = Convert.ToInt32(this.comboBox1.SelectedItem.ToString()) / 100.0;

                // 发时间处理放后台
                BackgroundWorkBean backgroundWorkBean = new BackgroundWorkBean();
                backgroundWorkBean.checkBox1Flag = this.checkBox1.Checked;
                backgroundWorkBean.checkBox2Flag = this.checkBox2.Checked;
                backgroundWorkBean.pathArr       = new string[listBox1.Items.Count];
                for (int a = 0; a < listBox1.Items.Count; a++)
                {
                    backgroundWorkBean.pathArr[a] = listBox1.Items[a].ToString();
                }
                backgroundWorker1.RunWorkerAsync(backgroundWorkBean);
            }
            else
            {
                if (backgroundWorker1.IsBusy)
                {
                    //按钮活性
                    this.button1.Enabled = true;
                    this.button2.Enabled = true;
                    this.button3.Text    = "开始";

                    backgroundWorker1.CancelAsync();
                }
            }
        }
Esempio n. 2
0
        //长时间处理放到后台作,不让页面假死掉
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            int imageCount = 0; //进度用

            BackgroundWorkBean bean = (BackgroundWorkBean)e.Argument;

            try
            {
                if (bean.checkBox1Flag)
                {
                    messageFlg = 1;
                    //全部文件合并到第一个文件夹
                    String mainFloder = bean.pathArr[0];
                    String renameFile;


                    for (int a = 1; a < bean.pathArr.Length; a++)
                    {
                        try
                        {
                            string[] fileList = Directory.GetFiles(bean.pathArr[a], "*.jpg", SearchOption.TopDirectoryOnly);

                            for (int i = 0; i < fileList.Length; i++)
                            {
                                imageCount++;
                                int index = 1;
                                renameFile = Path.GetFileName(fileList[i]);
                                while (File.Exists(mainFloder + @"\" + renameFile)) //重复时重命名
                                {
                                    renameFile = Path.GetFileNameWithoutExtension(fileList[i]) + "_" + index + Path.GetExtension(fileList[i]);
                                }

                                try
                                {
                                    File.Move(fileList[i], mainFloder + @"\" + renameFile);
                                }
                                catch (Exception)
                                {
                                    if (File.GetAttributes(fileList[i]).ToString().IndexOf("ReadOnly") != -1)
                                    {
                                        System.IO.File.SetAttributes(fileList[i], System.IO.FileAttributes.Normal); //去除只读
                                        File.Move(fileList[i], mainFloder + @"\" + renameFile);
                                    }
                                }

                                if (i % 10 == 0)
                                {
                                    backgroundWorker1.ReportProgress(imageCount);
                                }

                                if (backgroundWorker1.CancellationPending)
                                {
                                    e.Cancel = true;
                                    return;
                                }
                            }
                        }
                        catch (Exception) { }
                    }
                }

                //画像KEY取得
                Merge2    merge        = new Merge2();
                ArrayList imageKeyList = new ArrayList();

                messageFlg = 2;
                imageCount = 0;
                int aCount = bean.pathArr.Length;
                if (bean.checkBox1Flag)
                {
                    aCount = 1;
                }
                for (int a = 0; a < aCount; a++)
                {
                    string[] baseFileList = Directory.GetFiles(bean.pathArr[a], "*.jpg", SearchOption.TopDirectoryOnly);

                    /*                    for (int i = 0; i < baseFileList.Length; i++)
                     *                  {
                     *                      imageCount++;
                     *                      imageKeyList.Add(merge.MergePic(baseFileList[i]));
                     *
                     *                      if (i % 10 == 0)
                     *                      {
                     *                          backgroundWorker1.ReportProgress(imageCount);
                     *                      }
                     *                      if (e.Cancel || backgroundWorker1.CancellationPending)
                     *                      {
                     *                          return;
                     *                      }
                     *                  }*/

                    Thread[]      th          = new Thread[threadNum];
                    ThreadClass[] threadClass = new ThreadClass[threadNum];
                    Boolean       threadOKFlg = true;

                    //处理文件线程分割用
                    int imageListCut = (int)Math.Ceiling(baseFileList.Length * 1.0 / threadNum);
                    int startCut     = 0;
                    int endCut       = 0;

                    for (int i = 0; i < threadNum; i++)
                    {
                        endCut = startCut + imageListCut;

                        threadClass[i]            = new ThreadClass();
                        threadClass[i].startIndex = startCut;
                        threadClass[i].endIndex   = endCut;
                        threadClass[i].imageList  = baseFileList;

                        th[i] = new Thread(new ThreadStart(threadClass[i].imageThreadStart));
                        th[i].Start();

                        startCut = endCut;
                    }

                    while (true)
                    {
                        Thread.Sleep(1000);
                        imageCount = 0;
                        for (int i = 0; i < threadNum; i++)
                        {
                            imageCount = imageCount + threadClass[i].count;
                        }
                        backgroundWorker1.ReportProgress(imageCount);

                        //启动进程完了否
                        threadOKFlg = true;
                        for (int i = 0; i < threadNum; i++)
                        {
                            threadOKFlg = threadOKFlg && threadClass[i].endFlag;
                        }

                        //启动进程完了时处理
                        if (threadOKFlg)
                        {
                            for (int i = 0; i < threadNum; i++)
                            {
                                imageKeyList.AddRange(threadClass[i].imageKeyList);
                            }
                            break;
                        }

                        //关闭
                        if (backgroundWorker1.CancellationPending)
                        {
                            for (int i = 0; i < threadNum; i++)
                            {
                                th[i].Abort();
                            }
                            e.Cancel = true;
                            return;
                        }
                    }
                }


                string key = "";
                myDictionary = new Dictionary <string, string>();
                //比较
                messageFlg = 3;
                for (int i = 0; i < imageKeyList.Count - 1; i++)
                {
                    for (int j = i + 1; j < imageKeyList.Count; j++)
                    {
                        //                        textFlg = ((ImageID_KIND2)imageKeyList[j]).Path + "1";
                        //                        backgroundWorker1.ReportProgress(i);

                        // 完全相同直接删除
                        if (bean.checkBox1Flag && merge.GetSameLevel((ImageID_KIND2)imageKeyList[i], (ImageID_KIND2)imageKeyList[j]) == 1.0)
                        {
                            try
                            {
                                //                                textFlg = ((ImageID_KIND2)imageKeyList[j]).Path + "2";
                                //                                backgroundWorker1.ReportProgress(i);

                                File.Delete(((ImageID_KIND2)imageKeyList[j]).Path);

                                imageKeyList.Remove(imageKeyList[j]);
                                j--;
                            }
                            catch (Exception)
                            {
                                if (File.GetAttributes(((ImageID_KIND2)imageKeyList[j]).Path).ToString().IndexOf("ReadOnly") != -1)
                                {
                                    System.IO.File.SetAttributes(((ImageID_KIND2)imageKeyList[j]).Path, System.IO.FileAttributes.Normal); //去除只读
                                    File.Delete(((ImageID_KIND2)imageKeyList[j]).Path);
                                    j--;
                                }
                            }
                            continue;
                        }
                        else if (merge.GetSameLevel((ImageID_KIND2)imageKeyList[i], (ImageID_KIND2)imageKeyList[j]) >= samePercent)
                        {
                            //                            textFlg = ((ImageID_KIND2)imageKeyList[j]).Path + "3";
                            //                            backgroundWorker1.ReportProgress(i);

                            key = ((ImageID_KIND2)imageKeyList[i]).Path;
                            if (myDictionary.ContainsKey(key))
                            {
                                myDictionary[key] = myDictionary[key] + ";" + ((ImageID_KIND2)imageKeyList[j]).Path;
                            }
                            else
                            {
                                myDictionary.Add(key, ((ImageID_KIND2)imageKeyList[j]).Path);
                            }
                            //                           textFlg = ((ImageID_KIND2)imageKeyList[j]).Path + "4";
                            //                           backgroundWorker1.ReportProgress(i);
                        }
                        if (backgroundWorker1.CancellationPending)
                        {
                            e.Cancel = true;
                            return;
                        }
                    }
                    //                if (i % 10 == 0)
                    {
                        backgroundWorker1.ReportProgress(i);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }