J'essaie d'écrire un téléchargeur de fonds d'écran aléatoire pour mon bureau. Le code fonctionne correctement lors du premier téléchargement, mais il se bloque et déclenche une exception lors de la seconde tentative de . J'ai essayé de dipose du client et commencer avec un nouveau client Web. J'ai aussi essayé sans m'en débarrasser. Merci d'avance.WebClient lève une exception lors du deuxième téléchargement atttempt
public class ChangeWallpaper
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
public static void Main()
{
Random fileNumber = new Random();
string pathStart = "http://www.starcraft2.com/images/screenshots/ss";
string pathEnd = "-hires.jpg";
while (true) //forever loop
{
string randomFile = fileNumber.Next(1, 126).ToString();
WebClient Client = new WebClient();
//OK FIRST TIME -> THROWS EXCEPTION ON SECOND ATTEMPT!
Client.DownloadFile(pathStart + randomFile + pathEnd, "pic.jpg");
Client.Dispose(); //tried removing
Bitmap bm = new Bitmap(Image.FromFile("pic.jpg"));
bm.Save("pic.bmp", ImageFormat.Bmp);
bm.Dispose(); //tried removing - no help
SystemParametersInfo(20, 0, "pic.bmp", 0x01 | 0x02);
Thread.Sleep(60000); // Sleep for 1 minute
}
}
}
... et l'exception est? –
Quelle est la nature de l'exception? –
Afficher l'exception. Obtenez-vous également l'exception si vous supprimez l'appel SystemParametersInfo? – nos