• 多线程编写


    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

  • 相关阅读:
    前端面试题—Js
    前端面试题—css
    前端面试题—html
    JavaScript 闭包
    JavaScript 计算斐波那切数列
    JavaScript continue使用
    JavaScript break 使用
    JavaScript 综合运算 (数字运算符+比较运算符+逻辑运算符)
    JavaScript 逻辑运算符 特殊字符 纯数字字符串
    JavaScript 比较运算符 特殊字符 纯数字字符串
  • 原文地址:https://www.cnblogs.com/jiebian/p/2785492.html
Copyright © 2020-2023  润新知