代码之家  ›  专栏  ›  技术社区  ›  Arphile

我希望在加载大文件以显示时避免窗体冻结

  •  0
  • Arphile  · 技术社区  · 6 年前

    我做了我自己的FilePreviewHandler来预览一些像Explorer这样的文件,因为现在我做了自定义的文件浏览器。

    当我单击ListView上的项目时,我可以像explorer那样预览文件。

    我的文件浏览器中的PreviewHandler包含用于加载电影/文本文件的WebBrowser类,

    问题是这样的。

    PreviewHandler的代码区:

        public void Open( String FileName )
        {
            if ( !Enabled || Last == FileName )
            {
                return;
            }
            //FileName = DriveUtil.RevealPath( FileName );
    
            String FileType = MIMEAssistant.GetMIMEType( FileName );
            // Image Files
            if ( ImagePreviewTarget.Where( x => FileType.Contains( x ) ).Count() > 0 )
            {
                IsProgress = true;
                try
                {
                    if ( ImagePreviewHandler.Image != null )
                    {
                        ImagePreviewHandler.Image.Dispose();
                        GC.Collect();
                    }
                    Image TargetImage = Image.FromFile( FileName );
                    if ( TargetImage.Width > this.Width || TargetImage.Height > this.Height )
                    {
                        ImagePreviewHandler.SizeMode = PictureBoxSizeMode.Zoom;
                    }
                    else
                    {
                        ImagePreviewHandler.SizeMode = PictureBoxSizeMode.CenterImage;
                    }
                    ImagePreviewHandler.Image = TargetImage;
                    ImagePreviewHandler.BringToFront();
                }
                catch
                {
                    ImagePreviewHandler.Image = null;
                    GC.Collect();
                    ShellExPreviewHandler.BringToFront();
                    IsProgress = false;
                }
            }
    
    
            // WebPreviewArea
            // This area doesn't make any problem. So I hide my codes on here.
    
    
            // PdfPreviewArea
            // Currently I don't use Pdf PreviewHandler. So I also hide my codes on here.
    
            // Other extensions
            else
            {
                ShellExPreviewHandler.Open( FileName );
                ShellExPreviewHandler.BringToFront();
            }
    
            Last = FileName;
        }
    

    我试着用线,任务。运行,但所有这些都需要MyForm.调用绘制控件。

    螺纹部分=>

            Thread Preview = new Thread( (ThreadStart) =>
            {
                try
                {
                    if ( FilePreview != null && Browser.PreviewOpened )
                    {
                        Console.WriteLine( "FilePreview.Open Task Start" );
                        FilePreview.Open( Browser.SelectedPath );
                        Console.WriteLine( "FilePreview.Open Task Completed" );
                    }
                }
                catch ( Exception ex )
                {
                    Console.WriteLine( ex.StackTrace );
                }
    
                Process = false;
            } );
    
            Preview.Start();
            if ( Preview.Join( TimeSpan.Zero ) )
            {
                DateTime dt = DateTime.Now;
                TimeSpan Interval = TimeSpan.FromMilliseconds( 50 );
                while ( Process )
                {
                    if ( dt + Interval < DateTime.Now )
                    {
                        Application.DoEvents();
                        dt = DateTime.Now;
                    }
                }
            }
    

    任务。运行零件=>

    var t = Task.Run( () =>
            {
                try
                {
                    this.Invoke( new MethodInvoker( delegate
                    {
                        if ( Browser.SelectedItems.Count == 0 )
                        {
                            FilePreview.Initialize(); 
                            return;
                        }
                        else if ( Browser.SelectedItems[0].ForeColor == Color.Pink )
                        {
                            FilePreview.Initialize();
                            return;
                        }
    
                        if ( FilePreview != null && Browser.PreviewOpened )
                        {
                            FilePreview.Open( Browser.SelectedPath );
                        }
                    } ) );
                }
                catch
                {
    
                }
            } );
    

    BackgroundWorker=>

        private void PreviewWorker_DoWork( object sender, DoWorkEventArgs e )
        {
            FilePreviewHandler FilePreview = null;
            GCS.Windows.Forms.FileFolderList Browser = null;
    
            switch ( PreviewStatus ) {
                case BrowserStatus.Local:
                    FilePreview = LocalFilePreviewHandler;
                    Browser = LocalBrowser;
                    break;
                case BrowserStatus.Network:
                    FilePreview = NetFilePreviewHandler;
                    Browser = NetworkBrowser;
                    break;
            }
    
            if ( FilePreview == null )
            {
                return;
            }
    
            this.Invoke( new MethodInvoker( delegate
            {
                FilePreview.Open( Browser.SelectedPath );
            } ) );
        }
    

    在为PreviewHandler加载大文件时,有没有办法避免窗体冻结?

    0 回复  |  直到 6 年前