Csharp/CSharp Tutorial/Development/Context

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Contextual info

using System;
using System.Runtime.Remoting.Contexts;
using System.Threading;
class MainClass
{
  static void Main(string[] args)
  {
    Context ctx = Thread.CurrentContext;
    Console.WriteLine("Info about context {0}", ctx.ContextID);
    foreach(IContextProperty itfCtxProp in ctx.ContextProperties)
      Console.WriteLine("-> Ctx Prop: {0}", itfCtxProp.Name);
  }
}
Info about context 0
-> Ctx Prop: LeaseLifeTimeServiceProperty

Contextual information: ContextBoundObject

using System;
using System.Runtime.Remoting.Contexts;
using System.Threading;
[Synchronization]
public class SynchContextClass : ContextBoundObject
{
  public SynchContextClass()
  {
    Context ctx = Thread.CurrentContext;
    Console.WriteLine("Info about context {0}", ctx.ContextID);
    foreach(IContextProperty itfCtxProp in ctx.ContextProperties)
      Console.WriteLine("-> Ctx Prop: {0}", itfCtxProp.Name);
  }
}
class MainClass
{
  static void Main(string[] args)
  {
    SynchContextClass synchObj = new SynchContextClass();
  }
}
Info about context 1
-> Ctx Prop: LeaseLifeTimeServiceProperty
-> Ctx Prop: Synchronization

Get name of current AppDomain and context ID

using System;
using System.Threading;
class MainClass
{
  // [MTAThread]
  [STAThread]
  static void Main(string[] args)
  {
    Thread primaryThread = Thread.CurrentThread;
    primaryThread.Name = "ThePrimaryThread";
    Console.WriteLine("Name of current AppDomain: {0}", Thread.GetDomain().FriendlyName);
    Console.WriteLine("ID of current Context: {0}", Thread.CurrentContext.ContextID);
  }
}
Name of current AppDomain: main.exe
ID of current Context: 0