【问题标题】:Custom task runner method throws ArgumentException自定义任务运行器方法抛出 ArgumentException
【发布时间】:2014-05-24 16:14:00
【问题描述】:

由于 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.ArgumentExceptionValue 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.

还有一件事。这段代码运行良好,但突然间它开始这样做了。

【问题讨论】:

  • 我没有发现问题,它对我有用。你传入的动作是什么?
  • 它是 Action () => doThing(new Obj{ //configure properties })) 类型的匿名方法,如果我只运行这个 doThing(new Obj{ //configure properties } )) 它工作正常,但如果我将这个作为一个动作传递给那个方法,它就会开始做这个奇怪的事情
  • 错误发生在哪一行?
  • 在包含此语句的行上 () => doThing(new Obj{ //configure properties }))

标签: c# task-parallel-library argumentexception


【解决方案1】:

我明白了。问题是该操作包含像这样的行 Url = Request.Url,因为我猜在执行代码时 Request 对象不存在或未设置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    • 2015-12-30
    • 1970-01-01
    • 2012-08-10
    相关资源
    最近更新 更多