//【显示输出窗口】 private void showMessageMenu_Click(object sender, EventArgs e) { if (listStrategy.SelectedItems.Count <= 0) { return; } ListViewItem lvi = listStrategy.SelectedItems[0]; ProjectStrategy projectStrag = null; foreach (ProjectStrategy ps in listProjects) { if (lvi.Text == ps.username) { projectStrag = ps; } } if (projectStrag == null) { return; } if (projectStrag.runFlag == EnumRunState.运行中 || projectStrag.runFlag == EnumRunState.异常) { if (projectStrag.showMessage != null) { projectStrag.showMessage.Show(); } } }
private void TestTickMenuItem_Click(object sender, EventArgs e) { if (listStrategy.SelectedItems.Count <= 0) { return; } ListViewItem lvi = listStrategy.SelectedItems[0]; ProjectStrategy projectStrag = null; foreach (ProjectStrategy ps in listProjects) { if (lvi.Text == ps.username) { projectStrag = ps; } } if (projectStrag == null) { return; } if (projectStrag.runFlag == EnumRunState.运行中) { projectStrag.runStrategy.TestTick(); } }
//【停止】 private void Stop_MenuItem_Click(object sender, EventArgs e) { if (listStrategy.SelectedItems.Count <= 0) { return; } ListViewItem lvi = listStrategy.SelectedItems[0]; ProjectStrategy projectStrag = null; foreach (ProjectStrategy ps in listProjects) { if (lvi.Text == ps.username) { projectStrag = ps; } } if (projectStrag == null) { return; } if (projectStrag.runFlag == EnumRunState.停止 || projectStrag.runFlag == EnumRunState.未运行) { return; } projectStrag.showMessage.Print("策略已经停止运行"); projectStrag.CutOff(); projectStrag.Free(); projectStrag.Exit(); projectStrag.showMessage.Close(); projectStrag.runFlag = EnumRunState.停止; lvi.SubItems[2].Text = EnumRunState.停止.ToString(); lvi.SubItems[5].Text = "已停止"; }
//【删除】 private void Delete_MenuItem_Click(object sender, EventArgs e) { if (listStrategy.SelectedItems.Count <= 0) { return; } ListViewItem lvi = listStrategy.SelectedItems[0]; ProjectStrategy projectStrag = null; foreach (ProjectStrategy ps in listProjects) { if (lvi.Text == ps.username) { projectStrag = ps; if (projectStrag.runFlag == EnumRunState.运行中 || projectStrag.runFlag == EnumRunState.异常 || projectStrag.runFlag == EnumRunState.超时) { MessageBox.Show("运行状态下无法删除", "提示"); return; } else { listProjects.Remove(ps); foreach (ListViewItem llv in listStrategy.Items) { if (llv.Text == ps.username) { listStrategy.Items.Remove(llv); break; } } return; } } } }
public static List <ProjectStrategy> GetProjectInfoByFile() { List <ProjectStrategy> listPros = new List <ProjectStrategy>(); string filePath = @".\Config"; DirectoryInfo directory = new DirectoryInfo(filePath); if (!directory.Exists)//如果文件夹不存在 { return(null); } if (!File.Exists(filePath + @"\ProjectInfCofig.txt")) //如果文件不存在 { return(null); } FileStream file = new FileStream(filePath + @"\ProjectInfCofig.txt", FileMode.Open, FileAccess.Read, FileShare.Read); StreamReader sr = new StreamReader(file, Encoding.Default); string reader = null; while ((reader = sr.ReadLine()) != null) { //过滤 if (reader == "" || reader == "\n") { continue; } // string[] projectLine = reader.Split(new char[] { ',' }); if (projectLine.Length < 4) { continue; } ProjectStrategy pic = new ProjectStrategy(); pic.username = projectLine[0]; pic.projectName = projectLine[1]; pic.fileName = projectLine[2]; pic.strategyName = projectLine[3]; bool IsAdd = true; foreach (ProjectStrategy ps in listPros) { if (pic.username == ps.username || pic.projectName == ps.projectName) { IsAdd = false; break; } } if (IsAdd) { listPros.Add(pic); } } sr.Close(); file.Close(); return(listPros); }
private void listStrategy_SelectedIndexChanged(object sender, EventArgs e) { if (listStrategy.SelectedItems.Count <= 0) { return; } ListViewItem lvi = listStrategy.SelectedItems[0]; ProjectStrategy projectStrag = null; foreach (ProjectStrategy ps in listProjects) { if (lvi.Text == ps.username) { projectStrag = ps; } } ToolStripMenuItem mm = null; for (int i = 0; i < contextMenuStrip.Items.Count; i++) { if (contextMenuStrip.Items[i] is ToolStripMenuItem) { foreach (ToolStripMenuItem m in ((ToolStripMenuItem)contextMenuStrip.Items[i]).DropDownItems) { if (m.Text == "显示情报") { mm = m; } } } } if (projectStrag != null) { if (projectStrag.runFlag == EnumRunState.运行中 || projectStrag.runFlag == EnumRunState.异常) { if (mm != null) { mm.Checked = projectStrag.showMessage.GetHeartState(); } } else { if (mm != null) { mm.Checked = false; } } } }
static void Transmit(ref ProjectStrategy ps, UserInfoConfig user) { ps.password = user.password; ps.valsign = user.valsign; ps.contractID = user.contractID; string[] ini = user.trade_addr_port.Split(new char[] { ':' }); ps.trade_addr = ini[0]; ps.trade_port = ini[1]; ini = user.market_addr_port.Split(new char[] { ':' }); ps.market_addr = ini[0]; ps.market_port = ini[1]; ini = user.http_addr_port.Split(new char[] { ':' }); ps.http_addr = ini[0]; ps.http_port = ini[1]; }
//主窗体退出程序 public void ExitOnClose() { ProjectStrategy projectStrag = null; foreach (ProjectStrategy ps in listProjects) { projectStrag = ps; if (projectStrag.runFlag == EnumRunState.停止 || projectStrag.runFlag == EnumRunState.未运行) { continue; } projectStrag.CutOff(); projectStrag.Free(); projectStrag.Exit(); projectStrag.showMessage.Close(); projectStrag.runFlag = EnumRunState.停止; projectStrag.runStrategy = null; } }
//【显示心跳信息】 private void showHeartMenu_Click(object sender, EventArgs e) { if (listStrategy.SelectedItems.Count <= 0) { return; } ListViewItem lvi = listStrategy.SelectedItems[0]; ProjectStrategy projectStrag = null; foreach (ProjectStrategy ps in listProjects) { if (lvi.Text == ps.username) { projectStrag = ps; } } if (projectStrag == null) { return; } if (projectStrag.runFlag == EnumRunState.运行中 || projectStrag.runFlag == EnumRunState.异常) { if (projectStrag.showMessage != null) { if ((sender as ToolStripMenuItem).Checked == false) { projectStrag.showMessage.HeartInfoOnConsole(); (sender as ToolStripMenuItem).Checked = true; } else { projectStrag.showMessage.HeartInfoOnConsole(false); (sender as ToolStripMenuItem).Checked = false; } } } }
/**************************菜单事件***********************/ //【启动】 private void Start_MenuItem_Click(object sender, EventArgs e) { if (listStrategy.SelectedItems.Count <= 0) { return; } ListViewItem lvi = listStrategy.SelectedItems[0]; string filePath = null; string ClassName = null; ProjectStrategy projectStrag = null; foreach (ProjectStrategy ps in listProjects) { if (lvi.Text == ps.username) { filePath = ps.fileName; ClassName = ps.strategyName; projectStrag = ps; } } if (filePath == null || ClassName == null) { MessageBox.Show("此条记录已经失效!", "提示"); return; } if (projectStrag == null) { return; } //运行中无法连续启动 if (projectStrag.runFlag == EnumRunState.运行中 || projectStrag.runFlag == EnumRunState.异常 || projectStrag.runFlag == EnumRunState.超时) { return; } //判断是否存在此文件 if (!System.IO.File.Exists(filePath)) { MessageBox.Show("策略文件不存在或者被删除", "提示"); return; } //启动 Type typeStag = null; foreach (Type ty in listTypes) { if (ty.FullName == ClassName) { typeStag = ty; break; } } Strategy stt = null; try { stt = (Strategy)typeStag.Assembly.CreateInstance(typeStag.FullName); }catch (Exception es) { MessageBox.Show(es.Message, "提示"); return; } OutPut op = new OutPut(); op.Text = lvi.SubItems[1].Text; op.Show(manSHJH.GetMainFrame().dockPanel1, DockState.DockBottomAutoHide); if (!projectStrag.BeforeInit(stt, this, op)) { MessageBox.Show("初始化交易模型失败!", "提示"); return; } projectStrag.Init(); projectStrag.runFlag = EnumRunState.运行中; lvi.SubItems[2].Text = EnumRunState.运行中.ToString(); }
//添加工程 private void btnAddProject_Click(object sender, EventArgs e) { string userid = cboxSelectAccount.Text; string strategy = cboxSelectStrategy.Text; string porjname = txtProjectName.Text; #region 输入检测 if (userid == "无" || strategy == "无") { return; } if (userid == "" || strategy == "" || porjname == "") { MessageBox.Show("输入不能为空", "提示"); return; } foreach (ListViewItem lvi in listStrategy.Items) { if (lvi.Text == userid) { MessageBox.Show("账户不能重复", "提示"); return; } if (lvi.SubItems[1].Text == porjname) { MessageBox.Show("工程名不能重复", "提示"); return; } } #endregion #region 更新视图 //获取文件名 string filename = GetFileNameByClassName(strategy); if (filename == null) { MessageBox.Show("添加失败!文件不存在", "提示"); return; } ListViewItem lv = new ListViewItem(); lv.Text = userid; lv.SubItems.Add(porjname); lv.SubItems.Add(EnumRunState.未运行.ToString()); lv.SubItems.Add(System.IO.Path.GetFileName(filename)); lv.SubItems.Add(strategy); lv.SubItems.Add(". . ."); //添加到视图 listStrategy.Items.Add(lv); #endregion #region 保存信息 //保存数据 ProjectStrategy pps = new ProjectStrategy(); pps.username = userid; pps.projectName = porjname; pps.runFlag = EnumRunState.未运行; pps.fileName = filename; pps.strategyName = strategy; pps.runState = ""; Transmit(ref pps, GetUserInfoConfig(userid)); listProjects.Add(pps); //保存信息到文件中 SaveProjectToFile(); #endregion }
//读取账户信息 和 工程配置信息 private void ReadUserInfo() { #region 加载用户信息 if (listUsers == null) { listUsers = OperFile.GetUserInfoByFile(); if (listUsers == null) { return; } foreach (UserInfoConfig ufi in listUsers) { cboxSelectAccount.Items.Add(ufi.username); } } #endregion #region 加载工程配置信息 List <ProjectStrategy> tmpPS = null; tmpPS = OperFile.GetProjectInfoByFile(); if (tmpPS == null || tmpPS.Count == 0) { return; } listProjects = tmpPS; bool IsChange = false; //检查是否存在失效信息 for (int i = 0; i < listProjects.Count; i++) { ProjectStrategy pic = listProjects[i]; bool IsIn = false; foreach (UserInfoConfig ufi in listUsers) { if (pic.username == ufi.username) { Transmit(ref pic, ufi); IsIn = true; break; } } if (IsIn == false) { IsChange = true; listProjects.RemoveAt(i); } } if (IsChange == true) { OperFile.SaveProjectInfoConfig(listProjects); } #endregion #region 显示信息 foreach (ProjectStrategy ps in listProjects) { ListViewItem lvi = new ListViewItem(); lvi.Text = ps.username; lvi.SubItems.Add(ps.projectName); lvi.SubItems.Add(EnumRunState.未运行.ToString()); lvi.SubItems.Add(System.IO.Path.GetFileName(ps.fileName)); lvi.SubItems.Add(ps.strategyName); lvi.SubItems.Add(". . ."); listStrategy.Items.Add(lvi); } #endregion }