public static async Task <int> ImportModelAsync(string path, IProgress <float> fileProgress) { if (!FileSystemOps.PathIsValid(path)) { return(-1); } FileStream file = new FileStream( path, FileMode.Open, FileAccess.Read, FileShare.Read, //important - //we want other programs to be able to read while we're reading ioBufferSize, true); //actually make stream an async stream! //fileLoader.RunWorkerAsync(path); byte[] data = new byte[file.Length]; int dataHead = 0; //int bytesRead = 0; //read file into memory //var readFileTask = Task.Run(async () => //{ while (!shouldCancel) { if (dataHead >= data.Length) { break; } int maxBytesRead = Math.Min(ioBufferSize, data.Length - dataHead); int bytesRead = await file.ReadAsync(data, dataHead, maxBytesRead); dataHead += bytesRead; dataHead = MathHelpers.Clamp(dataHead, 0, data.Length); if (fileProgress != null) { fileProgress.Report(((float)bytesRead / data.Length) * 100f); } } //}); if (shouldCancel) { //operation canceled; report something and reset the canceler shouldCancel = false; return(-1); } //await readFileTask; //now proceed as normal int result = importModelFromBuffer(data); return(result); }
public static ImageSource GetImageFromIcon(string path, int imageSize) { try { System.Drawing.Icon iconRes = FileSystemOps.GetIcon(path); //now convert that into a WPF source return(System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(iconRes.Handle, Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(imageSize, imageSize))); } catch { //set to no source, and default icon size return(null); } }
public async static Task ExportModelAsync(string path, int mdlIdx) { if (!FileSystemOps.PathIsValid(path)) { return; } FileStream file = new FileStream( path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read, //important - //we want other programs to be able to read while we're reading ioBufferSize, true); //actually make stream an async stream! //file.Seek(0, SeekOrigin. var data = exportModelToBuffer(mdlIdx); await file.WriteAsync(data, 0, data.Length); }