//        public void Start() {
//            LogStreamWriter.Write(ExperimentInfo);
//            _StartTime = DateTime.Now;
//            Action wrappedAction = () =>
//            {
//                ExecThread = Thread.CurrentThread;
//                StartWithoutTimeout();
//            };
//            IAsyncResult result = wrappedAction.BeginInvoke(null, null);
//
//            if (result.AsyncWaitHandle.WaitOne(_Timeout)) {
//                wrappedAction.EndInvoke(result);
//            }
//            else
//            {
//                if (!ExecThread.IsAlive) return;
//                ExecThread.Abort();
//                LogStreamWriter.Write(TimeoutKill);
//                _Now = State.Error;
//                return;
//            }
//            LogStreamWriter.Write(ExperimentTimeInfo);
//            SolutionStreamWriter.Write(SolutionInfo);
//            LogStreamWriter.Write(ResultInfo);
//        }

        public async void Start()
        {
            _StartTime = DateTime.Now;
            _Now       = State.Running;
            LogStreamWriter.Write(ExperimentInfo);
            Task _Task;

            _Task = Task.Run(StartWithoutTimeout);
            var timeoutCancellationTokenSource = new CancellationTokenSource();

            if (await Task.WhenAny(_Task, Task.Delay(_Timeout, timeoutCancellationTokenSource.Token)) == _Task)
            {
                // task completed within timeout
                timeoutCancellationTokenSource.Cancel();
                _Now = State.Finished;
                LogStreamWriter.Write(ExperimentTimeInfo);
                SolutionStreamWriter.Write(SolutionInfo);
                LogStreamWriter.Write(ResultInfo);
                await _Task;
            }
            else
            {
                // timeout logic
                LogStreamWriter.Write(TimeoutKill);
                _Now = State.Error;
            }
        }
 public void Abort()
 {
     if (ExecThread != null && ExecThread.IsAlive)
     {
         ExecThread.Abort();
         LogStreamWriter.Write(ManuallyKill);
     }
     _Now = State.Init;
 }
 public bool SetTimeout(int time)
 {
     if (time <= 0 || time > 86400)
     {
         LogStreamWriter.Write(TimeoutLogicalError);
         return(false);
     }
     _Timeout = time * 1000;
     return(true);
 }