How to pass HttpContext.Current to methods called using Parallel.Invoke() in .net

只愿长相守 提交于 2019-12-05 08:11:48

Store a reference to the context, and pass it to the methods as an argument...

Like this:

    protected void Page_Load(object sender, EventArgs e)
    {
        var ctx = HttpContext.Current;
        System.Threading.Tasks.Parallel.Invoke(() => Display(ctx), () => Display2(ctx));
    }

    public void Display(HttpContext context)
    {
        if (context != null)
        {
            Response.Write("Method 1" + context.User.Identity.Name);
        }
        else
        {
            Response.Write("Method 1 Unknown");
        }
    }

    public void Display2(HttpContext context)
    {

        if (context != null)
        {
            Response.Write("Method 2" + context.User.Identity.Name);
        }
        else
        {
            Response.Write("Method 2 Unknown");
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!