473,396 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to hook two client.downloadfileasync to downloadfilecompleted?

In my code in two places im using webclient to download images from two other sites every 5 minutes.

This is the first one:
Expand|Select|Wrap|Line Numbers
  1. Client.DownloadFileAsync(myUri,Path.Combine( temp_dir + temp_file));
  2.  
And this is the second one:
Expand|Select|Wrap|Line Numbers
  1. Client.DownloadFileAsync(mySatelliteUri, Path.Combine(temp_dir + satellite_file_name));
  2.  
Now im registering once to downloadfilecompleted event in my constructor:
Expand|Select|Wrap|Line Numbers
  1. Client.DownloadFileCompleted += new AsyncCompletedEventHandler(Client_DownloadFileCompleted);
  2.  
Now in the downloadfilecompleted eventArgs:
Expand|Select|Wrap|Line Numbers
  1.  private void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
  2. {
  3. }
  4.  
Im doing some checkings and test on the downloaded files.
But since im downloading files from another sites even if t hey are both images but they have another content i want to seperate the actions im doing on the files inside the downloadfilecompleted eventargs event.

Since both files are downloading to the same downloadfilecompleted event i need to make somehow that when one of the files is downloaded so only some of the code in the completed event will take care of it and if the other file is downloaded then it will take care of it.

I mean i have one downloadfilecompleted event and both files are getting to there after downloaded i mean getting to this event how can i seperate the tests and actions to each file?

This is the code of the downloadfilecompleted event:

Expand|Select|Wrap|Line Numbers
  1. private void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
  2.          {
  3.              if (e.Error != null)
  4.              {
  5.                  pictureBox2.Load(@"d:\Weather_Michmoret.bmp");
  6.                  return;
  7.  
  8.              }
  9.              if (!e.Cancelled)
  10.              {
  11.                  string Next_File;
  12.                  bool file_compare;
  13.                  int i;
  14.                  bool bad_file;
  15.                  file_array_dl = Directory.GetFiles(sf, "radar*.jpg");
  16.                  if (file_array_dl.Length == 0)
  17.                  {
  18.                      File.Copy(temp_dir + temp_file, sf + @"\radar001.jpg");
  19.                      realtime_write_to_Log("Last Downloaded file is :" + sf + @"\radar001.jpg");
  20.                  }
  21.                  else
  22.                  {
  23.                      File.Copy(temp_dir + temp_file, bad_file_test_dir + testing_file);
  24.                      bad_file = Bad_File_Testing(bad_file_test_dir + testing_file);
  25.                      if (bad_file == false)
  26.                      {
  27.                          File.Delete(bad_file_test_dir + testing_file);
  28.                          return;
  29.                      }
  30.                      else
  31.                      {
  32.                          File.Delete(bad_file_test_dir + testing_file);
  33.                          i = last_image_file();
  34.                          last_file = sf + @"\radar" + i.ToString("D3") + ".jpg";
  35.                          File.Copy(last_file, bad_file_test_dir + testing_file);
  36.                          bad_file = Bad_File_Testing(bad_file_test_dir + testing_file);
  37.                          if (bad_file == true)
  38.                          {
  39.                              File.Delete(bad_file_test_dir + testing_file);
  40.                              if (File.Exists(last_file))
  41.                              {
  42.                                  file_compare = File_Utility.File_Comparison(temp_dir + temp_file, last_file);
  43.                                  if (file_compare == true)
  44.                                  {
  45.                                      return;
  46.                                  }
  47.  
  48.                                  i = last_image_file() + 1;
  49.                                  Next_File = sf + @"\radar" + i.ToString("D3") + ".jpg";
  50.                                  File.Copy(temp_dir + temp_file, Next_File);
  51.                                  realtime_write_to_Log("Last Downloaded file is :" + Next_File);
  52.                                  pictureBox1.Load(Next_File); // parameter is not valid כי הוא מנסה להעלות פה תמונה דפוקה שמשום מה הגיעה לספרייה של המכ"ם במקום להימחק. התמונה הדפוקה ירדה גם לספרייה הזמנית. לבדוק מדוע היא לא טופלה.
  53.                                  button1.Enabled = true;
  54.                              }
  55.                          }
  56.                          else
  57.                          {
  58.                              File.Delete(last_file);
  59.                              File.Delete(bad_file_test_dir + testing_file);
  60.                          }
  61.                      }
  62.                  }
  63.              }
  64.          }
  65.  
