Skip to content

Commit 8044ff6

Browse files
committed
init
1 parent 2ac4904 commit 8044ff6

10 files changed

+894
-62
lines changed

EasyTransaction/BatchTran.cs

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading;
7+
8+
namespace EasyTransaction
9+
{
10+
11+
delegate void BatchMessagePushHandle(MessageEventArgs args);
12+
13+
class MessageEventArgs : EventArgs
14+
{
15+
public string Message
16+
{
17+
get;
18+
private set;
19+
}
20+
21+
public MessageEventArgs(string msg)
22+
{
23+
this.Message = msg;
24+
}
25+
26+
}
27+
28+
/// <summary>
29+
/// 批量文件翻译
30+
/// </summary>
31+
class BatchTran
32+
{
33+
public event BatchMessagePushHandle BatchMessagePushed;
34+
/// <summary>
35+
/// 翻译间隔
36+
/// </summary>
37+
public int Interval { get; set; }
38+
39+
/// <summary>
40+
/// 翻译结果文件保存地址
41+
/// </summary>
42+
public string SaveDir { get; set; }
43+
44+
/// <summary>
45+
/// 翻译工具
46+
/// </summary>
47+
public TranslateTool Tool { get; set; }
48+
49+
/// <summary>
50+
/// 工作线程
51+
/// </summary>
52+
private Thread workThread;
53+
54+
/// <summary>
55+
/// 翻译文件名
56+
/// </summary>
57+
private string srcFileName;
58+
59+
/// <summary>
60+
/// 是否正在翻译中
61+
/// </summary>
62+
public bool IsRuning
63+
{
64+
get
65+
{
66+
return workThread != null && workThread.ThreadState != ThreadState.Aborted;
67+
}
68+
}
69+
70+
public BatchTran(TranslateTool tool)
71+
{
72+
this.Tool = tool;
73+
this.Tool.Start();
74+
}
75+
76+
public void Start(string fileName)
77+
{
78+
if (IsRuning)
79+
{
80+
throw new Exception("翻译线程正在工作中");
81+
}
82+
if (string.IsNullOrWhiteSpace(fileName))
83+
{
84+
throw new ArgumentNullException("文件不能为空");
85+
}
86+
if (workThread !=null)
87+
{
88+
workThread.Abort();
89+
}
90+
workThread = new Thread(new ThreadStart(WorkGo));
91+
srcFileName = fileName;
92+
workThread.Start();
93+
94+
}
95+
public void Stop()
96+
{
97+
if (!IsRuning)
98+
{
99+
return;
100+
}
101+
workThread.Abort();
102+
}
103+
private void WorkGo()
104+
{
105+
IList<string> dataRows;
106+
try
107+
{
108+
dataRows = FileLib.ScanEnterFile(srcFileName);
109+
if (dataRows == null)
110+
{
111+
pushMessage("翻译文件不存在");
112+
return;
113+
}
114+
if (dataRows.Count == 0)
115+
{
116+
pushMessage("翻译文件内容为空,翻译停止");
117+
return;
118+
}
119+
}
120+
catch (Exception ex)
121+
{
122+
pushMessage("读取翻译文件内容失败:" + ex.Message);
123+
return;
124+
}
125+
126+
pushMessage("翻译任务完成");
127+
128+
}
129+
130+
/// <summary>
131+
/// 推送消息
132+
/// </summary>
133+
/// <param name="msg"></param>
134+
private void pushMessage(string msg)
135+
{
136+
if (BatchMessagePushed == null)
137+
{
138+
return;
139+
}
140+
BatchMessagePushed(new MessageEventArgs(msg));
141+
}
142+
143+
144+
}
145+
}

EasyTransaction/EasyTransaction.csproj

+9-3
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,20 @@
4444
<Reference Include="System.Xml" />
4545
</ItemGroup>
4646
<ItemGroup>
47-
<Compile Include="Form1.cs">
47+
<Compile Include="BatchTran.cs" />
48+
<Compile Include="FileLib.cs" />
49+
<Compile Include="FrmMain.cs">
4850
<SubType>Form</SubType>
4951
</Compile>
50-
<Compile Include="Form1.Designer.cs">
51-
<DependentUpon>Form1.cs</DependentUpon>
52+
<Compile Include="FrmMain.Designer.cs">
53+
<DependentUpon>FrmMain.cs</DependentUpon>
5254
</Compile>
5355
<Compile Include="Program.cs" />
5456
<Compile Include="Properties\AssemblyInfo.cs" />
57+
<Compile Include="TranslateTool.cs" />
58+
<EmbeddedResource Include="FrmMain.resx">
59+
<DependentUpon>FrmMain.cs</DependentUpon>
60+
</EmbeddedResource>
5561
<EmbeddedResource Include="Properties\Resources.resx">
5662
<Generator>ResXFileCodeGenerator</Generator>
5763
<LastGenOutput>Resources.Designer.cs</LastGenOutput>

EasyTransaction/FileLib.cs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
7+
namespace EasyTransaction
8+
{
9+
/// <summary>
10+
/// 文件操作库
11+
/// </summary>
12+
class FileLib
13+
{
14+
/// <summary>
15+
/// 将文件内容按行全部读取
16+
/// </summary>
17+
/// <param name="fileName">文件名</param>
18+
/// <returns>行内容,如果文件不存在则返回null</returns>
19+
public static IList<string> ScanEnterFile(string fileName)
20+
{
21+
if (string.IsNullOrEmpty(fileName))
22+
return null;
23+
if ( !File.Exists(fileName))
24+
return null;
25+
//翻译为大文件,初始容量大一些
26+
var list = new List<string>(1000);
27+
using (StreamReader sr = File.OpenText(fileName))
28+
{
29+
while (!sr.EndOfStream)
30+
{
31+
list.Add(sr.ReadLine());
32+
}
33+
sr.Close();
34+
}
35+
return list;
36+
}
37+
}
38+
}

EasyTransaction/Form1.Designer.cs

-39
This file was deleted.

EasyTransaction/Form1.cs

-19
This file was deleted.

0 commit comments

Comments
 (0)