The task is this: a stream is started in the console. It is waiting for user input. As the user enters the data stream closes. At the same time, the second stream works in the background and if the user does not enter any data, the first stream closes within a minute. You can submit a solution to the program.
Closed due to the fact that the essence of the question is not clear to the participants free_ze , Igor , Kromster , AK ♦ , Denis Bubnov Jan 24 '17 at 6:43 .
Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
- 6of course not. - tCode
- oneAnd you yourself tried to do something in this direction? It can help, but you are unlikely to give you a ready-made solution. - BlackWitcher
- 3What is the question? - Grundy
|
2 answers
For the sake of interest, wrote so
using System; using System.Threading; using System.Timers; using Timer = System.Timers.Timer; namespace ConsoleApplication1 { class Program { private static string UserText; private static Thread FirstThread; private static Thread SecondThread; private static Timer Timer; static void Main() { FirstThread = new Thread(GetText); SecondThread = new Thread(SessionTimer); FirstThread.Start(); SecondThread.Start(); } private static void GetText() { UserText = Console.ReadLine(); Console.WriteLine($"Hello my dear, {UserText}"); Console.Read(); } private static void SessionTimer() { Timer = new Timer(); Timer.Elapsed += OnTimedEvent; Timer.Interval = 60000; Timer.Enabled = true; } private static void OnTimedEvent(object source, ElapsedEventArgs e) { if (!FirstThread.IsAlive && !SecondThread.IsAlive) { Console.WriteLine("Все потоки закрыты!"); } if (!string.IsNullOrEmpty(UserText)) { return; } if (FirstThread.ThreadState == ThreadState.AbortRequested) { return; } FirstThread.Abort(); Timer.Stop(); Console.WriteLine("Поток 2 закрыл поток 1"); } } } - oneCode from 2006. Are you with us on a time machine ...? Togo? - Bulson
|
There is a great Task.WaitAny method that can be timeout set:
class Program { static void Main(string[] args) { var readingTask = new Task(() => { var data = Console.ReadLine(); }); Task.WaitAny( new[] { readingTask }, TimeSpan.FromSeconds(60)); } } |