本文整理汇总了C#中ApartmentState类的典型用法代码示例。如果您正苦于以下问题:C# ApartmentState类的具体用法?C# ApartmentState怎么用?C# ApartmentState使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ApartmentState类属于命名空间,在下文中一共展示了ApartmentState类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DispatcherContext
/// <summary>
/// Initialies a new <see cref="DispatcherContext"/> synchronized with a new <see cref="Thread"/> instance
/// </summary>
/// <param name="threadApartmentState">The <see cref="ApartmentState"/> of the <see cref="Dispatcher"/>'s <see cref="Thread"/></param>
///<param name="owner">The <see cref="Dispatcher"/> that owns the <see cref="DispatcherContext"/></param>
public DispatcherContext(Dispatcher owner, ApartmentState threadApartmentState)
{
Thread dispatcherThread;
this.Owner = owner;
dispatcherThread = new Thread(new ParameterizedThreadStart(this.ExecuteOperations));
dispatcherThread.SetApartmentState(threadApartmentState);
}
示例2: SingleThreadTaskScheduler
public SingleThreadTaskScheduler(string name = null, ThreadPriority priority = ThreadPriority.Normal, ApartmentState apartmentState = ApartmentState.STA)
{
#if DEBUG
_allocStackTrace = new StackTrace();
#endif
_thread = new Thread(
() =>
{
try
{
foreach (var task in _queue.GetConsumingEnumerable())
TryExecuteTask(task);
}
finally
{
_queue.Dispose();
}
});
_thread.IsBackground = true;
_thread.Name = name;
_thread.Priority = priority;
_thread.SetApartmentState(apartmentState);
_thread.Start();
}
示例3: Run
private void Run(ThreadStart userDelegate, ApartmentState apartmentState)
{
lastException = null;
var thread = new Thread(
delegate()
{
try
{
userDelegate.Invoke();
}
catch (Exception e)
{
lastException = e;
}
});
thread.SetApartmentState(apartmentState);
thread.Start();
thread.Join();
if (ExceptionWasThrown())
{
ThrowExceptionPreservingStack(lastException);
}
}
示例4: AssemblyItem
public AssemblyItem( string path, ApartmentState apartment )
{
if ( !System.IO.Path.IsPathRooted( path ) )
throw new ArgumentException( "Assembly path must be absolute", "path" );
this.path = path;
this.apartment = apartment;
}
示例5: StartNewDispatcherThread
private void StartNewDispatcherThread(ApartmentState apartmentState)
{
var reset = new ManualResetEvent(false);
var t = new Thread((ThreadStart)delegate
{
Thread.CurrentThread.Name = string.Format("WorkDispatcherThread");
Dispatcher.Run(reset);
})
{
IsBackground = true
};
t.SetApartmentState(apartmentState);
t.Priority = ThreadPriority.Normal;
/* Starts the thread and creates the object */
t.Start();
/* We wait until our dispatcher is initialized and
* the new Dispatcher is running */
reset.WaitOne();
}
示例6: GetParentThreadInfo
public void GetParentThreadInfo()
{
ParentThread = Thread.CurrentThread;
#if !NETCF
ParentThreadApartment = GetApartmentState(ParentThread);
#endif
}
示例7: WorkerThread
protected WorkerThread(string name, ThreadPriority priority, ApartmentState apartmentState)
{
_thread = new Thread(new ThreadStart(this.InternalRun));
_thread.Name = name;
_thread.Priority = priority;
_thread.SetApartmentState(apartmentState);
}
示例8: ServerSteppablePipelineDriver
internal ServerSteppablePipelineDriver(PowerShell powershell, bool noInput, Guid clientPowerShellId, Guid clientRunspacePoolId, ServerRunspacePoolDriver runspacePoolDriver, ApartmentState apartmentState, HostInfo hostInfo, System.Management.Automation.RemoteStreamOptions streamOptions, bool addToHistory, Runspace rsToUse, ServerSteppablePipelineSubscriber eventSubscriber, PSDataCollection<object> powershellInput)
{
this.localPowerShell = powershell;
this.clientPowerShellId = clientPowerShellId;
this.clientRunspacePoolId = clientRunspacePoolId;
this.remoteStreamOptions = streamOptions;
this.apartmentState = apartmentState;
this.noInput = noInput;
this.addToHistory = addToHistory;
this.eventSubscriber = eventSubscriber;
this.powershellInput = powershellInput;
this.input = new PSDataCollection<object>();
this.inputEnumerator = this.input.GetEnumerator();
this.input.ReleaseOnEnumeration = true;
this.dsHandler = runspacePoolDriver.DataStructureHandler.CreatePowerShellDataStructureHandler(clientPowerShellId, clientRunspacePoolId, this.remoteStreamOptions, null);
this.remoteHost = this.dsHandler.GetHostAssociatedWithPowerShell(hostInfo, runspacePoolDriver.ServerRemoteHost);
this.dsHandler.InputEndReceived += new EventHandler(this.HandleInputEndReceived);
this.dsHandler.InputReceived += new EventHandler<RemoteDataEventArgs<object>>(this.HandleInputReceived);
this.dsHandler.StopPowerShellReceived += new EventHandler(this.HandleStopReceived);
this.dsHandler.HostResponseReceived += new EventHandler<RemoteDataEventArgs<RemoteHostResponse>>(this.HandleHostResponseReceived);
this.dsHandler.OnSessionConnected += new EventHandler(this.HandleSessionConnected);
if (rsToUse == null)
{
throw PSTraceSource.NewInvalidOperationException("RemotingErrorIdStrings", "NestedPipelineMissingRunspace", new object[0]);
}
this.localPowerShell.Runspace = rsToUse;
eventSubscriber.SubscribeEvents(this);
this.stateOfSteppablePipeline = PSInvocationState.NotStarted;
}
示例9: RunInApartment
/// <summary>
/// Runs in the specified apartment
/// </summary>
private void RunInApartment(ThreadStart userDelegate, ApartmentState apartmentState)
{
Exception thrownException = null;
Thread thread = new Thread(
delegate()
{
try
{
userDelegate.Invoke();
}
catch (Exception e)
{
thrownException = e;
}
});
thread.SetApartmentState(apartmentState);
thread.Start();
thread.Join();
if (thrownException != null)
{
ThrowExceptionPreservingStack(thrownException);
}
}
示例10: Run
private void Run(ThreadStart userDelegate, ApartmentState apartmentState)
{
lastException = null;
Thread thread = new Thread(
delegate()
{
#if !DEBUG
try
{
#endif
userDelegate.Invoke();
#if !DEBUG
}
catch (Exception e)
{
lastException = e;
}
#endif
});
thread.SetApartmentState(apartmentState);
thread.Start();
thread.Join();
if (ExceptionWasThrown())
ThrowExceptionPreservingStack(lastException);
}
示例11: Execute
/// <summary>Invokes the specified delegate with the specified parameters in the specified kind of apartment state.</summary>
/// <param name="d">The delegate to be invoked.</param>
/// <param name="parameters">The parameters to pass to the delegate being invoked.</param>
/// <param name="state">The apartment state to run under.</param>
/// <returns>The result of calling the delegate.</returns>
public static object Execute(
Delegate d, object[] parameters, ApartmentState state)
{
if (d == null) throw new ArgumentNullException("d");
if (state != ApartmentState.MTA && state != ApartmentState.STA)
throw new ArgumentOutOfRangeException("state");
if (Thread.CurrentThread.ApartmentState == state)
{
return d.DynamicInvoke(parameters);
}
else
{
ApartmentStateSwitcher switcher = new ApartmentStateSwitcher();
switcher._delegate = d;
switcher._parameters = parameters;
Thread t = new Thread(new ThreadStart(switcher.Run));
t.ApartmentState = state;
t.IsBackground = true;
t.Start();
t.Join();
if (switcher._exc != null) throw switcher._exc;
return switcher._rv;
}
}
示例12: RunInApartmentFromApartment
/// <summary>
/// A common helper method that tests running a test in an apartment from an apartment.
/// </summary>
private bool RunInApartmentFromApartment(ApartmentState fromApartMent, ApartmentState inApartment)
{
bool wasRunInCorrectApartment = false;
Thread runnerThread = new Thread((ThreadStart)delegate
{
if (inApartment == ApartmentState.MTA)
{
ThreadRunner.RunInMTA(delegate
{
wasRunInCorrectApartment = Thread.CurrentThread.GetApartmentState() == inApartment;
});
}
else if (inApartment == ApartmentState.STA)
{
ThreadRunner.RunInSTA(delegate
{
wasRunInCorrectApartment = Thread.CurrentThread.GetApartmentState() == inApartment;
});
}
}
);
runnerThread.SetApartmentState(fromApartMent);
runnerThread.Start();
runnerThread.Join();
return wasRunInCorrectApartment;
}
示例13: NUnitConfiguration
/// <summary>
/// Class constructor initializes fields from config file
/// </summary>
static NUnitConfiguration()
{
try
{
NameValueCollection settings = GetConfigSection("NUnit/TestCaseBuilder");
if (settings != null)
{
string oldStyle = settings["OldStyleTestCases"];
if (oldStyle != null)
allowOldStyleTests = Boolean.Parse(oldStyle);
}
settings = GetConfigSection("NUnit/TestRunner");
if (settings != null)
{
string apartment = settings["ApartmentState"];
if (apartment != null)
apartmentState = (ApartmentState)
System.Enum.Parse(typeof(ApartmentState), apartment, true);
string priority = settings["ThreadPriority"];
if (priority != null)
threadPriority = (ThreadPriority)
System.Enum.Parse(typeof(ThreadPriority), priority, true);
}
}
catch (Exception ex)
{
string msg = string.Format("Invalid configuration setting in {0}",
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
throw new ApplicationException(msg, ex);
}
}
示例14: DedicatedThreadPoolSettings
public DedicatedThreadPoolSettings(int numThreads,
string name = null,
TimeSpan? deadlockTimeout = null,
ApartmentState apartmentState = ApartmentState.Unknown,
Action<Exception> exceptionHandler = null,
int threadMaxStackSize = 0)
: this(numThreads, DefaultThreadType, name, deadlockTimeout, apartmentState, exceptionHandler, threadMaxStackSize)
{ }
示例15: InitializeThreadState
private void InitializeThreadState(object threadParams, ThreadWorkerMethodWithReturn workerMethod, ApartmentState aptState, bool background)
{
this.threadParams = threadParams;
this.threadWorkerMethodWithReturn = workerMethod;
this.thread = new Thread(new ThreadStart(this.ThreadEntryPointMethodWithReturn));
this.thread.SetApartmentState(aptState);
this.backgroundThread = background;
}