.NET Framework - pause in microsecund
Asked By alfa
26-Jan-10 04:56 PM
I have set pause to microsecund. I have Microsoft Development Environment
2002 (ver.7.0.9466) and .NET Framework 1.0 (ver 1.0.3705) and
Stopwatch stopWatch = new Stopwatch(); do not work
How can I do it.
Environment
(1)
Console
(1)
Thread
(1)
Main
(1)
Stopwatch
(1)
Microsecund
(1)
Alexey Smirnov replied to alfa
Use Threading.Thread.Sleep
Imports System.Threading
Class Example
Shared Sub Main()
For i As Integer =3D 0 To 4
Console.WriteLine("Sleep for 2 seconds.")
Thread.Sleep(2000)
Next
Console.WriteLine("Main thread exits.")
End Sub
End Class
http://msdn.microsoft.com/en-us/library/d00bd51t.aspx
Hope this helps
GUI Thread - Is cross-thread operation really bad? .NET Framework Hi all, When I first learn threading, I had always been advised that doing a cross-thread operation on a GUI thread is not safe and should be avoided. But it is not until recently that it Add(label1); Controls.Add(button1); } void button1_Click(object sender, EventArgs e) { button1.Enabled = false; new Thread(SimulateLongRunningWork).Start(); } void SimulateLongRunningWork() { for (int i = 1; i < = 100; i++) { Thread.Sleep(100); / / simulate long running task label1.Text = "Progress: " + i + "%"; / / <- cross-thread operation } label1.Text + = " Completed!"; / / <- cross-thread operation button1.Text = "Completed"; / / <- cross-thread operation } [STAThread] public static void Main(string[] args) { Application
output in SimpleWork is like this. So why will the ManagedThreadId start counting from 3 ? Thread: 3 Thread: 4 Thread: 5 Thread: 3 Thread: 4 Thread: 5 Thread: 3 Thread: 4 Thread: 5 Thread: 3 Thread: 4 Thread: 5 Thread: 3 Thread: 4 Thread: 5
WaitForMultiObjects equivalent in C# .NET Framework For the longest, for my non-I / O based thread work, I used WaitForMultiObjects (WFMO) as a way to wait for a set of worker threads to complete, simpling by putting the thread handles in an array and passing it to WFMO, for example: / / Wait (infinitely) for 10 Workers[i].IsBusy) done++; } Application.DoEvents(); } / / THREADS WE DONE - do whatever I only used a Thread Pool. But I do not see a function there to determine when all the queued doing is writing a class that spawns the dynamically allocated workers with an additional optional thread to monitor the completion and fire an OnMultiThreadComplete event. The default is to just wait in the calling thread with a function that does the loop I showed in my last post. That works applicable. In this particular example, this means a few things: ??? Don't block the GUI thread. In other words, the very design idea of waiting on all the worker threads is Use the features built into BackgroundWorker to track and respond to changes in the active thread count. ??? In other cases, use the Monitor class for cross-thread signaling. The first two points go together, in that using the BackgroundWorker correctly means that
divide by zero on purpose so I expect that this domain should stop and the main thread should continue with the for loop. What happens now is that I get an exception is correct about division by with zero and then the whole application stop. So the main thread does not continue with the for loop. My purpose of this test was to check that the main thread in the primary application domain could continue executing when the new application domain with it is own additional thread run into problem such as divide by zero. using System; using System.Text; using System.Threading; public class Program { static void Main(string[] args) { AppDomain domain = AppDomain.CreateDomain("SecondDomain"); Thread.CurrentThread.Name = "Thread 2"; / / / / Thread.Start in
1) Disk (1) Bit (1) Dec (1) Jeff Gaines Wiltshire (1) Compiler (1) ReadKey (1) Console (1) On 29 / 01 / 2011 in message Why are you surprised? NET is a wrapper true); at the bottom of both programs. WTF? Of all possible ways of preventing the console from closing when running from GUI that must be the clumsiest. I tried copying the CPU can create different results. Arne . . . . Very interesting analysis. Does it change anything using a stopwatch (elapsed property), instead of the environment variable ? I think Peter was doubting that part. LOL! that is funny. Frankly, I never used the console and i was just wondering what was that for !! You made me curious though. What is a long time), then exactly how it is measured should not matter much, C#: Console.ReadKey(); C++: getch(); is a couple of possibilities. Arne Thanks ! good to know :-) I am trying with the stopwatch (stopWatch.Elapsed.Milliseconds) on a winform and i am getting smaller elapsed values. Not much smaller