-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMailTask.cs
103 lines (87 loc) · 2.64 KB
/
MailTask.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using MultiThreading.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MultiThreading
{
public class MailTask : INotifyPropertyChanged
{
public Guid TaskId { get; set; } = Guid.NewGuid();
private bool isRunning;
private bool isStarted;
private DateTime? nextRunning;
public MailProviderType ProviderType { get; set; }
public MailTask(MailProviderType providerType)
{
ProviderType = providerType;
}
public bool IsRunning
{
get => isRunning;
set
{
isRunning = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsRunning)));
}
}
public bool IsStarted
{
get => isStarted;
set
{
isStarted = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsStarted)));
}
}
public int Second { get; set; } = 60;
public DateTime? NextRunning
{
get => nextRunning;
set
{
nextRunning = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(NextRunning)));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public async Task Run(int number)
{
var manager = new MailManager();
while (IsStarted)
{
IsRunning = true;
var mails = Enumerable.Empty<MailObject>();
if (ProviderType == MailProviderType.GoogleMail)
{
mails = FakeDataCreator.GetGoogleMails(number);
}
else if (ProviderType == MailProviderType.Smtp)
{
mails = FakeDataCreator.GetSmtpMails(number);
}
else
{
mails = FakeDataCreator.GetMails(number);
}
manager.AddMails(mails);
await manager.SendAllMails();
IsRunning = false;
NextRunning = DateTime.Now.AddSeconds(Second);
await Task.Delay(Second * 1000);
}
}
public void Start()
{
IsStarted = true;
NextRunning = DateTime.Now.AddSeconds(Second);
}
public void Stop()
{
IsStarted = false;
NextRunning = null;
}
}
}