自定义任务亚军方法引发的ArgumentException [英] Custom task runner method throws ArgumentException
问题描述
由于这一事实的CultureInfo不是从线程复制到线程我做了下面的方法做的事情对我来说。
Because of the fact that CultureInfo is not being copied from thread to thread I have made following method to do that thing for me.
public static StartCustomTask(Action action, TaskCreationOptions tco = TaskCreationOptions.None)
{
var currentCult = Thread.CurrentThread.CurrentCuture;
var currentUiCult = Thread.CurrentThread.CurrentUICulture;
return Task.Factory.StartNew(() =>
{
Thread.CurrentThread.CurrentCuture = currentCult;
Thread.CurrentThread.CurrentUICulture = currentUiCult;
action();
}, tco);
}
基本上是从当前线程的代码拷贝文化信息的线程是要去执行在动作
。我不知道为什么,但它抛出 System.ArgumentException
说值不在预期范围
之内。我试图在主线程定期运行动作本身和它顺利完美。我的意思是,该方法正被一个动作没有问题本身是有问题的地方在上面的代码我猜。
basically this code copies culture info from current thread to the thread that is gonna execute the action
. I don't know why but it throws System.ArgumentException
saying Value does not fall within the expected range
. I've tried to run the action itself regularly on the main thread and it goes perfectly. By that I mean, that method that is being an action does not have a problem itself there is a problem somewhere in the code above I guess.
下面是一个例外。
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at System.Web.Hosting.IIS7WorkerRequest.GetServerVariableInternal(String name)
at System.Web.HttpRequest.BuildUrl(Func`1 pathAccessor)
at System.Web.HttpRequest.get_Url()
at SL.CoreLogic.Web.FrontEnd.Controllers.AccountController.<>c__DisplayClass37.<ResetPassword>b__31() in d:\CoreProjects\CoreLogic\CoreLogic-RusSlot\SL.CoreLogic\SL.CoreLogic.Web.FrontEnd\Controllers\AccountController.cs:line 447
at SL.CoreLogic.Common.CustomTask.<>c__DisplayClass1.<StartWithCurrentCulture>b__0() in d:\CoreProjects\CoreLogic\CoreLogic-RusSlot\SL.CoreLogic\SL.CoreLogic.Common\CustomTask.cs:line 22
at System.Threading.Tasks.Task.
还有一件事。此代码工作完美,但突然它开始这样做。
and one more thing. this code was working perfectly but all of a sudden it started doing this.
推荐答案
我知道了。问题是,该诉讼包含这样的URL = Request.Url一条线,因为我想在正在执行的请求对象不存在时间码或没有设置。
I got it. the problem was that the action contained a line like this Url = Request.Url, as I guess at the time code was being executed the Request object did not exist or was not set.
这篇关于自定义任务亚军方法引发的ArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!