2010-05-10 6 views
2

Je vais avoir un très erreur bizarre utilisant la classe WebClient dans .NET 4.erreur WebClient lorsque vous utilisez un fil dans .NET

L'application télécharge quelques fichiers sur l'Internet et fournit une sortie sur une zone de texte (l'interface graphique est WPF). La méthode qui fait cela est la suivante:

void DownloadFiles(object files) { 
    var fileL = (List<string>) files; 
    foreach (string url in fileL) 
    { 
     byte[] data; 
     using (var k = new WebClient()) 
     { 
      data = k.DownloadData(url); 
     } 
     //Bunch of irrelevant code goes here... 
    } 
} 

(j'ai ajouté l'aide de tout en essayant de deviner [oui je suis désespéré/ignorant] une solution, le problème se produit même si l'webclient est déclarée et initialisé en dehors la boucle foreach)

Maintenant, le problème apparaît uniquement lorsque j'exécute cette méthode sur un thread distinct du thread principal de l'interface utilisateur WPF. Si elle est exécutée sur le thread de l'interface utilisateur, cela fonctionne parfaitement. Lorsqu'un nouveau thread est créé avec:

Thread t = new Thread(DownloadFiles); 
t.Start(files); 

La première fois que le code passe dans la boucle, il fonctionnera, mais lorsque son second passage dans la boucle, je serai toujours recevoir un TargetParameterCountException.

Je n'arrive pas à comprendre cette erreur. Toute aide est fortement appréciée.

modifier Voici les détails de l'exception:

Exception.Message = "Parameter count mismatch." 
Exception.InnerException = null 
Exception.Source = " mscorlib" 

Le StackTrace suit:

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.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) 
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) 
at System.Windows.Threading.DispatcherOperation.InvokeImpl()  at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state) 
at System.Threading.ExecutionContext.runTryCode(Object userData) 
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) 
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) 
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
at System.Windows.Threading.DispatcherOperation.Invoke() 
at System.Windows.Threading.Dispatcher.ProcessQueue() 
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) 
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) 
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) 
at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) 
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) 
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) 
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) 
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) 
at System.Windows.Threading.Dispatcher.Run() 
at System.Windows.Application.RunDispatcher(Object ignore) 
at System.Windows.Application.RunInternal(Window window) 
at System.Windows.Application.Run(Window window) 
at System.Windows.Application.Run() 
at FileDownloader.App.Main() in d:\mis documentos\visual studio 2010\Projects\OneMangaDownloader\FileDownloader\obj\x86\Debug\App.g.cs:line 0 
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
at System.Threading.ThreadHelper.ThreadStart() 
+0

S'il vous plaît afficher plus de détails sur l'erreur (message, stacktrace, détails InnerException ...) –

+0

Ajouté l'information que vous avez demandé Thomas, merci pour toute aide –

Répondre

0

Je sais qu'il ya un bug dans WebClient où il met toujours l'appel sur le thread d'interface utilisateur . Je ne sais pas si cela cause les problèmes.

For more info read this post.

+0

Je pense que c'est une restriction Windows Mobile peut-être? Aussi la limite de 6 connexions. Si non ... Wow ... –

Questions connexes