gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:54:15 25 4
gpt4 key购买 nike

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

(我在尝试寻找 [是的,我就是那么绝望/无知] 解决方案时添加了 using,即使在 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()

最佳答案

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

For more info read this post .

关于c# - 在 .NET 中使用线程时出现 WebClient 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2800474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com