zoukankan      html  css  js  c++  java
  • 多线程编写

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using PONumberServer;
    using System.Threading;
    using System.Threading.Tasks;
    using Untity;
    using System.Reflection;
    using ServerInterFace;
    namespace MultiThreadTestTool
    {
    public partial class MainForm : Form
    {
    public List<long> timeList;
    private object obj;
    public string ServerType=string.Empty;
    public IServer Iserver;
    private string ServerUrl;
    public MainForm()
    {
    InitializeComponent();

    string strChoseItem = Configer.GetConfig("ChoseItem");
    if (!string.IsNullOrWhiteSpace(strChoseItem))
    {
    string[] arrItem = strChoseItem.Split(',');
    cmbChose.Items.AddRange(arrItem);
    cmbChose.SelectedIndex = 0;
    }
    else {
    btnLoad.Enabled = false;
    }

    string strThreadItem = Configer.GetConfig("ThreadItem");
    if (!string.IsNullOrWhiteSpace(strThreadItem))
    {
    string[] arrItem = strThreadItem.Split(',');
    cmbThreadChose.Items.AddRange(arrItem);
    cmbThreadChose.SelectedIndex = 0;
    }

    tbInputValue.Text = "1021739";
    timeList = new List<long>();
    obj = new object();


    }

    private void btnLoad_Click(object sender, EventArgs e)
    {
    Logger.WriteProcessLog("开始时间:" + DateTime.Now.ToString());
    btnLoad.Enabled = false;
    timeList.Clear();
    ServerType = cmbChose.SelectedItem.ToString();
    if (string.IsNullOrWhiteSpace(ServerType))
    {
    return;
    }
    ServerUrl = Configer.GetConfig(ServerType) + tbInputValue.Text.Trim();
    Assembly MyAssembly = Assembly.Load(ServerType + "Server");
    Iserver = (IServer)MyAssembly.CreateInstance(ServerType + "Server." + ServerType);
    int threadNum=0;
    int.TryParse(cmbThreadChose.SelectedItem.ToString(), out threadNum);
    threadNum = threadNum == 0 ? 1 : threadNum;
    var taskQueue = new Queue<Task>();

    for (int i = 0; i < threadNum; i++)
    {
    taskQueue.Enqueue(Task.Factory.StartNew(() =>
    {
    Execute();

    }));

    }

    Task.Factory.ContinueWhenAll(taskQueue.ToArray(), completedTasks =>
    {
    taskCompleteExecute(threadNum);
    });

    }

    void taskCompleteExecute(int threadNum)
    {
    int count = timeList.Count;
    long maxtime = timeList.Max();
    long mintime = timeList.Min();
    long totalTime = 0;
    timeList.ForEach((time) => { totalTime += time; });
    float avgTime = totalTime / threadNum;
    string strResult = "MinTime:" + mintime.ToString() + Environment.NewLine +
    "MaxTime:" + maxtime.ToString() + Environment.NewLine +
    "AvgTime:" + avgTime.ToString() + Environment.NewLine +
    "EndTime:" + DateTime.Now.ToString();
    ;
    SetTextBoxValue(strResult);
    SetButtonValue(null);
    }
    void SetButtonValue(object obj) {
    if (btnLoad.InvokeRequired)
    {
    btnLoad.Invoke(new Action<object>(SetButtonValue), obj);
    }
    else
    {
    this.btnLoad.Enabled = true;
    }
    }
    void SetTextBoxValue(object obj) {
    if (tbResult.InvokeRequired)
    {
    tbResult.Invoke(new Action<object>(SetTextBoxValue),obj);
    }
    else {
    this.tbResult.Text = obj.ToString();
    }
    }

    public void Execute() {

    long time = Iserver.LoadData(ServerUrl);
    lock (obj)
    {
    timeList.Add(time);
    Logger.WriteProcessLog("耗时:"+ time.ToString() +" : "+DateTime.Now.ToString());
    }
    }
    }
    }

    个人网站:http://www.jiebian.or

  • 相关阅读:
    对 Sea.js 进行配置 seajs.config
    jquery 设置style:display
    Js获取当前日期时间及其它操作
    2.4 js数组与字符串的转换 > 我的程序猿之路:第十四章
    2.3 js刷新页面所有 > 我的程序猿之路:第十三章
    2.2 HTML/JSP中控制按钮的显示和隐藏与单页面多列表 > 我的程序猿之路:第十二章
    2.1 字符串替换字符或字符设置为空 > 我的程序猿之路:第十一章
    1.9 23种设计模式之单例模式详情 > 我的程序猿之路:第九章
    1.8 Oracle 登陆时报错信息:无监听程序 > 我的程序猿之路:第八章
    1.7 Oracle 11g )impdp(数据泵)--导入dmp文件(全过程) > 我的程序猿之路:第七章
  • 原文地址:https://www.cnblogs.com/jiebian/p/2785492.html
Copyright © 2011-2022 走看看