由于 CultureInfo 没有从一个线程复制到另一个线程,所以我制定了以下方法来为我做这件事。
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);
}
基本上这段代码将文化信息从当前线程复制到要执行的线程action
。我不知道为什么,但它抛出System.ArgumentException
saying Value does not fall within the expected range
。我尝试在主线程上定期运行该操作本身,并且运行得很完美。我的意思是,作为一个动作的方法本身没有问题,我猜上面的代码中的某个地方有问题。
这是异常的堆栈跟踪
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.
还有一件事。这段代码运行得很好,但突然间它开始这样做了。
我得到了它。问题是该操作包含像这样的行 Url = Request.Url,因为我猜在执行代码时 Request 对象不存在或未设置。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)