예제 #1
0
        private void btnTask_Click(object sender, RoutedEventArgs e)
        {
            string path;
            int    quality;
            int    maxLine;

            if (!CheckInit(out path, out quality, out maxLine))
            {
                return;
            }

            string[] pathList = GetAllFilePathList(path);
            sb.Clear();
            curProgress = 0;
            maxProgress = pathList.Length;

            Task[] taskList = new Task[pathList.Length];
            sw.Restart();
            btnTask.IsEnabled = false;
            for (int i = 0; i < taskList.Length; i++)
            {
                int index = i;
                taskList[i] = Task.Run(() =>
                {
                    string sFile  = pathList[index];
                    string dFile  = sFile + ".jpg";
                    string errMsg = "";
                    //Console.WriteLine("开始处理:"+ sFile);
                    if (!HandlerForImg.GetPicThumbnail(sFile, dFile, ref errMsg, quality, maxLine))
                    {
                        sb.AppendLine(sFile);
                        sb.AppendLine(errMsg);
                    }
                    Dispatcher.BeginInvoke(new InvokeDelegate(InvokeMethod), DispatcherPriority.ApplicationIdle);
                    curProgress++;
                    //Console.WriteLine("处理完毕:" + sFile);
                });
            }

            Task.Run(() =>
            {
                Task.WaitAll(taskList);
                Dispatcher.BeginInvoke(new InvokeDelegate(InvokeMethod), DispatcherPriority.ApplicationIdle);
                sw.Stop();
                string str;
                if (sb.Length > 0)
                {
                    WriteToFile.WriteInToFile(sb.ToString());
                    sb.Clear();
                    str = string.Format("完成!!耗时{0}。\n失败{1}次,见log.txt。", sw.Elapsed.ToString(), sb.Length / 2);
                }
                else
                {
                    str = string.Format("完成!!耗时{0}:。", sw.Elapsed.ToString());
                }
                MessageBox.Show(str);
                btnTask.IsEnabled = true;
            });
        }
예제 #2
0
        private void BtnInvoke_Click(object sender, RoutedEventArgs e)
        {
            string path;
            int    quality;
            int    maxLine;

            if (!CheckInit(out path, out quality, out maxLine))
            {
                return;
            }

            string[] pathList = GetAllFilePathList(path);
            sb.Clear();
            curProgress = 0;
            maxProgress = pathList.Length;

            Action[] actionList = new Action[pathList.Length];
            Console.WriteLine("start");
            sw.Restart();

            for (int i = 0; i < actionList.Length; i++)
            {
                int index = i;
                actionList[i] = () =>
                {
                    string sFile  = pathList[index];
                    string dFile  = sFile + ".jpg";
                    string errMsg = "";
                    //Console.WriteLine("开始处理:" + sFile);
                    if (!HandlerForImg.GetPicThumbnail(sFile, dFile, ref errMsg, quality, maxLine))
                    {
                        Console.WriteLine(errMsg);
                        sb.AppendLine(sFile);
                    }
                    Dispatcher.BeginInvoke(new InvokeDelegate(InvokeMethod), DispatcherPriority.ApplicationIdle);
                    curProgress++;
                    //Console.WriteLine("处理完毕:" + sFile);
                };
            }

            Task.Run(() =>
            {
                Parallel.Invoke(actionList);
                Console.WriteLine("finish   " + sw.Elapsed);
            });
        }
예제 #3
0
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string path;
            int    quality;
            int    maxLine;

            if (!CheckInit(out path, out quality, out maxLine))
            {
                return;
            }

            string[] pathList = GetAllFilePathList(path);
            sb.Clear();

            curProgress = 0;
            maxProgress = pathList.Length;

            sw.Restart();

            ParallelOptions po = new ParallelOptions();

            po.MaxDegreeOfParallelism = pathList.Length / 2;
            var loopResult = Parallel.For(0, pathList.Length, i =>
            {
                string sFile  = pathList[i];
                string dFile  = sFile + ".jpg";
                string errMsg = "";
                if (!HandlerForImg.GetPicThumbnail(sFile, dFile, ref errMsg, quality, maxLine))
                {
                    Console.WriteLine(errMsg);
                    sb.AppendLine(sFile);
                }
                Dispatcher.BeginInvoke(new InvokeDelegate(InvokeMethod), DispatcherPriority.ApplicationIdle);
                curProgress++;
            });

            sw.Stop();
            Console.WriteLine(loopResult.IsCompleted + "finish  " + sw.Elapsed);
            WriteToFile.WriteInToFile(sb.ToString());
        }