开发者

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

开发者 https://www.devze.com 2023-02-13 07:58 出处:网络
Without modifying the form itself, can we make the from only show for some specific time, say 5 minute开发者_如何学运维s.You could do:

Without modifying the form itself, can we make the from only show for some specific time, say 5 minute开发者_如何学运维s.


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.


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();
    }


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消