本文整理匯總了C#中System.Threading.Thread類的典型用法代碼示例。如果您正苦於以下問題:C# Thread類的具體用法?C# Thread怎麽用?C# Thread使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Thread類屬於System.Threading命名空間,在下文中一共展示了Thread類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Initialize
// Initialize Parallel class's instance creating required number of threads
// and synchronization objects
private void Initialize( )
{
threadsCount = System.Environment.ProcessorCount;
//No point starting new threads for a single core computer
if (threadsCount <= 1) {
return;
}
// array of events, which signal about available job
jobAvailable = new AutoResetEvent[threadsCount];
// array of events, which signal about available thread
threadIdle = new ManualResetEvent[threadsCount];
// array of threads
threads = new Thread[threadsCount];
for ( int i = 0; i < threadsCount; i++ )
{
jobAvailable[i] = new AutoResetEvent( false );
threadIdle[i] = new ManualResetEvent( true );
threads[i] = new Thread( new ParameterizedThreadStart( WorkerThread ) );
threads[i].IsBackground = false;
threads[i].Start( i );
}
}
示例2: Main
static void Main(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine("Usage:");
Console.WriteLine("'extractor.exe output_folder input.xef' to extract a .xef file.");
return;
}
string myPhotos = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
string subFolderPath = System.IO.Path.Combine(myPhotos, args[0]);
Directory.CreateDirectory(subFolderPath);
using (Extractor extractor = new Extractor(subFolderPath))
{
extractor.ListenForFrames();
string xefFileName = args[1];
Console.WriteLine("Extracting frames from .xef file: {0}", xefFileName);
Player player = new Player(xefFileName);
Thread playerThread;
playerThread = new System.Threading.Thread(new ThreadStart(player.PlayXef));
playerThread.Start();
playerThread.Join();
extractor.Stop();
}
}
示例3: MultiThread
public void MultiThread()
{
int threadCount = 5;
_threadedMessageCount = 100;
int totalMessageCount = threadCount * _threadedMessageCount;
List<Thread> threads = new List<Thread>();
for (int thread = 0; thread < threadCount; thread++)
{
Thread t = new Thread(new ThreadStart(SendMessageThread));
threads.Add(t);
t.Start();
}
foreach (Thread t in threads)
{
t.Join();
}
POP3ClientSimulator.AssertMessageCount(_account.Address, "test", totalMessageCount);
for (int i = 0; i < totalMessageCount; i++)
{
string content = POP3ClientSimulator.AssertGetFirstMessageText(_account.Address, "test");
Assert.IsTrue(content.Contains("X-Spam-Status"), content);
}
}
示例4: Main
private static void Main(string[] args)
{
var n = 10;
var chickenFarm = new ChickenFarm();
var token = chickenFarm.GetToken();
var chickenFarmer = new Thread(chickenFarm.FarmSomeChickens) {Name = "TheChickenFarmer"};
var chickenStore = new Retailer(token);
chickenFarm.PriceCut += chickenStore.OnPriceCut;
var retailerThreads = new Thread[n];
for (var index = 0; index < retailerThreads.Length; index++)
{
retailerThreads[index] = new Thread(chickenStore.RunStore) {Name = "Retailer" + (index + 1)};
retailerThreads[index].Start();
while (!retailerThreads[index].IsAlive)
{
;
}
}
chickenFarmer.Start();
chickenFarmer.Join();
foreach (var retailerThread in retailerThreads)
{
retailerThread.Join();
}
}
示例5: SetClipboard
public static void SetClipboard(string result)
{
var thread = new Thread(() => Clipboard.SetText(result));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
示例6: SetUp
public void SetUp()
{
runner = new TestRunner(x => x.AddFixture<SlowFixture>());
test = new Test("slow test").With(Section.For<SlowFixture>().WithStep("GoSlow"));
var reset = new ManualResetEvent(false);
var running = new ManualResetEvent(false);
var thread = new Thread(() =>
{
running.Set();
Debug.WriteLine("Starting to run");
test.LastResult = runner.RunTest(new TestExecutionRequest()
{
Test = test,
TimeoutInSeconds = 60
});
test.LastResult.ShouldNotBeNull();
Debug.WriteLine("finished running");
reset.Set();
});
thread.Start();
running.WaitOne();
Thread.Sleep(1000);
Debug.WriteLine("Aborting now!");
runner.Abort();
Debug.WriteLine("Done aborting");
reset.WaitOne(5000);
test.LastResult.ShouldNotBeNull();
Debug.WriteLine("completely done");
}
示例7: StartService
public static void StartService()
{
Thread thread = new Thread(OrderProcessingService.Run);
thread.Name = "Order Processing Thread";
thread.IsBackground = true;
thread.Start();
}
示例8: RelayPort
public RelayPort(Cpu.Pin pin, bool initialState, bool glitchFilter, Port.ResistorMode resistor, int timeout)
: base(pin, initialState, glitchFilter, resistor)
{
currentstate = initialState;
relayThread = new Thread(new ThreadStart(RelayLoop));
relayThread.Start();
}
示例9: Service
public Service()
{
InitializeComponent();
_server = new ConnectsterServer();
var job = new ThreadStart(_server.Start);
_thread = new Thread(job);
}
示例10: mainForm
//UI Init and Main Thread Creation
public mainForm()
{
InitializeComponent();
AcceptButton = buttonSend;
tmain = new System.Threading.Thread(main);
tmain.Start();
}
示例11: SetupPrimaryDisplayForm
/// <summary>
/// Setups the primary display form.
/// </summary>
public static void SetupPrimaryDisplayForm()
{
PrimaryDisplayForm = new DisplayForm(800, 600);
Thread thread = new Thread(new ThreadStart(CreatePrimaryDisplayForm));
thread.Start();
}
示例12: Start
public void Start()
{
Stop();
m_bThreadStop = false;
m_thread = new System.Threading.Thread(Main);
m_thread.Start();
}
示例13: Start_WhenExecutedInMultipleThreads_ShouldBeThreadSafeAndNotExecuteSameTaskTwice
public void Start_WhenExecutedInMultipleThreads_ShouldBeThreadSafeAndNotExecuteSameTaskTwice()
{
const string resultId = "result/1";
Enumerable.Range(1, 100)
.ToList()
.ForEach(i =>
{
CreateRaceConditionTask(resultId);
var thread1 = new Thread(() =>
{
var taskExecutor =
MasterResolve<ITaskExecutor>();
taskExecutor.Start();
});
var thread2 = new Thread(() =>
{
var taskExecutor =
MasterResolve<ITaskExecutor>();
taskExecutor.Start();
});
thread1.Start();
thread2.Start();
thread1.Join();
thread2.Join();
});
var result = Store.Load<ComputationResult<int>>(resultId);
result.Result.Should().Be(100);
}
示例14: setupRequestResponseBackgroundThead
private void setupRequestResponseBackgroundThead()
{
requestResponseThread = new Thread(new ThreadStart(RequestResponseBackground_Thread));
requestResponseThread.IsBackground = true;
multicastRequestThread = new Thread(new ThreadStart(MulticastRequestBackgroud_Thread));
multicastRequestThread.IsBackground = true;
}
示例15: RunInNewThread
[System.Security.SecurityCritical] // auto-generated
private int RunInNewThread () {
Thread th = new Thread(new ThreadStart(NewThreadRunner));
th.SetApartmentState(m_apt);
th.Start();
th.Join();
return m_runResult;
}