0

Here is my CS

protected void timer_Tick(object sender, EventArgs e)
    {
        lblNum.Text = string.Format("{0:dd:MM:yyyy hh:mm:ss}", DateTime.Now);

        if (DateTime.Now.Second == 30)
        {
            Thread t = new Thread(DisplayImages);
            t.Start();
            //t.Suspend();
            t.Join();
        }
    }

I want to wait thread t for some fixed interval of time, leaving my main thread untouched. After that interval I would like to do some stuffs in the thread t.

I tried t.Sleep(), but no avail as Sleep() is a static one.

Update:

See my existing question. This question is a part of that. My main thread has a ticker which I dont want to stop. So I cant use Thread.Sleep() as it might freeze the main one.

Community
  • 1
  • 1
  • 1
    Why not start `DisplayImages()` with `Thread.Sleep(30000);` ? – Eugen Rieck May 28 '13 at 12:29
  • Ok I understand what you want. What is preventing you from doing it? – usr May 28 '13 at 12:29
  • Why close ? Any reason? –  May 28 '13 at 12:30
  • As long as there is a Join() in the main-thread code this is pointless. – H H May 28 '13 at 12:31
  • That's not an update but a link. And a question should stand on its own. – H H May 28 '13 at 12:36
  • 3
    Don't mess with threads or timers in ASP.NET. Whatever you want, you're on the wrong track now. – H H May 28 '13 at 12:37
  • @HenkHolterman I know, but my client said I need such one. And the application load is less or equals to 0. Hardly 1-2 user a day. So he doesnot care about the performance of the app. So we are too not caring about the performance of the app. –  May 28 '13 at 12:51
  • 1
    @AmitRanjan: As everyone else is saying, performance isn't the problem. You're simply taking the wrong approach to the problem. What you're trying to do *won't work*. Please see sense and put the delay on the client side. – Jon Skeet May 28 '13 at 13:06

2 Answers2

5

EDIT: Given that this is ASP.NET, it's not at all clear that what you're trying to do makes any sense. It's very possible that you actually want a client-side (Javascript) delay instead. You haven't really told us what effect you're trying to achieve, but I strongly suspect that as soon as you start thinking about what the request/response timing looks like, the idea of waiting 30 seconds before displaying the images really doesn't work at the server side... you'll either delay the whole response, or it will have been sent by the time you try to display the images. Putting the delay on the client side makes much more sense.

In cases where it actually makes sense to create a thread which sleeps to start with...

I want to wait thread t for some fixed interval of time, leaving my main thread untouched. After that interval I would like to do some stuffs in the thread t.

It sounds like you just want to create one ThreadStart from another, which sleeps and then executes the original one:

public static ThreadStart CreateDelayedThreadStart(ThreadStart original,
                                                   TimeSpan delay)
{
    return () =>
    {
        Thread.Sleep(delay);
        original();
    };
}

Then:

Thread t = new Thread(CreateDelayedThreadStart(DisplayImages,
                                               TimeSpan.FromSeconds(30));
t.Start();

You should not use Thread.Join, as that will block the calling thread.

I don't think it's really the best way of scheduling a task to start in 30 seconds, mind you. I would suggest you look into using the TPL...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Finally got the guru's attention. I was waiting. I will follow the way you showed me. :) –  May 28 '13 at 12:39
  • And how useful is this going to be in ASP.NET, server side? You're helping the OP to drill a hole in his foot. – H H May 28 '13 at 12:52
  • @HenkHolterman: hence the last sentence. I can move that up to the top, if you'd prefer that... – Jon Skeet May 28 '13 at 12:55
  • Yes, it should be _before_ all that copy/paste bait. Or _instead of_. – H H May 28 '13 at 12:56
  • @HenkHolterman: See the edit. I think it's potentially still useful to have the rest of the answer for other cases where it makes sense. – Jon Skeet May 28 '13 at 13:01
  • I still think you should keep that for those 'other cases'. See the first response of the OP. He's away now, implementing this disaster. – H H May 28 '13 at 13:04
  • @HenkHolterman : See the first response of the OP. He's away now, implementing this disaster.No I am still here , –  May 28 '13 at 13:11
0

You can use the method Thread.Sleep. This could be called within the context of that thread's execution.

And, having noticed the ASP.NET tag, I'm inclined to agree with Jon (as if I wouldn't be anyway!). In ASP.NET you do not want to be multi-threading like this. Instead consider extracting the logic into an appropriate medium, such as a Windows Service or Task.

You don't want to do 'long running tasks' in the web app itself.

Community
  • 1
  • 1
Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
  • 2
    @AmitRanjan I see, please don't do this. – Grant Thomas May 28 '13 at 12:40
  • I know, but my client said I need such one. And the application load is less or equals to 0. Hardly 1-2 user a day. So he doesnot care about the performance of the app. So we are too not caring about the performance of the app. –  May 28 '13 at 12:54
  • 1
    @AmitRanjan This isn't about performance, which just goes to show your lack of experience with what's at hand, and _that_ is dangerous. – Grant Thomas May 28 '13 at 12:57