2013-05-08 6 views
6

में उपयोग करने वाले विधियों के लिए कैसे पास करें, मेरे पास दो विधियां हैं जो उपयोगकर्ता आईडी प्राप्त करने के लिए HttpContext.Current का उपयोग करती हैं। जब मैं इन पद्धतियों को अलग-अलग कॉल करता हूं, तो मुझे उपयोगकर्ता आईडी मिलती है लेकिन जब उसी विधि को समानांतर का उपयोग कहा जाता है। इन्वोक() HttpContext.Current शून्य है।HttpContext.Current को parallel.Invoke() में .net

मैं कारण पता है, मैं सिर्फ मैं HttpContext.Current लिए पहुंच सकते हैं का उपयोग कर के आसपास काम करने के लिए देख रहा हूँ। मैं इस सुरक्षित थ्रेड नहीं है पता है, लेकिन मैं केवल पढ़ने कार्रवाई करने के लिए चाहते हैं

public partial class _Default : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      Display(); 
      Display2(); 
      Parallel.Invoke(Display, Display2); 
     } 

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

     public void Display2() 
     { 

      if (HttpContext.Current != null) 
      { 
       Response.Write("Method 2" + HttpContext.Current.User.Identity.Name); 
      } 
      else 
      { 
       Response.Write("Method 2 Unknown"); 
      } 
     } 
    } 

धन्यवाद

उत्तर

5

स्टोर संदर्भ के लिए एक संदर्भ है, और यह एक तर्क के रूप तरीकों को पारित ...

इस तरह:

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"); 
     } 
    } 
संबंधित मुद्दे