Exemplo n.º 1
0
        public MovieData GetMovie(string url)
        {
            lock (lockThis)
            {
                var selection = from m in movies where m.Address == url select m;
                MovieData movie = selection.FirstOrDefault();

                if (movie == null)
                {
                    movie = new MovieData(url);
                    movie.Stopped = true;
                    movies.Add(movie);
                }

                if (movie != null && movie.Stopped && !movie.HasAllData)
                {
                    movie.Stopped = false;
                    RequestThread request = new RequestThread(movie);
                    Thread requestThread = new Thread(request.GetMovie);
                    requestThread.Start();
                }

                var otherMovies = from m in movies where m != movie select m;
                foreach (MovieData stopMovie in otherMovies)
                {
                    stopMovie.Stopped = true;
                }

                if (movie != null)
                {
                    movie.LastUsed = DateTime.Now;
                }

                if (movies.Count > MaxMovies)
                {
                    var ordered = from m in movies where m.Stopped orderby m.LastUsed ascending select m;
                    int delete = movies.Count - MaxMovies;
                    foreach (MovieData deleteMovie in ordered)
                    {
                        deleteMovie.ReleaseResources();
                        movies.Remove(deleteMovie);
                        --delete;
                        if (delete <= 0)
                        {
                            GC.Collect();
                            break;
                        }
                    }
                }

                return movie;
            }
        }
Exemplo n.º 2
0
 public RequestThread(MovieData movieIn)
 {
     movie = movieIn;
 }
Exemplo n.º 3
0
 public TrailerThread(HttpListenerContext contextIn, MovieData movieIn)
 {
     context = contextIn;
     movie = movieIn;
 }