|
@@ -10,22 +10,48 @@ using System.Windows.Forms;
|
|
using System.IO;
|
|
using System.IO;
|
|
using log = Log.Log;
|
|
using log = Log.Log;
|
|
using Excel = Microsoft.Office.Interop.Excel;
|
|
using Excel = Microsoft.Office.Interop.Excel;
|
|
|
|
+using System.Threading;
|
|
|
|
|
|
namespace frequency
|
|
namespace frequency
|
|
{
|
|
{
|
|
public partial class Form1 : Form
|
|
public partial class Form1 : Form
|
|
{
|
|
{
|
|
|
|
+ public long currentIndex;
|
|
|
|
+ public long totalNum;
|
|
|
|
+ public Dictionary<string, int> finalResult;
|
|
|
|
+
|
|
public Form1()
|
|
public Form1()
|
|
{
|
|
{
|
|
|
|
+ // init logger
|
|
log.Init("frequency.log");
|
|
log.Init("frequency.log");
|
|
|
|
+
|
|
|
|
+ // init params
|
|
path = System.Environment.CurrentDirectory;
|
|
path = System.Environment.CurrentDirectory;
|
|
|
|
+
|
|
|
|
+ // init component
|
|
InitializeComponent();
|
|
InitializeComponent();
|
|
|
|
+
|
|
|
|
+ // init ShowProgress
|
|
|
|
+ Thread.Sleep(1000);
|
|
|
|
+ Thread thread = new Thread(this.ShowProgress);
|
|
|
|
+ thread.IsBackground = true;
|
|
|
|
+ thread.Start();
|
|
}
|
|
}
|
|
|
|
|
|
private string[] originTextsOrder;
|
|
private string[] originTextsOrder;
|
|
private string[] originTextsInverse;
|
|
private string[] originTextsInverse;
|
|
private string[] targetTexts;
|
|
private string[] targetTexts;
|
|
|
|
|
|
|
|
+ // progress status
|
|
|
|
+ const string CAL_WAIT = "wait";
|
|
|
|
+ const string CAL_RUNNING = "running";
|
|
|
|
+ const string CAL_ERROR = "error";
|
|
|
|
+ const string CAL_SUCCESS = "success";
|
|
|
|
+ const string CAL_EXPORT = "exporting";
|
|
|
|
+
|
|
|
|
+ public delegate void ChangeProgressStatus();
|
|
|
|
+ public delegate void CallBackDelegate();
|
|
|
|
+
|
|
private void SelectOriginButton(object sender, EventArgs e)
|
|
private void SelectOriginButton(object sender, EventArgs e)
|
|
{
|
|
{
|
|
var fileDialog = new OpenFileDialog();
|
|
var fileDialog = new OpenFileDialog();
|
|
@@ -62,23 +88,19 @@ namespace frequency
|
|
|
|
|
|
private void Calculate_Click(object sender, EventArgs e)
|
|
private void Calculate_Click(object sender, EventArgs e)
|
|
{
|
|
{
|
|
- this.label1.Text = "计算中...";
|
|
|
|
- this.label1.Refresh();
|
|
|
|
|
|
+ if (null == this.originTextPath || null == this.targetTextPath || "" == this.originTextPath.Trim() || "" == this.targetTextPath.Trim())
|
|
|
|
+ {
|
|
|
|
+ MessageBox.Show("请先选择文件");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //this.progress.Text = "计算中...";
|
|
|
|
+ //this.progress.Refresh();
|
|
|
|
+ this.progressStatus = CAL_RUNNING;
|
|
this.calculate.Enabled = false;
|
|
this.calculate.Enabled = false;
|
|
|
|
|
|
var code = this.Calculate();
|
|
var code = this.Calculate();
|
|
- if (code != 0)
|
|
|
|
- {
|
|
|
|
- this.label1.Text = "计算出错";
|
|
|
|
- this.label1.Refresh();
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- this.label1.Text = "计算完成";
|
|
|
|
- this.label1.Refresh();
|
|
|
|
- }
|
|
|
|
Application.DoEvents();
|
|
Application.DoEvents();
|
|
- this.calculate.Enabled = true;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
private int Calculate()
|
|
private int Calculate()
|
|
@@ -104,21 +126,40 @@ namespace frequency
|
|
this.CleanTarget();
|
|
this.CleanTarget();
|
|
|
|
|
|
// look up
|
|
// look up
|
|
- var result = this.Lookup();
|
|
|
|
- log.D("Calculate_Click look up success with length: {0}", result.Count);
|
|
|
|
|
|
+ Thread runLoopUp = new Thread(Lookup);
|
|
|
|
+ CallBackDelegate cbd = LoopUpCallBack;
|
|
|
|
+ runLoopUp.Start(cbd);
|
|
|
|
+
|
|
|
|
+ log.D("Calculate_Click look up success");
|
|
|
|
|
|
// write data to file
|
|
// write data to file
|
|
- this.label1.Text = "导出数据中...";
|
|
|
|
- this.label1.Refresh();
|
|
|
|
|
|
+ //this.progress.Text = "导出数据中...";
|
|
|
|
+ //this.progress.Refresh();
|
|
|
|
+ //this.progressStatus = CAL_EXPORT;
|
|
|
|
|
|
- var msg = this.Write2Xlsx(result);
|
|
|
|
|
|
+ //var msg = this.Write2Xlsx(result);
|
|
|
|
+ //if ("" != msg)
|
|
|
|
+ //{
|
|
|
|
+ // log.E("Calculate_Click write data to xlsx error: {0}", msg);
|
|
|
|
+ // return 1;
|
|
|
|
+ //}
|
|
|
|
+ log.D("Calculate_Click write data to xlsx success");
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void LoopUpCallBack()
|
|
|
|
+ {
|
|
|
|
+ progressStatus = CAL_EXPORT;
|
|
|
|
+ var msg = this.Write2Xlsx(finalResult);
|
|
if ("" != msg)
|
|
if ("" != msg)
|
|
{
|
|
{
|
|
|
|
+ progressStatus = CAL_ERROR;
|
|
|
|
+ calculate.Enabled = true;
|
|
log.E("Calculate_Click write data to xlsx error: {0}", msg);
|
|
log.E("Calculate_Click write data to xlsx error: {0}", msg);
|
|
- return 1;
|
|
|
|
|
|
+ return ;
|
|
}
|
|
}
|
|
- log.D("Calculate_Click write data to xlsx success");
|
|
|
|
- return 0;
|
|
|
|
|
|
+ progressStatus = CAL_SUCCESS;
|
|
|
|
+ log.D("Calculate_Click write data to xlsx success with length: {0}", finalResult.Count);
|
|
}
|
|
}
|
|
|
|
|
|
private void SeeResult_Click(object sender, EventArgs e)
|
|
private void SeeResult_Click(object sender, EventArgs e)
|
|
@@ -318,10 +359,10 @@ namespace frequency
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- private Dictionary<string, int> Lookup()
|
|
|
|
|
|
+ private void Lookup(object o)
|
|
{
|
|
{
|
|
this.currentIndex = 0;
|
|
this.currentIndex = 0;
|
|
- this.totalNum = this.originTextsOrder.Length + this.originTextsInverse.Length;
|
|
|
|
|
|
+ this.totalNum = this.targetTexts.Length * this.originTextsOrder.Length + this.originTextsInverse.Length * this.targetTexts.Length;
|
|
Dictionary<string, int> result = new Dictionary<string, int>();
|
|
Dictionary<string, int> result = new Dictionary<string, int>();
|
|
for (int i = 0; i < this.originTextsOrder.Length; i++)
|
|
for (int i = 0; i < this.originTextsOrder.Length; i++)
|
|
{
|
|
{
|
|
@@ -362,17 +403,22 @@ namespace frequency
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- return result;
|
|
|
|
|
|
+ finalResult = result;
|
|
|
|
+ log.D("Lookup calcate success with length: {0}", result.Count);
|
|
|
|
+
|
|
|
|
+ var cbd = o as CallBackDelegate;
|
|
|
|
+ cbd();
|
|
|
|
+ return ;
|
|
}
|
|
}
|
|
|
|
|
|
private bool judge(string origin, string target)
|
|
private bool judge(string origin, string target)
|
|
{
|
|
{
|
|
- var strs = origin.Split('-');
|
|
|
|
|
|
+ var strs = origin.Split(new char[] { '-' }, 2, StringSplitOptions.None);
|
|
if (strs.Length < 2)
|
|
if (strs.Length < 2)
|
|
{
|
|
{
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
- if ((strs[0] + "-" + strs[1] == target) || (strs[0] + "--" + strs[1] == target))
|
|
|
|
|
|
+ if (target.Contains(strs[0] + "-" + strs[1]) || target.Contains(strs[0] + "--" + strs[1]))
|
|
{
|
|
{
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
@@ -405,14 +451,6 @@ namespace frequency
|
|
index++;
|
|
index++;
|
|
}
|
|
}
|
|
|
|
|
|
- //sheet.Cells[1, 1] = "Name";
|
|
|
|
- //sheet.Cells[1, 2] = "Sex";
|
|
|
|
- //for (int i = 2; i < 5; i++)
|
|
|
|
- //{
|
|
|
|
- // sheet.Cells[i, 1] = "Name" + i;
|
|
|
|
- // sheet.Cells[i, 2] = "Sex" + i;
|
|
|
|
- //}
|
|
|
|
-
|
|
|
|
try
|
|
try
|
|
{
|
|
{
|
|
sheet.SaveAs(filename);
|
|
sheet.SaveAs(filename);
|
|
@@ -439,12 +477,61 @@ namespace frequency
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- private void showProgress()
|
|
|
|
|
|
+ private void ShowProgress()
|
|
{
|
|
{
|
|
|
|
+
|
|
|
|
+ log.D("ShowProgress start successful");
|
|
while (true)
|
|
while (true)
|
|
{
|
|
{
|
|
- this.label1.Text = this.label1.Text;
|
|
|
|
|
|
+ if (this.IsHandleCreated)
|
|
|
|
+ {
|
|
|
|
+ Invoke(new ChangeProgressStatus(ChangeStatus));
|
|
|
|
+ }
|
|
|
|
+ //log.D("currentIndex: {0}, totalNum: {1}", currentIndex, totalNum);
|
|
|
|
+ Thread.Sleep(1000);
|
|
|
|
+ }
|
|
|
|
+ log.D("ShowProgress had finished");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void ChangeStatus()
|
|
|
|
+ {
|
|
|
|
+ string status = "";
|
|
|
|
+ if (CAL_WAIT == progressStatus || "" == progressStatus)
|
|
|
|
+ {
|
|
|
|
+ status = "计算未开始";
|
|
|
|
+ }
|
|
|
|
+ if (CAL_RUNNING == progressStatus)
|
|
|
|
+ {
|
|
|
|
+ status = string.Format("计算中... {0}/{1}", currentIndex, totalNum);
|
|
}
|
|
}
|
|
|
|
+ if (CAL_ERROR == progressStatus)
|
|
|
|
+ {
|
|
|
|
+ status = "计算出错";
|
|
|
|
+ }
|
|
|
|
+ if (CAL_EXPORT == progressStatus)
|
|
|
|
+ {
|
|
|
|
+ status = "导出数据中...";
|
|
|
|
+ }
|
|
|
|
+ if (CAL_SUCCESS == progressStatus)
|
|
|
|
+ {
|
|
|
|
+ status = "计算完成";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ log.D("ChangeStatus with status {0}", status);
|
|
|
|
+ this.progress.Text = status;
|
|
|
|
+ this.progress.Refresh();
|
|
|
|
+
|
|
|
|
+ if (progressStatus == CAL_ERROR || progressStatus == CAL_SUCCESS)
|
|
|
|
+ {
|
|
|
|
+ this.calculate.Enabled = true;
|
|
|
|
+ this.calculate.Refresh();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void Form1_FormClosing(object sender, FormClosingEventArgs e)
|
|
|
|
+ {
|
|
|
|
+ System.Environment.Exit(0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|