在.NET中使用线程时出现WebClient错误

时间:2010-05-10 05:20:01

标签: c# .net multithreading .net-4.0 webclient

我在.NET 4中使用WebClient类时遇到了非常奇怪的错误。

该应用程序只是从互联网上下载一些文件并在文本框上提供输出(GUI是WPF)。执行此操作的方法如下:

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...
    }
}

(我在尝试神圣的时候添加了使用[是的我是绝望/无知]一个解决方案,即使在foreach循环之外声明和初始化webclient,问题也会发生)

现在只有当我在与WPF UI主线程分开的线程上执行此方法时才会出现问题。如果它在UI线程上执行,那么它可以完美地工作。使用以下命令创建新线程时

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

第一次代码进入循环时它会起作用,但是当它在循环中第二次传递时,我将始终收到一个TargetParameterCountException。

我无法理解这个错误。非常感谢任何帮助。

修改 以下是异常详细信息:

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

StackTrace如下:

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()

1 个答案:

答案 0 :(得分:0)

我知道WebClient中存在一个错误,它始终将调用放在UI线程上。不确定这是否会导致问题。

For more info read this post