.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Imran Ghani
Post New Web Links

Can I use Thread.Sleep() instead of delay acitivity..

Posted By:      Posted Date: April 10, 2011    Points: 0   Category :JavaScript/VBScript
Basically I want to pause the activity for 20mins. I just simply use Thread.Sleep() method.. Should I use delay activity..
- What is the difference between Thread.Sleep() and delay activity..?

View Complete Post

More Related Resource Links

TCPClient and Read not returning complete data unless I use thread.sleep


Hello experts, here is my code that I have a question on:

 Dim receivedbyte(1024) As Byte
        Dim myread As Integer = 0
        Dim myresponsesb As New StringBuilder()
            myread = myns.Read(receivedbyte, 0, receivedbyte.Length)
            myresponsesb.Append(System.Text.Encoding.UTF8.GetString(receivedbyte, 0, myread))

        Loop While (myns.DataAvailable)

In the above, myns is a networkstream that is initialized with a tcpclient that is communicating with a XMPP server. basically, iam getting back xml messages.

The issue with the above is that if I dont use thread.sleep(500), the complete data is not returned. Why is that? I have tried to use a streamreader instead of read and using that method i can get complete data from the stream. What am I doing wrong with the Read method of networkstream?

SECOND: The XML being returned from the server is not well formed - the closing tag for one of the tags is missing and I am unable to use XDocument to parse this XML because of this. What could be the issue here? Is it my code or something on server end?

Also, what technique should i use to make the returned XML well formed so that I can use XDOCUMENT and LINQ to parse this?


long Thread.Sleep causes thread to exit


I have a WPF application where a user enters a time in seconds and clicks a button. Clicking the button causes a thread to start which sets a bit in an external device and after the amount of time entered by the user has elapsed the bit has to be set to 0.

This application controls a stepper motor. So for example if the user enters 5 seconds then a bit is set in the motor which makes it start moving and after 5 sec the bit is reset which makes the motor stop moving. An example of the code I have written is as below

