我制作了自己的FilePreviewHandler来预览诸如Explorer之类的某些文件,因为当前我制作了自定义文件浏览器。
当我单击ListView上的项目时,可以预览资源管理器之类的文件。
文件浏览器中的PreviewHandler包含用于加载电影/文本文件的WebBrowser类, PictureBox用于加载图像文件,而ShellExPreviewHandler用于其他扩展。
参与的问题是这样的。
当我尝试加载100mb或更大的大文件或8000x4000像素的大图像文件之类的大文件时,表格会冻结直到它加载文件。
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;
}
我尝试过的内容:
我试图为此使用Thread,Task.Run,Backgroundworker,但是所有这些都需要MyForm.Invoke来绘制控件。
线程部分=>
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;
}
}
}
Task.Run Part =>
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加载大文件时冻结表格?