user496949
user496949

Reputation: 86075

without adding timer or modify the form itself, can we show the form only for specific time span

Without modifying the form itself, can we make the from only show for some specific time, say 5 minutes.

Upvotes: 0

Views: 240

Answers (4)

Hans Passant
Hans Passant

Reputation: 941397

There's no reason for the timer to have to be a member of the form. This will work just fine:

    private void button1_Click(object sender, EventArgs e) {
        var frm = new ThirdPartyForm();
        var tmr = new Timer() { Interval = 5*60*1000, Enabled = true };
        tmr.Tick += delegate { frm.Close(); tmr.Dispose(); };
        frm.Show();
    }

Upvotes: 1

Scott Wisniewski
Scott Wisniewski

Reputation: 25041

You could do:

Thread.Sleep(...);
theForm.Invoke(...);

But at it's core, that's semantically equivalent to using a timer. The only difference is that the timer will pick a thread from the thread pool and in the "sleep" case you'd have to allocate the thread your self. I would advocate that's better to use the thread pool where you can, which means you should just use a timer.

FYI: You can use a timer without using a System.Windows.Forms.Timer control by using the System.Threading.Timer class. That would allow you to do what you want without having to modify the form.

Upvotes: 1

Brad
Brad

Reputation: 163234

So, the form is 3rd party? Meaning, not in your program or what? If you must, you can simply close that form from another form or thread. If from another form, use a timer object there.

Upvotes: 0

Mark Cidade
Mark Cidade

Reputation: 99957

Yes you could (e.g., count to 5 minutes in a separate thread and close the form) but you would probably only be duplicating the functionality of a timer.

Upvotes: 0

Related Questions