コード例 #1
0
ファイル: BstPicLoader.cs プロジェクト: wind1up/BladeSoulTool
        private static void RunLoading(string url, string path, PictureBox picture, TextBox box = null)
        {
            new Thread(() =>
            {
                Timer loadingTimer = null;
                if (!BstPicLoader.LoadingTimers.ContainsKey(picture))
                {
                    // 更新图片成读取状态,如果Dictionary里已经有这个PictureBox的Timer了,说明loading图已经加载了
                    var loadingGif = new BstGifImage(BstManager.PathLoadingGif)
                    {
                        ReverseAtEnd = false
                    };
                    loadingTimer          = new Timer(50);
                    loadingTimer.Elapsed += (s, e) =>
                    {
                        MethodInvoker loadingAction = () =>
                        {
                            picture.Image = loadingGif.GetNextFrame();
                        };
                        try
                        {
                            picture.BeginInvoke(loadingAction);
                        }
                        catch (InvalidOperationException ex)
                        {
                            BstLogger.Instance.Log(ex.ToString());
                            // 因为我们可能会在GUI_Picture的UI中的PictureBox里显示loading动态图
                            // 而上述的窗口可能在关闭后被销毁,这里我们需要处理窗口被销毁后的错误
                            // 这时候Timer应该在Dictionary里注册过了
                            if (BstPicLoader.LoadingTimers.ContainsKey(picture))
                            {
                                var timer     = BstPicLoader.LoadingTimers[picture];
                                timer.Enabled = false;
                                BstPicLoader.LoadingTimers.Remove(picture);
                                timer.Dispose();
                            }
                        }
                    };
                    BstPicLoader.LoadingTimers.Add(picture, loadingTimer);
                    loadingTimer.Enabled = true;
                }
                else
                {
                    loadingTimer = BstPicLoader.LoadingTimers[picture];
                }

                // 检查是否有本地缓存
                byte[] blob = null;
                if (File.Exists(path))
                {
                    // 本地缓存存在,直接读取
                    blob = BstManager.GetBytesFromFile(path);
                }
                else
                {
                    // 下载图片
                    blob = BstManager.DownloadImageFile(url, path);
                    if (blob == null)
                    {
                        // 图片下载失败
                        BstManager.ShowMsgInTextBox(box, string.Format(BstI18NLoader.Instance.LoadI18NValue("BstPicLoader", "picDownloadFailed"), url));
                        // 停止动态图更新
                        loadingTimer.Enabled = false;
                        BstPicLoader.LoadingTimers.Remove(picture);
                        loadingTimer.Dispose();
                        // 更新下载失败icon
                        var errorIconBitmap             = BstManager.ConvertByteToImage(BstManager.Instance.ErrorIconBytes);
                        MethodInvoker updateErrorAction = () => picture.Image = errorIconBitmap;
                        try
                        {
                            picture.BeginInvoke(updateErrorAction);
                        }
                        catch (Exception ex)
                        {
                            BstLogger.Instance.Log(ex.ToString());
                        }
                        return;
                    }
                }

                loadingTimer.Enabled = false;               // 加载完成,停止动态loading图的更新
                BstPicLoader.LoadingTimers.Remove(picture); // 加载完成,删除Dictionary里注册的Timer
                loadingTimer.Dispose();

                BstManager.ShowMsgInTextBox(box, string.Format(BstI18NLoader.Instance.LoadI18NValue("BstPicLoader", "picDownloadSucceed"), url));

                // 转换成位图
                var bitmap = BstManager.ConvertByteToImage(blob);
                // 更新图片内容
                MethodInvoker updateAction = () => picture.Image = bitmap;
                try
                {
                    picture.BeginInvoke(updateAction);
                }
                catch (Exception ex)
                {
                    BstLogger.Instance.Log(ex.ToString());
                }
            }).Start();
        }
コード例 #2
0
        public void Run()
        {
            var updatedCount  = 0;
            var isAnyTaskLeft = true;

            while (isAnyTaskLeft)
            {
                var task = this._queue.Dequeue();

                // 加载图片
                byte[] pic          = null;
                var    downloadUrl  = BstManager.GetIconPicUrl(task.ElementData);
                var    imgCachePath = BstManager.GetIconPicTmpPath(task.ElementData);
                if (File.Exists(imgCachePath))
                {
                    // 已有缓存文件
                    pic = BstManager.GetBytesFromFile(imgCachePath);
                }
                else
                {
                    // 没有缓存文件,需要下载
                    pic = BstManager.DownloadImageFile(downloadUrl, imgCachePath);
                }

                // 检查结果并更新UI
                if (pic == null)
                {
                    BstManager.ShowMsgInTextBox(task.Box, string.Format(BstI18NLoader.Instance.LoadI18NValue("BstIconLoader", "iconDownloadFailed"), downloadUrl));
                    // 更新加载失败icon
                    task.Table.Rows[task.RowId][task.ColId] = BstManager.Instance.ErrorIconBytes;
                }
                else
                {
                    BstManager.ShowMsgInTextBox(task.Box, string.Format(BstI18NLoader.Instance.LoadI18NValue("BstIconLoader", "iconDownloadSucceed"), downloadUrl));
                    // 更新图片
                    task.Table.Rows[task.RowId][task.ColId] = pic;
                }

                // 检查icon加载进度,并刷新ui
                updatedCount++;
                if (updatedCount >= BstIconLoader.UpdateThrottle)
                {
                    MethodInvoker tableUpdateAction = () => task.Grid.Refresh();
                    try
                    {
                        task.Grid.BeginInvoke(tableUpdateAction);
                    }
                    catch (Exception ex)
                    {
                        BstLogger.Instance.Log(ex.ToString());
                    }
                    updatedCount = 0;
                }

                if (this._queue.Count != 0)
                {
                    continue;                         // 仍旧还有工作未完成,继续轮询
                }
                // 当前工作队列已清空,最后更新UI,设置关闭状态
                MethodInvoker tableFinalUpdateAction = () => task.Grid.Refresh();
                task.Grid.BeginInvoke(tableFinalUpdateAction);
                BstManager.DisplayDataGridViewVerticalScrollBar(task.Grid);
                BstManager.ShowMsgInTextBox(task.Box, BstI18NLoader.Instance.LoadI18NValue("BstIconLoader", "iconDownloadAllDone"));
                BstLogger.Instance.Log("[BstIconLoader] Queued works all done, thread exit ...");
                isAnyTaskLeft = false;
            }
        }