public void Update() { while (_running) { if (!_songLibrary.ScanLibrary()) { Thread.Sleep(500); } try { WMPPlayState playState; lock (lockObject) { playState = player.playState; } if (playState == WMPPlayState.wmppsStopped || playState == WMPPlayState.wmppsUndefined) { Next(null); } } catch { } string status; if (SongPlayerFactory.GetSongPlayer().PlayerStatus.RequestedSong != null) { status = string.Format("Currently playing: {0} sec -> {1}", SongPlayerFactory.GetSongPlayer().PlayerStatus.Position, SongPlayerFactory.GetSongPlayer().PlayerStatus.RequestedSong.Song.GetArtistAndTitle()); } else { status = "No song playing..."; } OnPlayerStatusChanged(status); int minimalsonginqueue; if (!int.TryParse(SongPlayerFactory.GetConfigFile().GetValue("player.minimalsonginqueue"), out minimalsonginqueue)) { minimalsonginqueue = 0; } //Enqueue random song when the queue is empty and the current song is almost finished if (_currentSong != null && _queue.Count < minimalsonginqueue + ((int)(DateTime.Now - _currentSongStart).TotalSeconds + 20 > _currentSong.Song.Duration ? 1 : 0)) { RequestedSong requestedSong = _songLibrary.GetRandomSong(); if (requestedSong != null) { Enqueue(requestedSong.Song, requestedSong.RequesterName); } } } }
private static void Run() { Console.Clear(); DrawArt(); using (HttpListener listener = new HttpListener()) { if (!int.TryParse(SongPlayerFactory.GetConfigFile().GetValue("server.port"), out port)) { port = 8765; } prefix = string.Format("http://*:{0}/", port); DrawProgramStatus(); SongPlayerFactory.GetSongPlayer().LibraryStatusChanged += new StatusChangedEventHandler(Program_LibraryStatusChanged); SongPlayerFactory.GetSongPlayer().PlayerStatusChanged += new StatusChangedEventHandler(Program_PlayerStatusChanged); listener.Prefixes.Add(prefix); listener.Start(); while (_running) { HttpListenerContext context = listener.GetContext(); Stopwatch watch = Stopwatch.StartNew(); Program_LastRequestChanged(string.Format("{0} - {1}\t{2}", DateTime.Now.ToString("HH:mm:ss"), context.Request.UserHostAddress, context.Request.RawUrl)); try { Dispatcher.ProcessRequest(context); watch.Stop(); } catch (Exception ex) { context.Response.StatusCode = 500; using (var writer = new StreamWriter(context.Response.OutputStream)) writer.Write(ex.ToString()); } } } }
private async static Task Run() { // list of tasks List <Task> tasks = new List <Task>(); Console.Clear(); DrawArt(); using (HttpListener listener = new HttpListener()) { host = SongPlayerFactory.GetConfigFile().GetValue("server.host"); if (string.IsNullOrWhiteSpace(host)) { host = "*"; } if (!int.TryParse(SongPlayerFactory.GetConfigFile().GetValue("server.port"), out port)) { port = 8765; } DrawProgramStatus(); string stringVolume = SongPlayerFactory.GetConfigFile().GetValue("player.startupvolume"); int volume; if (!int.TryParse(stringVolume, out volume)) { volume = 50; } SongPlayerFactory.GetSongPlayer().Volume = volume; SongPlayerFactory.GetSongPlayer().LibraryStatusChanged += new StatusChangedEventHandler(Program_LibraryStatusChanged); SongPlayerFactory.GetSongPlayer().PlayerStatusChanged += new StatusChangedEventHandler(Program_PlayerStatusChanged); listener.Prefixes.Add(Prefix); listener.Start(); // maximum number of tasks int maximumNumberOfTasks = 4 * Environment.ProcessorCount; Program_LastRequestChanged(string.Format("Asynchronous handles a maximum of {0} requests.", maximumNumberOfTasks)); using (SemaphoreSlim semaphore = new SemaphoreSlim(maximumNumberOfTasks, maximumNumberOfTasks)) { while (_running) { // wait until a semaphore request is released await semaphore.WaitAsync(); tasks.RemoveAll(x => x.Status == TaskStatus.RanToCompletion); tasks.Add(Task.Run(() => { listener.GetContextAsync().ContinueWith(async(t) => { HttpListenerContext context = await t; try { Program_LastRequestChanged(string.Format("{0} - {1}\t{2}", DateTime.Now.ToString("HH:mm:ss"), context.Request.UserHostAddress, context.Request.RawUrl)); Dispatcher.ProcessRequest(context); return; } catch (Exception ex) { context.Response.StatusCode = 500; using (var writer = new StreamWriter(context.Response.OutputStream)) writer.Write(ex.ToString()); } finally { // release semaphore request semaphore.Release(); } }); })); } } } await Task.WhenAll(tasks); }