EDN Admin
Well-known member
I need that if the user click the cancel button while its creating the list and save the gif images so it will delete the files remain in the
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
tempRadarPngToGifDirectory and will stop the process.
Also if the user close the form in the middle also cancel it stop the process and delete remain files and close the form.
If the user close the form before process start or after it completed then just close the form dont delete anything.
And if the user cancelled the process in the middle also show a message on a lable in the form that the process have been cancelled or something like that.
I tried use the backgroundworker1 <span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small CancellationPending but it dosent work.
The code:
<span style="font-family:Consolas; font-size:x-small <font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.ComponentModel;
<span style="color:Blue; using System.Data;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Windows.Forms;
<span style="color:Blue; using System.IO;
<span style="color:Blue; using unfreez_wrapper;
<span style="color:Blue; using DannyGeneral;
<span style="color:Blue; namespace mws
{
<span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class Animation_Radar_Preview : Form
{
<span style="color:Blue; private MemoryStream _memSt = <span style="color:Blue; null;
Image img;
<span style="color:Blue; int numberOfFiles;
<span style="color:Blue; string nameOfStartFile;
<span style="color:Blue; string nameOfEndFile;
List<<span style="color:Blue; string> Directories;
DirectoryInfo dir1;
<span style="color:Blue; string previewFileName;
<span style="color:Blue; string previewDirectory;
UnFreezWrapper unfreez;
String radar_images_download_directory;
String tempRadarPngToGifDirectory;
<span style="color:Blue; string path_exe;
<span style="color:Blue; int mtpStart;
<span style="color:Blue; int mtpEnd;
<span style="color:Blue; public Animation_Radar_Preview()
{
InitializeComponent();
radar_images_download_directory = Options_DB.Get_Radar_Images_Download_Directory();
path_exe = Path.GetDirectoryName(Application.LocalUserAppDataPath);
tempRadarPngToGifDirectory = Path.Combine(path_exe, <span style="color:#A31515; "tempRadarPngToGifDirectory");
dir1 = <span style="color:Blue; new DirectoryInfo(tempRadarPngToGifDirectory);
<span style="color:Blue; foreach (FileInfo file <span style="color:Blue; in dir1.GetFiles())
{
file.Delete();
}
Directories = <span style="color:Blue; new List<<span style="color:Blue; string>();
Directories.Add(radar_images_download_directory);
Directories.Add(tempRadarPngToGifDirectory);
backgroundWorker1.RunWorkerAsync(Directories);
mtpStart = Picturebox1_Fullscreen.mtp1Start;
mtpEnd = Picturebox1_Fullscreen.mtp1End;
unfreez = <span style="color:Blue; new UnFreezWrapper();
previewDirectory = path_exe + <span style="color:#A31515; "\" + "previewDirectory";
<span style="color:Blue; if (Directory.Exists(previewDirectory))
{
}
<span style="color:Blue; else
{
Directory.CreateDirectory(previewDirectory);
}
previewFileName = previewDirectory + <span style="color:#A31515; "\" + "preview.gif";
}
<span style="color:Blue; private <span style="color:Blue; void backGroundWorker1_DoWork(<span style="color:Blue; object sender, DoWorkEventArgs e)
{
<span style="color:Blue; if (backgroundWorker1.CancellationPending)
{
e.Cancel = <span style="color:Blue; true; <span style="color:Blue; return;
}
BackgroundWorker bgw = (BackgroundWorker)sender;
List<<span style="color:Blue; string> Directories = (List<<span style="color:Blue; string>)e.Argument;
String radar_images_download_directory = Directories[0];
String tempRadarPngToGifDirectory = Directories[1];
List<<span style="color:Blue; string> myGifList = <span style="color:Blue; new List<<span style="color:Blue; string>();
DirectoryInfo dir1 = <span style="color:Blue; new DirectoryInfo(radar_images_download_directory);
FileInfo[] pngFileInfo = dir1.GetFiles(<span style="color:#A31515; "*.png");
<span style="color:Blue; int total = pngFileInfo.Length;
<span style="color:Blue; int Counter = 0;
<span style="color:Blue; int percentage = 0;
nameOfStartFile = pngFileInfo[0].Name;
<span style="color:Blue; for (<span style="color:Blue; int i = mtpStart; i< mtpEnd;i++)<span style="color:Green; //foreach (FileInfo SingleFileInfo in pngFileInfo)
{
<span style="color:Blue; try
{
String FileName = tempRadarPngToGifDirectory + <span style="color:#A31515; "\" + Counter.ToString("D6<span style="color:#A31515; ") + ".Gif";
<span style="color:Green; // loading png and convert it to gif
Image gifImage = Image.FromFile(pngFileInfo.FullName);
gifImage.Save(FileName, System.Drawing.Imaging.ImageFormat.Gif);
Counter += 1;
<span style="color:Green; // add the filename to the List
myGifList.Add(FileName);
nameOfEndFile = pngFileInfo.Name.ToString();
<span style="color:Green; // calculating percentage and report it
percentage = Counter * 100 / total;<span style="color:Green; //(total / 100) * Counter;
bgw.ReportProgress(percentage);
}
<span style="color:Blue; catch (Exception ex)
{
<span style="color:Green; // this is a bad idea inside a backgroundworker
MessageBox.Show(<span style="color:#A31515; "error >>>>>> " + ex);
}
}
numberOfFiles = myGifList.Count();
unfreez.MakeGIF(myGifList, previewFileName, 8, <span style="color:Blue; true);
bgw.ReportProgress(100);
<span style="color:Green; // whatever this will do
<span style="color:Green; //unfreez.MakeGIF(myGifList, previewFileName, animatedGifSpeed, loop);
<span style="color:Green; // setting the myGifList as the result of the method
e.Result = myGifList;
}
<span style="color:Blue; private <span style="color:Blue; void backGroundWorker1_ProgressChanged(<span style="color:Blue; object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
List<<span style="color:Blue; string> myGifList;
<span style="color:Blue; private <span style="color:Blue; void backGroundWorker1_Completed(<span style="color:Blue; object sender, RunWorkerCompletedEventArgs e)
{
<span style="color:Blue; if (e.Error != <span style="color:Blue; null)
{
<span style="color:Blue; throw e.Error;
}
<span style="color:Blue; else
{
myGifList = (List<<span style="color:Blue; string>)e.Result;
pictureBoxImage(previewFileName);
}
}
<span style="color:Blue; public <span style="color:Blue; static <span style="color:Blue; bool IsDirectoryEmpty(DirectoryInfo directory)
{
FileInfo[] files = directory.GetFiles();
DirectoryInfo[] subdirs = directory.GetDirectories();
<span style="color:Blue; return (files.Length == 0 && subdirs.Length == 0);
}
<span style="color:Blue; private <span style="color:Blue; void button2_Click(<span style="color:Blue; object sender, EventArgs e)
{
DialogResult result1;
result1 = <span style="color:Blue; new DialogResult();
SaveFileDialog sd = <span style="color:Blue; new SaveFileDialog();
sd.Title = <span style="color:#A31515; "Select a folder to save the animated gif to";
sd.InitialDirectory = "c:\";
sd.FileName = <span style="color:Blue; null;
sd.Filter = <span style="color:#A31515; "Gif File|*.gif;*.jpg|Gif|*.gif";
sd.FilterIndex = 1;
sd.RestoreDirectory = <span style="color:Blue; true;
result1 = sd.ShowDialog();
<span style="color:Blue; string file1 = sd.FileName;
<span style="color:Blue; if (result1 == DialogResult.OK)
{
File.Move(previewFileName, file1);
}
}
<span style="color:Blue; private <span style="color:Blue; void button1_Click(<span style="color:Blue; object sender, EventArgs e)
{
backgroundWorker1.CancelAsync();
}
<span style="color:Blue; public <span style="color:Blue; void pictureBoxImage(<span style="color:Blue; string pbImage)
{
Image img2 = <span style="color:Blue; null;
<span style="color:Blue; try
{
<span style="color:Blue; using (img = Image.FromFile(pbImage))
{
<span style="color:Green; //get the old image thats loaded from the _memSt memorystream
<span style="color:Green; //and dispose it
Image i = <span style="color:Blue; this.pictureBox1.Image;
<span style="color:Blue; this.pictureBox1.Image = <span style="color:Blue; null;
<span style="color:Blue; if (i != <span style="color:Blue; null)
i.Dispose();
<span style="color:Green; //grab the old stream
MemoryStream m = _memSt;
<span style="color:Green; //save the new image to this stream
_memSt = <span style="color:Blue; new MemoryStream();
img.Save(_memSt, System.Drawing.Imaging.ImageFormat.Gif);
<span style="color:Blue; if (m != <span style="color:Blue; null)
m.Dispose();
<span style="color:Green; //create our image to display
img2 = Image.FromStream(_memSt);
}
<span style="color:Blue; if (img2 != <span style="color:Blue; null)
pictureBox1.Image = img2;
label2.Text = numberOfFiles.ToString();
label6.Text = nameOfStartFile.ToString();
label4.Text = nameOfEndFile.ToString();
<span style="color:Green; //File.Delete(pbImage);
}
<span style="color:Blue; catch (Exception err)
{
Logger.Write(<span style="color:#A31515; "Animation Error >>> " + err);
}
}
}
}
[/code]
</font>
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small Thanks.
<hr class="sig danieli
View the full article
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
tempRadarPngToGifDirectory and will stop the process.
Also if the user close the form in the middle also cancel it stop the process and delete remain files and close the form.
If the user close the form before process start or after it completed then just close the form dont delete anything.
And if the user cancelled the process in the middle also show a message on a lable in the form that the process have been cancelled or something like that.
I tried use the backgroundworker1 <span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small CancellationPending but it dosent work.
The code:
<span style="font-family:Consolas; font-size:x-small <font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.ComponentModel;
<span style="color:Blue; using System.Data;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Windows.Forms;
<span style="color:Blue; using System.IO;
<span style="color:Blue; using unfreez_wrapper;
<span style="color:Blue; using DannyGeneral;
<span style="color:Blue; namespace mws
{
<span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class Animation_Radar_Preview : Form
{
<span style="color:Blue; private MemoryStream _memSt = <span style="color:Blue; null;
Image img;
<span style="color:Blue; int numberOfFiles;
<span style="color:Blue; string nameOfStartFile;
<span style="color:Blue; string nameOfEndFile;
List<<span style="color:Blue; string> Directories;
DirectoryInfo dir1;
<span style="color:Blue; string previewFileName;
<span style="color:Blue; string previewDirectory;
UnFreezWrapper unfreez;
String radar_images_download_directory;
String tempRadarPngToGifDirectory;
<span style="color:Blue; string path_exe;
<span style="color:Blue; int mtpStart;
<span style="color:Blue; int mtpEnd;
<span style="color:Blue; public Animation_Radar_Preview()
{
InitializeComponent();
radar_images_download_directory = Options_DB.Get_Radar_Images_Download_Directory();
path_exe = Path.GetDirectoryName(Application.LocalUserAppDataPath);
tempRadarPngToGifDirectory = Path.Combine(path_exe, <span style="color:#A31515; "tempRadarPngToGifDirectory");
dir1 = <span style="color:Blue; new DirectoryInfo(tempRadarPngToGifDirectory);
<span style="color:Blue; foreach (FileInfo file <span style="color:Blue; in dir1.GetFiles())
{
file.Delete();
}
Directories = <span style="color:Blue; new List<<span style="color:Blue; string>();
Directories.Add(radar_images_download_directory);
Directories.Add(tempRadarPngToGifDirectory);
backgroundWorker1.RunWorkerAsync(Directories);
mtpStart = Picturebox1_Fullscreen.mtp1Start;
mtpEnd = Picturebox1_Fullscreen.mtp1End;
unfreez = <span style="color:Blue; new UnFreezWrapper();
previewDirectory = path_exe + <span style="color:#A31515; "\" + "previewDirectory";
<span style="color:Blue; if (Directory.Exists(previewDirectory))
{
}
<span style="color:Blue; else
{
Directory.CreateDirectory(previewDirectory);
}
previewFileName = previewDirectory + <span style="color:#A31515; "\" + "preview.gif";
}
<span style="color:Blue; private <span style="color:Blue; void backGroundWorker1_DoWork(<span style="color:Blue; object sender, DoWorkEventArgs e)
{
<span style="color:Blue; if (backgroundWorker1.CancellationPending)
{
e.Cancel = <span style="color:Blue; true; <span style="color:Blue; return;
}
BackgroundWorker bgw = (BackgroundWorker)sender;
List<<span style="color:Blue; string> Directories = (List<<span style="color:Blue; string>)e.Argument;
String radar_images_download_directory = Directories[0];
String tempRadarPngToGifDirectory = Directories[1];
List<<span style="color:Blue; string> myGifList = <span style="color:Blue; new List<<span style="color:Blue; string>();
DirectoryInfo dir1 = <span style="color:Blue; new DirectoryInfo(radar_images_download_directory);
FileInfo[] pngFileInfo = dir1.GetFiles(<span style="color:#A31515; "*.png");
<span style="color:Blue; int total = pngFileInfo.Length;
<span style="color:Blue; int Counter = 0;
<span style="color:Blue; int percentage = 0;
nameOfStartFile = pngFileInfo[0].Name;
<span style="color:Blue; for (<span style="color:Blue; int i = mtpStart; i< mtpEnd;i++)<span style="color:Green; //foreach (FileInfo SingleFileInfo in pngFileInfo)
{
<span style="color:Blue; try
{
String FileName = tempRadarPngToGifDirectory + <span style="color:#A31515; "\" + Counter.ToString("D6<span style="color:#A31515; ") + ".Gif";
<span style="color:Green; // loading png and convert it to gif
Image gifImage = Image.FromFile(pngFileInfo.FullName);
gifImage.Save(FileName, System.Drawing.Imaging.ImageFormat.Gif);
Counter += 1;
<span style="color:Green; // add the filename to the List
myGifList.Add(FileName);
nameOfEndFile = pngFileInfo.Name.ToString();
<span style="color:Green; // calculating percentage and report it
percentage = Counter * 100 / total;<span style="color:Green; //(total / 100) * Counter;
bgw.ReportProgress(percentage);
}
<span style="color:Blue; catch (Exception ex)
{
<span style="color:Green; // this is a bad idea inside a backgroundworker
MessageBox.Show(<span style="color:#A31515; "error >>>>>> " + ex);
}
}
numberOfFiles = myGifList.Count();
unfreez.MakeGIF(myGifList, previewFileName, 8, <span style="color:Blue; true);
bgw.ReportProgress(100);
<span style="color:Green; // whatever this will do
<span style="color:Green; //unfreez.MakeGIF(myGifList, previewFileName, animatedGifSpeed, loop);
<span style="color:Green; // setting the myGifList as the result of the method
e.Result = myGifList;
}
<span style="color:Blue; private <span style="color:Blue; void backGroundWorker1_ProgressChanged(<span style="color:Blue; object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
List<<span style="color:Blue; string> myGifList;
<span style="color:Blue; private <span style="color:Blue; void backGroundWorker1_Completed(<span style="color:Blue; object sender, RunWorkerCompletedEventArgs e)
{
<span style="color:Blue; if (e.Error != <span style="color:Blue; null)
{
<span style="color:Blue; throw e.Error;
}
<span style="color:Blue; else
{
myGifList = (List<<span style="color:Blue; string>)e.Result;
pictureBoxImage(previewFileName);
}
}
<span style="color:Blue; public <span style="color:Blue; static <span style="color:Blue; bool IsDirectoryEmpty(DirectoryInfo directory)
{
FileInfo[] files = directory.GetFiles();
DirectoryInfo[] subdirs = directory.GetDirectories();
<span style="color:Blue; return (files.Length == 0 && subdirs.Length == 0);
}
<span style="color:Blue; private <span style="color:Blue; void button2_Click(<span style="color:Blue; object sender, EventArgs e)
{
DialogResult result1;
result1 = <span style="color:Blue; new DialogResult();
SaveFileDialog sd = <span style="color:Blue; new SaveFileDialog();
sd.Title = <span style="color:#A31515; "Select a folder to save the animated gif to";
sd.InitialDirectory = "c:\";
sd.FileName = <span style="color:Blue; null;
sd.Filter = <span style="color:#A31515; "Gif File|*.gif;*.jpg|Gif|*.gif";
sd.FilterIndex = 1;
sd.RestoreDirectory = <span style="color:Blue; true;
result1 = sd.ShowDialog();
<span style="color:Blue; string file1 = sd.FileName;
<span style="color:Blue; if (result1 == DialogResult.OK)
{
File.Move(previewFileName, file1);
}
}
<span style="color:Blue; private <span style="color:Blue; void button1_Click(<span style="color:Blue; object sender, EventArgs e)
{
backgroundWorker1.CancelAsync();
}
<span style="color:Blue; public <span style="color:Blue; void pictureBoxImage(<span style="color:Blue; string pbImage)
{
Image img2 = <span style="color:Blue; null;
<span style="color:Blue; try
{
<span style="color:Blue; using (img = Image.FromFile(pbImage))
{
<span style="color:Green; //get the old image thats loaded from the _memSt memorystream
<span style="color:Green; //and dispose it
Image i = <span style="color:Blue; this.pictureBox1.Image;
<span style="color:Blue; this.pictureBox1.Image = <span style="color:Blue; null;
<span style="color:Blue; if (i != <span style="color:Blue; null)
i.Dispose();
<span style="color:Green; //grab the old stream
MemoryStream m = _memSt;
<span style="color:Green; //save the new image to this stream
_memSt = <span style="color:Blue; new MemoryStream();
img.Save(_memSt, System.Drawing.Imaging.ImageFormat.Gif);
<span style="color:Blue; if (m != <span style="color:Blue; null)
m.Dispose();
<span style="color:Green; //create our image to display
img2 = Image.FromStream(_memSt);
}
<span style="color:Blue; if (img2 != <span style="color:Blue; null)
pictureBox1.Image = img2;
label2.Text = numberOfFiles.ToString();
label6.Text = nameOfStartFile.ToString();
label4.Text = nameOfEndFile.ToString();
<span style="color:Green; //File.Delete(pbImage);
}
<span style="color:Blue; catch (Exception err)
{
Logger.Write(<span style="color:#A31515; "Animation Error >>> " + err);
}
}
}
}
[/code]
</font>
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small Thanks.
<hr class="sig danieli
View the full article