LogControl.cs
2.0 KB
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
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AutoScanAndLabel
{
public partial class LogControl : UserControl
{
public LogControl()
{
CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
showLogProDelegate = new ShowLogProDelegate(ShowLogPro);
//LogUtil.ShowLog += LogUtil_ShowLog;
}
delegate void ShowLogProDelegate(string msg, Color color);
ShowLogProDelegate showLogProDelegate;
private void LogUtil_ShowLog(string msg, Color color)
{
if (this.InvokeRequired)
this.Invoke(showLogProDelegate, msg, color);
else
ShowLogPro(msg, color);
}
private List<string> logList = new List<string>();
public int showCount = 30;
private void ShowLogPro(string msg, Color color)
{
if(Monitor.TryEnter(logBox))
{
try
{
if (logList.Count >= showCount)
{
logList.RemoveAt(0);
}
DateTime now = DateTime.Now;
logList.Add(now.ToLongTimeString() + " " + msg + Environment.NewLine);
logBox.Text = string.Join("", logList);
logBox.Select(logBox.Text.Length, 0);
logBox.ScrollToCaret();
}
catch (Exception ex)
{
LogUtil.error("出错:" + ex.ToString());
}
finally
{
Monitor.Exit(logBox);
}
}
}
private void LogControl_Load(object sender, EventArgs e)
{
}
}
}