public MainForm(bool hidden) : base(formResizable: false) { #if RELEASE CheckForIllegalCrossThreadCalls = false; #endif CultureHelper.GloballySetCultureToGB(); MainFormInstance = this; InitializeComponent(); if (hidden) { ShowInTaskbar = false; CenterForm(); WindowState = FormWindowState.Minimized; Opacity = 0; StartHidden = true; } MusicDataFetcher = new MusicDataFetcher(); SetVersion("v" + Global.ApplicationVersion); lblIssues.GotFocus += LinkLabel_GotFocus; lblDiscoveredFiles.GotFocus += LinkLabel_GotFocus; if (!EdgeDependencyChecker.CheckEdgeCoreFilesArePresentAndCorrect()) { btnConnectToYoutube.Enabled = false; InstallEdge(); } else { ConnectToYTMusicForm = new ConnectToYTMusic(this); } FileScanner = new FileScanner(this); FileUploader = new FileUploader(this); PlaylistProcessor = new PlaylistProcessor(this); IdleProcessor = new IdleProcessor(this); QueueChecker = new QueueChecker(this); InitialiseTimers(); InitialiseTooltips(); InitialiseSystemTrayIconMenuButtons(); ConnectToYouTubeMusic(); StartMainProcess(); }
private void SetMusicDetails(MusicManageTreeNodeModel nodeTag) { if (lblArtistTitle.InvokeRequired || lblAlbumTitle.InvokeRequired || lblSongTitle.InvokeRequired || lblDuration.InvokeRequired || lblDatabaseExistence.InvokeRequired || lblMbId.InvokeRequired || lblUploaded.InvokeRequired || pbCoverArt.InvokeRequired) { var d = new SetMusicDetailsDelegate(SetMusicDetails); Invoke(d, new object[] { nodeTag }); } else { if (nodeTag.NodeType == MusicManageTreeNodeModel.NodeTypeEnum.Playlist) { lblAlbumTitle.Text = nodeTag.PlaylistTitle; lblArtistTitle.Text = "Playlist"; } else { lblArtistTitle.Text = nodeTag.ArtistTitle; lblAlbumTitle.Text = nodeTag.AlbumTitle; } lblSongTitle.Text = nodeTag.SongTitleOrDescription; lblDuration.Text = nodeTag.Duration; lblDatabaseExistence.Text = nodeTag.DatabaseExistence; lblMbId.Text = nodeTag.MbId; if (nodeTag.NodeType == MusicManageTreeNodeModel.NodeTypeEnum.Album) { lblMbId.Tag = "https://musicbrainz.org/release/" + nodeTag.MbId; } else if (nodeTag.NodeType == MusicManageTreeNodeModel.NodeTypeEnum.Song) { lblMbId.Tag = "https://musicbrainz.org/recording/" + nodeTag.MbId; } else { lblMbId.Tag = string.Empty; } lblUploaded.Text = nodeTag.Uploaded; if (nodeTag.NodeType == MusicManageTreeNodeModel.NodeTypeEnum.Artist || nodeTag.NodeType == MusicManageTreeNodeModel.NodeTypeEnum.Root) { pbCoverArt.Image = Properties.Resources.default_artwork_60; } else { if (string.IsNullOrEmpty(nodeTag.CovertArtUrl)) { pbCoverArt.Image = Properties.Resources.default_artwork_60; } else { ThreadPool.QueueUserWorkItem(delegate { byte[] imageBytes = MusicDataFetcher.GetImageBytesFromUrl(nodeTag.CovertArtUrl); if (imageBytes == null) { pbCoverArt.Image = Properties.Resources.default_artwork_60; } else { using (var ms = new MemoryStream(imageBytes)) { var image = Image.FromStream(ms); if (image.Width != 60) { image = ImageHelper.ResizeBitmap((Bitmap)image, 60, 60); } SetCovertArtImage(image); } } }); } } } }
/// <summary> /// Starts a new thread which loops through MusicFiles to check if they've already been uploaded to YouTube Music ahead of /// time and stores them in a cache to speed up the upload checker. /// </summary> /// <param name="musicFilesList">Path to music file to be uploaded</param> /// <param name="cookieValue">Cookie from a previous YouTube Music sign in via this application (stored in the database)</param> /// <param name="musicDataFetcher">You can pass an existing MusicDataFetcher object, or one will be created if left blank</param> public static void StartPrefetchingUploadedFilesCheck( List <MusicFile> musicFilesList, string cookieValue, MusicDataFetcher musicDataFetcher = null) { UploadCheckCache.CleanUp = false; UploadCheckCache.CachedObjects.Clear(); UploadCheckCache.CachedObjectHash.Clear(); try { if (UploadCheckPreloaderThread != null) { UploadCheckPreloaderThread.Abort(); } } catch { } UploadCheckPreloaderThread = new Thread((ThreadStart) delegate { while (!UploadCheckCache.CleanUp) { // The pretecher runs in parallel and doesn't leave any time // for the normal requests... So every second grant a full 100ms UploadCheckCache.Sleep = true; ThreadHelper.SafeSleep(100); UploadCheckCache.Sleep = false; ThreadHelper.SafeSleep(1000); } UploadCheckCache.CachedObjects.Clear(); UploadCheckCache.CachedObjectHash.Clear(); }) { IsBackground = true }; UploadCheckPreloaderThread.Start(); UploadCheckPreloaderThread = new Thread((ThreadStart) delegate { musicFilesList.AsParallel().ForAllInApproximateOrder(cacheObject => { if (!UploadCheckCache.CleanUp) { while (UploadCheckCache.Sleep) { ThreadHelper.SafeSleep(2); } while (UploadCheckCache.Pause) { ThreadHelper.SafeSleep(200); } UploadCheckCache.CachedObjects.Add(new UploadCheckCache.MusicFileCacheObject { MusicFilePath = cacheObject.Path, Result = IsSongUploaded(cacheObject.Path, cookieValue, out string entityId, musicDataFetcher, false) != UploadCheckResult.NotPresent, EntityId = entityId, MbId = !string.IsNullOrEmpty(cacheObject.MbId) ? cacheObject.MbId : musicDataFetcher.GetTrackMbId(cacheObject.Path, false).Result, ReleaseMbId = !string.IsNullOrEmpty(cacheObject.MbId) ? cacheObject.ReleaseMbId : musicDataFetcher.GetReleaseMbId(cacheObject.Path, false).Result }); UploadCheckCache.CachedObjectHash.Add(cacheObject.Path); }
public MainForm(bool hidden) : base(formResizable: false) { #if RELEASE CheckForIllegalCrossThreadCalls = false; #endif CultureHelper.GloballySetCultureToGB(); MainFormInstance = this; InitializeComponent(); btnConnectToYoutube.Enabled = false; if (hidden) { ShowInTaskbar = false; CenterForm(); WindowState = FormWindowState.Minimized; Opacity = 0; StartHidden = true; } MusicDataFetcher = new MusicDataFetcher(); SetVersion("v" + Global.ApplicationVersion); lblIssues.GotFocus += LinkLabel_GotFocus; lblDiscoveredFiles.GotFocus += LinkLabel_GotFocus; ConnectToYTMusicForm = new ConnectToYTMusic(this); FileScanner = new FileScanner(this); FileUploader = new FileUploader(this); PlaylistProcessor = new PlaylistProcessor(this); IdleProcessor = new IdleProcessor(this); QueueChecker = new QueueChecker(this); InitialiseTimers(); InitialiseTooltips(); InitialiseSystemTrayIconMenuButtons(); ConnectToYouTubeMusic(); StartMainProcess(); // Restart everything after 24 hours (is the application is continually run) _restartThread = new Thread((ThreadStart) delegate { while (Settings == null) { ThreadHelper.SafeSleep(500); } while (true) { if (Settings.UploadPlaylists) { if (!Settings.LastPlaylistUpload.HasValue) { Settings.LastPlaylistUpload = DateTime.Now.AddHours(Global.SessionRestartHours * -1).AddHours(-2); } if (!_scanAndUploadThread.IsAlive && DateTime.Now > ((DateTime)Settings.LastPlaylistUpload).AddHours(Global.SessionRestartHours) && !PlaylistProcessor.ProcessingPlaylistsFinished) { StartMainProcess(); } } if (DateTime.Now.AddHours(Global.SessionRestartHours * -1) > SessionStart) { SessionStart = DateTime.Now; PlaylistProcessor.ProcessingPlaylistsFinished = false; StartMainProcess(); } ThreadHelper.SafeSleep(15000); } }) { IsBackground = true }; _restartThread.Start(); }