Guest User

Untitled

a guest
Sep 25th, 2015
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.12 KB | None | 0 0
  1. namespace ConsoleApplication4
  2. {
  3.     using System.Windows.Forms;
  4.  
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Application.Run(new DemoView());
  10.         }
  11.     }
  12. }
  13.  
  14. namespace ConsoleApplication4
  15. {
  16.     public interface IDemoView
  17.     {
  18.         void SetProgress(int val);
  19.     }
  20. }
  21.  
  22. namespace ConsoleApplication4
  23. {
  24.     public interface IDemoPresenter
  25.     {
  26.         void OnButtonClick();
  27.     }
  28. }
  29.  
  30. namespace ConsoleApplication4
  31. {
  32.     using System;
  33.  
  34.     public interface IDemoModel
  35.     {
  36.         void DoWork(Action<int> progressCallback);
  37.     }
  38. }
  39.  
  40. namespace ConsoleApplication4
  41. {
  42.     using System;
  43.     using System.Windows.Forms;
  44.  
  45.     public partial class DemoView : Form, IDemoView
  46.     {
  47.         private IDemoPresenter _presenter;
  48.  
  49.         public DemoView()
  50.         {
  51.             _presenter = new DemoPresenter(this, new DemoModel());
  52.  
  53.             InitializeComponent();
  54.         }
  55.  
  56.         public void SetProgress(int val)
  57.         {
  58.             Invoke(new EventHandler((s, e) => { progressBar1.Value = val; }));
  59.         }
  60.  
  61.         private void button1_Click(object sender, EventArgs e)
  62.         {
  63.             _presenter.OnButtonClick();
  64.         }
  65.     }
  66. }
  67.  
  68. namespace ConsoleApplication4
  69. {
  70.     public class DemoPresenter : IDemoPresenter
  71.     {
  72.         private IDemoView _view;
  73.         private IDemoModel _model;
  74.  
  75.         public DemoPresenter(IDemoView view, IDemoModel model)
  76.         {
  77.             _view = view;
  78.             _model = model;
  79.         }
  80.  
  81.         public void OnButtonClick()
  82.         {
  83.             _model.DoWork(_view.SetProgress);
  84.         }
  85.     }
  86. }
  87.  
  88. namespace ConsoleApplication4
  89. {
  90.     using System;
  91.     using System.Linq;
  92.     using System.Threading;
  93.  
  94.     public class DemoModel : IDemoModel
  95.     {
  96.         public void DoWork(Action<int> progressCallback)
  97.         {
  98.             new Thread(() =>
  99.             {
  100.                 foreach (var el in Enumerable.Range(0, 101))
  101.                 {
  102.                     progressCallback(el);
  103.                     Thread.Sleep(100);
  104.                 }
  105.             }).Start();
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment