c# - How can I terminate a function after X seconds? -
i got this:
void func() { int = int.parse(system.readline.console()); } but want function finish after 5 seconds example, if user didn't insert anything. how can that?
i thought that:
void func() { system.timers.timer timer = new timer(); timer.interval = 5000 timer.elapsed += somefunc; timer.start() int = int.parse(system.console.readline()); } void somefunc(object sender, eventargs args) { thread.currentthread.abort(); } but looks ugly. in addition, don't want abort threads, break function. don't want create thread function.
this appears work me
var dt = datetime.now; var s = string.empty; while ((datetime.now - dt).totalseconds < 5) { if (console.keyavailable) s += console.readkey().keychar; }
Comments
Post a Comment