Now it was working good without problems untill i wanted to make the downloads async. Since i changed it to async im getting exception and i cant figure out what is the problem or/and how to solve it.
So first thing in the constructor i added this line wich register me to the DownloadFileCompleted event:
Expand|Select|Wrap|Line Numbers
- Client.DownloadFileCompleted += new AsyncCompletedEventHandler(Client_DownloadFileCompleted);
Inside this timer1 event code im also calling a function i called it: filesdownload()
Expand|Select|Wrap|Line Numbers
- private void timer1_Tick(object sender, EventArgs e)
- {
- try
- {
- numbers = Convert.ToInt64(textBox1.Text);
- if (numbers <= 0)
- {
- timer1.Stop();
- button1.Enabled = true;
- textBox1.Enabled = true;
- MessageBox.Show("Value cannot be zero or below zero");
- textBox1.Text = "";
- }
- else
- {
- Numbers_Timer = Numbers_Timer + 1;
- if (Numbers_Timer >= numbers)
- {
- try
- {
- filesdownload();
- number_of_files_in_current_directory = Directory.GetFiles(sf, "radar*.jpg");
- label8.Text = "Current Number Of Files In The Selected Directory Are: " + number_of_files_in_current_directory.Length;
- button1.Enabled = false;
- Numbers_Timer = 0;
- }
- catch (Exception Local_Timer1_Exceptions)
- {
- Logger.Write("Error :" + Local_Timer1_Exceptions);
- }
- }
- time_left = numbers - Numbers_Timer;
- label1.Text = "Time left for the next radar image to be download: " + time_left;
- }
- }
- catch (Exception General_Exceptions)
- {
- timer1.Stop();
- textBox1.Clear();
- button1.Enabled = true;
- textBox1.Enabled = true;
- Numbers_Timer = 0;
- }
- }
Expand|Select|Wrap|Line Numbers
- private void filesdownload()
- {
- string url;
- url = "http://www.ims.gov.il/Ims/Pages/RadarImage.aspx?Row=9&TotalImages=10&LangID=1&Location=";
- if (!Directory.Exists(sf))
- {
- timer1.Enabled = false;
- }
- Client.DownloadFileAsync(new Uri(url), temp_dir + temp_file);
- }
Expand|Select|Wrap|Line Numbers
- void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
- {
- string Next_File;
- bool file_compare;
- int i;
- bool bad_file;
- progressBar1.Maximum = 10;
- progressBar1.Minimum = 0;
- progressBar1.TabIndex = 0;
- progressBar1.Value = 0;
- progressBar1.Step = 10;
- progressBar1.PerformStep();
- button1.Enabled = true;
- file_array_dl = Directory.GetFiles(sf, "radar*.jpg");
- if (file_array_dl.Length == 0)
- {
- File.Copy(temp_dir + temp_file, sf + @"\radar001.jpg");
- }
- else
- {
- i = last_image_file();
- last_file = sf + @"\radar" + i.ToString("D3") + ".jpg";
- File.Copy(last_file, bad_file_test_dir + testing_file);
- bad_file = Bad_File_Testing(bad_file_test_dir + testing_file);
- if (bad_file == true)
- {
- File.Delete(bad_file_test_dir + testing_file);
- if (File.Exists(last_file))
- {
- file_compare = File_Utility.File_Comparison(temp_dir + temp_file, last_file);
- if (file_compare == true)
- {
- return;
- }
- i = last_image_file() + 1;
- Next_File = sf + @"\radar" + i.ToString("D3") + ".jpg";
- File.Copy(temp_dir + temp_file, Next_File);
- pictureBox1.Load(Next_File);
- progressBar1.Maximum = 10;
- progressBar1.Minimum = 0;
- progressBar1.TabIndex = 0;
- progressBar1.Value = 0;
- progressBar1.Step = 10;
- progressBar1.PerformStep();
- button1.Enabled = true;
- }
- }
- else
- {
- File.Delete(last_file);
- File.Delete(bad_file_test_dir + testing_file);
- }
- }
- Logger.Write("File Downloaded");
- }
Now when im connected to the internet the downloading is working fine no problems.
When i disconnected from the internet manualy for testing then the program dosent stuck and its keeping downloading i mean its trying to download like once counting 3 2 1 then again 3 2 1 but in the second time the seconds dosent reset back to 3 but its keeping counting 0 -1 -2 -3 -4 -5....and so on untill i stop the program.
Now i tried using breakpoints and couldnt find the problem or how to fix it.
The error/exception im getting when im disconnected from the internet is:
16/10/2010 13:09:29 -> Error :System.NotSupportedException: WebClient does not support concurrent I/O operations.
at System.Net.WebClient.ClearWebClientState()
at System.Net.WebClient.DownloadFileAsync(Uri address, String fileName, Object userToken)
at WindowsFormsApplication1.Form1.filesdownload() in D:\C-Sharp\Download File\Downloading-File-Project-Version-008\Downloading File\Downloading File\Form1.cs:line 308
at WindowsFormsApplication1.Form1.timer1_Tick(Object sender, EventArgs e) in D:\C-Sharp\Download File\Downloading-File-Project-Version-008\Downloading File\Downloading File\Form1.cs:line 265
Also another thing is when im disconnected from the internet and it keep downloading try to download im getting an error on:
Program.cs
Under the last line wich is Application.Run(new Form1()); on the closing symbol } im getting exception:
Exception has been thrown by the target of an invocation
System.Reflection.TargetInvocationException was unhandled
Message=Exception has been thrown by the target of an invocation.
Source=mscorlib
StackTrace:
at System.RuntimeMethodHandle._InvokeMethodFast(Objec t target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Windows.Forms.Control.InvokeMarshaledCallba ckDo(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallba ckHelper(Object obj)
at System.Threading.ExecutionContext.runTryCode(Objec t userData)
at System.Runtime.CompilerServices.RuntimeHelpers.Exe cuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.Run(ExecutionCon text executionContext, ContextCallback callback, Object state)
at System.Windows.Forms.Control.InvokeMarshaledCallba ck(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallba cks()
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager. System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32 reason, ApplicationContext context)
at WindowsFormsApplication1.Program.Main() in D:\C-Sharp\Download File\Downloading-File-Project-Version-008\Downloading File\Downloading File\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.Runtime.Hosting.ApplicationActivator.Create Instance(ActivationContext activationContext, String[] activationCustomData)
at Microsoft.VisualStudio.HostingProcess.HostProc.Run UsersAssemblyDebugInZone()
at System.Threading.ExecutionContext.Run(ExecutionCon text executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.IO.IOException
Message=The file 'C:\Users\Chocolade\AppData\Local\Microsoft\Window sFormsApplication1\Bad_File-Test\testing_file.jpg' already exists.
Source=mscorlib
StackTrace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite)
at System.IO.File.Copy(String sourceFileName, String destFileName)
at WindowsFormsApplication1.Form1.Client_DownloadFile Completed(Object sender, AsyncCompletedEventArgs e) in D:\C-Sharp\Download File\Downloading-File-Project-Version-008\Downloading File\Downloading File\Form1.cs:line 1386
InnerException:
I dont know why the async dosent work and what did i do wrong.
Thanks for helping.