private void btnMoveMotor(object sender, RoutedEventArgs e){
  int time=Convert.ToInt32(txtTime.Text);
  Thread moveMotorThread=new Thread(()=> moveMotor(time));

private void moveMotor(int time){
  //set bit to start motor to move
  //reset bit to stop motor

Sometimes the user can enter a very long time (like 300 seconds) and this causes my thread to exit before the 300 seconds have elapsed. I know th

How to Sleep/Delay within a "for" loop in C# ?


I would like to have a code for an image slide show. Images should be displayed for a give time, e.g. 5 sec. "Thred.Sleep()" seems not working for this purpose. What is the best way ?


WPF Client App Freezing -- UI Thread Sleep, Wait, Join

Okay, I need some help from the community -- I am about to lose my mind.

I have a .net 3.5SP1 WPF client app that allows field tickets to be written out in the field and synced, using sync services, to our production servers.

Here's the situation:

After my background worker threads have finished their work, the tip of the day goes away and the app freezes randomly a few seconds later.  I can only make it happen about 1 in every 20 times, if that. 


When I view in the debugger, it says the UI thread is "sleeping, waiting for join."  Now, background workers should all play cleanly with the UI thread.  So honestly I don't know if another thread is causing it - I actually even read a borderless Window (which would be my tip of the day screen) could cause it.  But I've removed that and tested and can still generate the issue.


I have tried to figure out what's causing this off and on for a month now and have had no luck.  I have removed what I think may be offending code, to only find out I'm still have the problem.  For example, I've removed a sync image, splash screen, code from background worker, etc.


If anyone has any tools to help identify what threads are hanging my U

Flowchart workflow, Delay seems to hold thread


When I create a Flowchart workflow with a delay, and run it via WorkflowApplication.Run(), then try to terminate the main program thread, I would expect the workflow would terminate without a problem because the Delay has relinquished the ThreadPool thread.  But it does not.  If I don't cancel every running workflow, the application will not terminate.

I should say that when my process ends, I just want the workflows to go away.  I don't need to persist them.  When I run the program again, I'll just start over.

To demo this problem, I created a simple app.  I run a flowchart workflow that has a 10-second Delay, then a WriteLine("here") then loops back to the delay.  There's no way out of this loop.

I know I can keep a list of all the WorkflowApplication instances, and loop through them when I stop.  But why do I need to?  What is keeping my process from terminating?  Is there a way to change this? 

I invoke the workflow using this code: 

class Program


 static void Main(string[] args)


  WorkflowApplication app = new WorkflowApplication(

Thread.Sleep() issue


 Hello all -


I'm having an issue with thread.sleep, as I'm purposley trying to cause a 5 minute delay in a service oriented application I'm supporting.  When monitoring for a specific exception, I'm doing a Thread.Sleep(300000) statement.  300,000 milliseconds = 5 minutes, right?  Trouble is, the delays encountered are never 5 minutes, always less, and they vary, sometimes 30 seconds, sometimes 40 seconds, sometimes a full minute.  Anybody have any idea why?

Note:  I have my Thread.Sleep(300000) statement in a catch block where I'm monitoring for IOException, specifically a semaphore timeout, or not enough storage available.  Would this type of exception cause the thread.sleep() to behave sporadically like I described above?  Seems to always be consistantly a five minute delay as intentioned when tested elsewhere in my application.

Any insight would be appreciated!




Why is Thread.Sleep() not interruped when it is in catch block?


Take a look at following simple code..
(This program is not meant to do some useful things. It's just for a demonstration purpose.)

class Program {
    private static void Main(string[] args) {
        // start new thread.
        Thread thread = new Thread(new ThreadStart(ThreadFunc));
        thread.IsBackground = false;

        // interrupt the thread.

    private static void ThreadFunc() {
        try {
            try {
                // assume something goes wrong...
                throw new Exception("TEST");
            catch (Exception) {

Thread Performance: Resource Contention Concurrency Profiling in Visual Studio 2010


Visual Studio 2010 includes new resource contention profiling features that help detect concurrency contention among threads. We walk through a profiling investigation to demonstrate how you can pinpoint and fix resource contention problems in your code.

Maxim Goldin

MSDN Magazine June 2010

Thread Diagnostics: Performance Tuning with The Concurrency Visualizer in Visual Studio 2010


Understanding performance bottlenecks in multithreaded apps used to require significant instrumentation and analysis by expert developers. The Concurrency Visualizer in Visual Studio 2010 significantly reduces the burden of parallel performance analysis.

Hazim Shafi

MSDN Magazine March 2010

CLR Inside Out: Thread Management In The CLR


Getting the performance you want in concurrent applications is not as straightforward as you might think. See how common threading issues can affect your application.

Erika Fuentes and Eric Eilebrecht

MSDN Magazine December 2008

Editor's Note: new Thread(ReadEditorsNote).Start(); yourAttention.WaitOne();


Introducing the issue, Howard Dierking points out that you can't simply parallelize your code blindly if you expect to truly reap the benefits that parallelism promises.

Howard Dierking

MSDN Magazine October 2008

Pooled Threads: Improve Scalability With New Thread Pool APIs


The new threadpool APIs in Windows Vista and in the upcoming Windows Server 2008 provide advances that even native Windows developers can benefit from.

Robert Saccone

MSDN Magazine October 2007

Concurrent Affairs: Build a Richer Thread Synchronization Lock


In my last column, I showed the various thread synchronization mechanisms employed by the Microsoft® . NET Framework (see Concurrent Affairs: Performance-Conscious Thread Synchronization). I then examined the performance characteristics of all these mechanisms and determined that the Interlocked methods performed the best because the calling thread never has to transition to kernel mode.

Jeffrey Richter

MSDN Magazine March 2006

Concurrent Affairs: Performance-Conscious Thread Synchronization


In my career, I have architected and implemented many thread synchronization techniques. This has provided me with a lot of experience that has shaped the way I now think about thread synchronization problems.

Jeffrey Richter

MSDN Magazine October 2005

Basic Instincts: Thread Synchronization


My last three Basic Instincts columns have examined techniques for using asynchronous delegates and creating secondary threads. Those columns demonstrated how to introduce multithreaded behavior into your applications.

Ted Pattison

MSDN Magazine September 2004

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend