SplashForm.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.ComponentModel;
  3. using System.Runtime.InteropServices;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. namespace MediaBrowser.ServerApplication.Splash
  8. {
  9. public partial class SplashForm : Form
  10. {
  11. private readonly TaskScheduler _uiThread;
  12. private readonly Progress<double> _progress;
  13. public SplashForm(Version version, Progress<double> progress)
  14. {
  15. InitializeComponent();
  16. lblVersion.Text = string.Format("Version {0}...", version);
  17. _progress = progress;
  18. progress.ProgressChanged += progress_ProgressChanged;
  19. _uiThread = TaskScheduler.FromCurrentSynchronizationContext();
  20. }
  21. async void progress_ProgressChanged(object sender, double e)
  22. {
  23. await Task.Factory.StartNew(() =>
  24. {
  25. var width = e * 6.48;
  26. panelProgress.Width = Convert.ToInt32(width);
  27. }, CancellationToken.None, TaskCreationOptions.None, _uiThread);
  28. }
  29. protected override void OnClosing(CancelEventArgs e)
  30. {
  31. _progress.ProgressChanged += progress_ProgressChanged;
  32. base.OnClosing(e);
  33. }
  34. }
  35. public static class ControlHelper
  36. {
  37. #region Redraw Suspend/Resume
  38. [DllImport("user32.dll", EntryPoint = "SendMessageA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
  39. private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
  40. private const int WM_SETREDRAW = 0xB;
  41. public static void SuspendDrawing(this Control target)
  42. {
  43. SendMessage(target.Handle, WM_SETREDRAW, 0, 0);
  44. }
  45. public static void ResumeDrawing(this Control target) { ResumeDrawing(target, true); }
  46. public static void ResumeDrawing(this Control target, bool redraw)
  47. {
  48. SendMessage(target.Handle, WM_SETREDRAW, 1, 0);
  49. if (redraw)
  50. {
  51. target.Refresh();
  52. }
  53. }
  54. #endregion
  55. }
  56. }