Now i need in there to make another code same thing but instead of radar files to take care of satellite files. since the radar files are downloading to a specific directory and the satellite images are downloading to another directory.
But if ill put there the same code wich i have allready in my general code it will be a mess since both files are getting to the same completed event and how it will know to wich code to go and make the tests?

In this example of downloadfilecompleted event im taking care of the radar images.
Now i want to make there in the downloadfilecompleted code to take care for the satellite images.

In other words in the downloadfilecompleted event i want to make once a cheking for:
Expand|Select|Wrap|Line Numbers
  1. Client.DownloadFileAsync(myUri,Path.Combine( temp_dir + temp_file));
  2.  
And if the other file is downloaded so the completed event will take care for the
Expand|Select|Wrap|Line Numbers
  1. Client.DownloadFileAsync(mySatelliteUri, Path.Combine(temp_dir + satellite_file_name));
  2.  
If someone please can show me a sample how to do it and explain also how to do it cuz i cant figure it out for long time now.
And for now the files are getting to the same event and thats making problems.

Thanks for helping.
Nov 1 '10 #1
1 6464
Bassem
344 100+
Hello,

The DownloadFileAsync Method has +1 overload. Take a look into it.
Expand|Select|Wrap|Line Numbers
  1. public void DownloadFileAsync(Uri address, string fileName, object userToken);
  2.         //   userToken:
  3.         //     A user-defined object that is passed to the method invoked when the asynchronous
  4.         //     operation completes.
  5.  
Now you can distinguish between any number of tasks (or type of files in your case).
So, your two downloads would be something like this:
Expand|Select|Wrap|Line Numbers
  1. Client.DownloadFileAsync(myUri,Path.Combine( temp_dir + temp_file), "radar");
  2. Client.DownloadFileAsync(mySatelliteUri, Path.Combine(temp_dir + satellite_file_name), "satellite");
When the Download is completed (i.e. in client_DownloadFileCompleted) check the e.UserState if it "radar" or "satellite" and do your action based on that.

Hoping this would help.
Nov 2 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Russ | last post by:
Hello, I have an application that consists of both thin client portions and fat client portions. One such view consists of a fat client treeview on the left, and a thin client asp.net webapp...
7
by: jpierson | last post by:
Hi, I am tryin to create a keyboard hook that sends the keystroke ctrl + pause/break. I haven't used keyboard hooks before so I'm not too sure how to use them public int MyKeyboardProc(int...
6
by: Giovanni | last post by:
Hi Guys, Really strange problem I am experiencing... I have setup a virtual directory with Read Only permissions on an ISA/IIS server (SBS 2003). In that virt. dir., I placed 1 file...
0
by: zeng.hui.stephen | last post by:
I download the demo http://msdn.microsoft.com/msdnmag/issues/02/10/cuttingedge/. I inherite the demo, and write my code. I want to use Hook to monitor C++ Edit change. I use a C# form...
3
by: Ricardo Vazquez | last post by:
THIS IS MY SCENARIO: - PBX (a private telephone exchange or switch) - A Telephony server running on computer "A" (it communicates with the PBX via IP) - An ASP.NET application (running on...
5
by: Jeff Schwab | last post by:
Q1: When a module is imported, is there any way for the module to determine the name of the client code's module? Q2: My understanding is that the code in a module is executed only on the first...
22
by: schneider | last post by:
I need to hook the system mouse down event. I'm trying to replicate how a context menu hides when the mouse clicks outside of the control. Thanks, Schneider
2
by: =?Utf-8?B?Z3JlYXRiYXJyaWVyODY=?= | last post by:
Hi, I know there isn't a specific event property for the download speed, but can anyone tell me how to find it? I'm not sure how to write the code. Thanks, Jason
0
by: Chocolade | last post by:
I have a program that is downloading files every 3 seconds or the user time in seconds he choose. For the example of the problem its every 3 seconds. Now it was working good without problems...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.