本文整理汇总了C#中Thread.SetApartmentState方法的典型用法代码示例。如果您正苦于以下问题:C# Thread.SetApartmentState方法的具体用法?C# Thread.SetApartmentState怎么用?C# Thread.SetApartmentState使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Thread
的用法示例。
在下文中一共展示了Thread.SetApartmentState方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MTATaskScheduler
/// <summary>Initializes a new instance of the MTATaskScheduler class with the specified concurrency level.</summary>
/// <param name="numberOfThreads">The number of threads that should be created and used by this scheduler.</param>
/// <param name="nameFormat">The template name form to use to name threads.</param>
public MTATaskScheduler(int numberOfThreads, string nameFormat)
{
// Validate arguments
if (numberOfThreads < 1) throw new ArgumentOutOfRangeException("numberOfThreads");
// Initialize the tasks collection
tasks = new BlockingCollection<Task>();
// Create the threads to be used by this scheduler
_threads = Enumerable.Range(0, numberOfThreads).Select(i =>
{
var thread = new Thread(() =>
{
// Continually get the next task and try to execute it.
// This will continue until the scheduler is disposed and no more tasks remain.
foreach (var t in tasks.GetConsumingEnumerable())
{
TryExecuteTask(t);
}
});
thread.IsBackground = true;
thread.SetApartmentState(ApartmentState.MTA);
thread.Name = String.Format("{0} - {1}", nameFormat, thread.ManagedThreadId);
return thread;
}).ToList();
// Start all of the threads
_threads.ForEach(t => t.Start());
}
示例2: SetClipboard
public static void SetClipboard(string result)
{
var thread = new Thread(() => Clipboard.SetText(result));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
示例3: Capture
protected void Capture(object sender, EventArgs e)
{
string url = txtUrl.Text.Trim();
Thread thread = new Thread(delegate()
{
using (WebBrowser browser = new WebBrowser())
{
browser.ScrollBarsEnabled = false;
browser.AllowNavigation = false;
browser.AllowWebBrowserDrop = false;
browser.ScriptErrorsSuppressed = true;
browser.Navigate("www.cnn.com");
//browser.Navigate((url, null, <data>, "Content-Type: application/x-www-form-urlencoded");)
browser.Width = 1024;
browser.Height = 768;
browser.ClientSize = new Size(1024,768);
browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DownloadCompleted);
while ((browser.IsBusy) | (browser.ReadyState != WebBrowserReadyState.Complete))
{
System.Windows.Forms.Application.DoEvents();
}
//Thread.Sleep(5000);
//browser.Dispose();
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
示例4: Connection
public void Connection()
{
BTN_Connect.Visible=false;
T = new Thread(Connect);
T.IsBackground=true;
T.SetApartmentState(ApartmentState.STA);
T.Start();
}
示例5: AddWorker
public static void AddWorker(ThreadManager threadManager)
{
Interlocked.Increment(ref threadManager.activeThreads);
var worker = new WorkThread(threadManager);
var thread = new Thread(worker.Loop);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
示例6: Generate
public Bitmap Generate()
{
// Thread
var m_thread = new Thread(_Generate);
m_thread.SetApartmentState(ApartmentState.STA);
m_thread.Start();
m_thread.Join();
return m_Bitmap;
}
示例7: StartImport
/// <summary>
///
/// </summary>
/// <param name="sharepointConnectionId"></param>
/// <param name="adaptername"></param>
/// <param name="objectdefinitionId"></param>
public void StartImport(string sharepointConnectionId, string adaptername, string workflowName, ObjectDefinition objectdefinition)
{
ConnectionId = sharepointConnectionId;
AdapterName = adaptername;
SharePointObjectDefinition = objectdefinition;
WorkflowName = workflowName;
MainThread = new Thread(SharePointClientImportHelper.Start);
MainThread.Priority = ThreadPriority.Lowest;
MainThread.SetApartmentState(ApartmentState.STA);//Creating a single threaded apartment.
MainThread.Start();
Thread.Sleep(100);
}
示例8: Initialize
private static void Initialize()
{
// Run the message loop on another thread so we're compatible with a console mode app
var t = new Thread(() => {
frm = new Form();
var dummy = frm.Handle;
frm.BeginInvoke(new MethodInvoker(() => mre.Set()));
Application.Run();
});
t.SetApartmentState(ApartmentState.STA);
t.IsBackground = true;
t.Start();
mre.WaitOne();
}
示例9: SetClipboard
static void SetClipboard()
{
var templateClass = @"
public static class ModuleInitializer
{
public static void Initialize()
{
//Your code goes here
}
}";
var thread = new Thread(() => Clipboard.SetText(templateClass));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
示例10: OpenURLWithData
public void OpenURLWithData(string wszURL, IntPtr pbPostData, int cbData, IntPtr hwnd)
{
Thread InvokeThread;
// Create POST data and convert it to a byte array.
m_byteArray = new byte[cbData];
Marshal.Copy(pbPostData, m_byteArray, 0, cbData);
m_szUrl = wszURL;
m_hwnd = hwnd;
InvokeThread = new Thread(new ThreadStart(InvokeMethod));
InvokeThread.SetApartmentState(ApartmentState.STA);
InvokeThread.Start();
}
示例11: Run
void Run(ThreadStart userDelegate, ApartmentState apartmentState)
{
lastException = null;
var thread = new Thread(() => MultiThreadedWorker(userDelegate));
thread.SetApartmentState(apartmentState);
thread.Start();
thread.Join();
if (ExceptionWasThrown())
{
ThrowExceptionPreservingStack(lastException);
}
}
示例12: RunInSta
public void RunInSta(Action userDelegate)
{
lastException = null;
var thread = new Thread(() => MultiThreadedWorker(() => userDelegate()));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
if (lastException != null)
{
ThrowExceptionPreservingStack(lastException);
}
}
示例13: IEBrowser
/// <summary>
/// class constructor
/// </summary>
/// <param name="visible">whether or not the form and the WebBrowser control are visiable</param>
/// <param name="userName">client user name</param>
/// <param name="password">client password</param>
/// <param name="resultEvent">functionality to keep the main thread waiting</param>
public IEBrowser(bool visible, string URL, AutoResetEvent resultEvent)
{
//this.userName = userName;
//this.password = password;
this.resultEvent = resultEvent;
htmlResult = null;
thrd = new Thread(new ThreadStart(
delegate {
Init(visible,URL);
System.Windows.Forms.Application.Run(this);
}));
// set thread to STA state before starting
thrd.SetApartmentState(ApartmentState.STA);
thrd.Start();
}
示例14: setNorth_Click
protected void setNorth_Click(object sender, EventArgs e)
{
northNetwork = (sender as Button).Text.ToUpper();
// Do work!
Thread threadNorth = new Thread( () =>
{
using ( System.Windows.Forms.WebBrowser webBrowser1 = new System.Windows.Forms.WebBrowser() )
{
webBrowser1.DocumentCompleted += loadPresetsPage;
webBrowser1.Navigate( NORTH_URL_SET_PRESET );
System.Windows.Forms.Application.Run();
}
} );
threadNorth.Name = "North";
threadNorth.SetApartmentState( ApartmentState.STA );
threadNorth.Start();
threadNorth.Join();
}
示例15: RunAsync
public Task<RunSummary> RunAsync(IMessageSink diagnosticMessageSink, IMessageBus messageBus, object[] constructorArguments,
ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
{
var tcs = new TaskCompletionSource<RunSummary>();
var thread = new Thread(() =>
{
try
{
var testCaseTask = testCase.RunAsync(diagnosticMessageSink, messageBus, constructorArguments, aggregator,
cancellationTokenSource);
tcs.SetResult(testCaseTask.Result);
}
catch (Exception e)
{
tcs.SetException(e);
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
return tcs.Task;
}