【问题标题】:How to create a context that you can access?如何创建可以访问的上下文?
【发布时间】:2009-10-23 13:15:50
【问题描述】:

是否可以创建一个可以从一组窗口访问但不能从不同组访问的上下文?

例如,两个 Web 请求在各自的上下文中运行,而像 ServiceSecurityContext.Current 这样的对象指向不同的实例。

我的应用程序是一个带有PlanWindow 的类似 Word 的应用程序,用于打开计划文档。我希望能够执行PlanContext.Current 之类的操作,这将返回我当前的计划,而不必将文档传递给从该PlanWindow 打开的任何对话框。

我认为这与线程有关,但我不知道从哪里开始。

【问题讨论】:

  • 将 ParentForm 引用传递给 ChildForm 并不是一件不合理的事情。然后,您可以检查所需的 ParentForm 上的任何公共属性。
  • 你想在哪里调用“PlanContext.Current”?是否仅在您的 GUI 对象(用户控件和表单)中?

标签: c# .net architecture


【解决方案1】:

我在我的一个项目中解决了类似的问题。我使用了System.Runtime.Remoting.Messaging.CallContext的GetData和SetData方法。

CallContext 对于每个线程都是唯一的,您可以使用它来存储特定于线程的对象。

在这个特定的场景中,我会像这样创建一个 PlanContext 类:

public class PlanContext
{
    const string _PlanDocumentSessionKey = "Current_PlanDocument";
    public static PlanDocument Current
    {
        get
        {
            return CallContext.GetData(_PlanDocumentSessionKey) as PlanDocument;
        }
        set
        {
            CallContext.SetData(_PlanDocumentSessionKey,value);
        }
    }
}

在您实例化文档的代码中,添加以下内容:

PlanContext.Current = newDocument;

仅供参考,HttpContext.Current 还使用 CallContext 来检索特定线程的上下文。

【讨论】:

  • 在 Winform 上下文中,您有 1 个单一的 GUI 线程。使用 CallContext 将始终返回相同的 PlanDocument ...因此这将等效于 PlanContext 类上名为 Current 的静态属性。还是我错过了什么?
  • 如果他打算使用单线程,静态变量就可以了。但我假设他想为他创建的每个 PlanWindow 使用多个线程(因为他将他的场景与两个 webrequests 进行比较)。就个人而言,我觉得使用多线程是一种更好的方法,因为在一个窗口中打开的模式对话框(比如打印对话框)不会阻止另一个文档的输入。
【解决方案2】:

如何使用以下架构解决此问题:

IPlanContextReceiver
{   
     public object StateByWichPlanContextCanDeciceWhatToReturn get;
}

class SomeWindow : Window, IPlanContextReceiver

在 PlanContext 中,您拥有的 Current 属性

public static PlanContext GetCurrent(IPlanContextReceiver receiver)
{
     lock(contextSync) // be threadsafe if necessary
     {
         if(/*condition that checks receiver.StateByWichPlanContextCanDeciceWhatToReturn*/)
         {
            // context is valid for this receiver
            // return the correct context from an internal store or similar
            return Contexts["TheContextForCoolReceivers"];
         }
         else if(/*condition that checks receiver.StateByWichPlanContextCanDeciceWhatToReturn*/)
         {
            // context is valid for this receiver
            // return the correct context from an internal store or similar
            return Contexts["TheContextForUncoolReceivers"];
         }

         // no existing context is available for this receiver
         return null;
    } 
}

如果您再次将其设为单例,GetCurrent 也可以是实例方法。它也可以是一个索引器,而不是一个方法 - 这是一个品味问题。

另外,StateByWichPlanContextCanDeciceWhatToReturn 实际上是什么取决于您。它可以像需要检查的多个属性一样复杂,也可以像为每个窗口上下文组设置一次的字符串一样简单。

【讨论】:

    【解决方案3】:

    因此,即使活动表单是对话框,这也是另一个有效的命题:

    1) 使用此界面标记所有窗口(子窗口):

    public interface IPlanViewer
    {
        //this means that all you windows will have this property
        PlanDocument Document { get; }
    }
    

    2) 创建“上下文”类

    public class Context
    {
        public static Context Current { get; set; }
        public IPlanDocument Document { get; set; }
        static Context()
        {
            Current = new Context();
        }
    }
    

    3) 在您的 MDI 表单中,使用此方法处理 MdiChildActive 事件

    private void MdiForm_MdiChildActivate(object sender, EventArgs e)
    {
        var currentView = Form.ActiveForm as IPlanViewer;
        if (currentView != null)
            Context.Current.Document = currentView.Document;
    }
    

    4) 像这样使用这个上下文

    private void WorkWithCurrentDoc() 
    {
        var doc = Context.Current.Document;
        doc.Title = "totot"
        // etc ...
    }
    

    就是这样。好点了吗?

    咒语。

    【讨论】:

      【解决方案4】:

      静态字典? PlanContext[sessionID] ?

      【讨论】:

      • 但这会迫使我随身携带 SessionId。如果我要向所有表单和对象传递一个值,那么它也必须是文档本身。
      【解决方案5】:

      我认为这与线程无关。

      如果在Current,你的意思是Selected or Active Plan,那么你可以在@ 987654324上设置一个PlanWindow ob ob @对象,它跟踪所选计划的@对象(即:在选择/激活不同的计划时更新)所有其他窗口/对话框都可以访问属性(例如,使其成为静态)

      【讨论】:

        【解决方案6】:

        我从来没有这样做过,所以我只能为你指出一个似乎正确的方向。

        首先,以下是如何在两个线程上使用 Application.Run 来创建 Windows 窗体应用程序中的多个“主”窗口,每个窗口都在自己的线程上:

        http://msdn.microsoft.com/en-us/library/ms157901.aspx

        其次,这里是每个线程状态的方法:

        How does HttpContext.Current work in a multi-threaded environment?

        http://blogs.msdn.com/jfoscoding/archive/2006/07/18/670497.aspx

        【讨论】:

          【解决方案7】:

          首先,我假设您的应用程序是一个 MDI Windows 窗体,每个子窗口都向用户显示一个文档。

          1) 使用此界面标记所有窗口(子窗口):

          public interface IPlanViewer
          {
              //this means that all you windows will have this property
              PlanDocument Document { get; }
          }
          

          2) 创建“上下文”类

          public class Context
          {
              public static Context Current { get; set; }
              static Context()
              {
                  Current = new Context();
              }
          
              //The key method : I'm returning the document of the currently selected child windows or null if no windows are opened
              public PlanDocument Document
              {
                  get { 
                      var currentView =  Form.ActiveForm as IPlanViewer;
                      if (currentView != null)
                          return currentView.Document;
                      else
                          return null;
                  }
              }
          }
          

          3) 像这样使用这个上下文

          private void WorkWithCurrentDoc() 
          {
              var doc = Context.Current.Document;
              doc.Title = "totot"
              // etc ...
          }
          

          就是这样。我希望这会对你有所帮助。

          咒语。

          【讨论】:

          • 我在使用 Form.ActiveForm 时遇到的问题是,如果我在一个对话框中,这现在是活动表单而不是 IPlanViewer。
          • 您好 Pierre Alain,我了解您的问题,我刚刚提交了另一个可能更好的答案。
          猜你喜欢
          • 2020-12-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多