• 多线程编写


    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

  • 相关阅读:
    项目风险软考之软工基础知识(1)
    序列输出ZOJ1108 FatMouse's Speed
    电平波特率UART自适应波特率的设置方法
    nullnull2013金山西山居创意游戏程序挑战赛——初赛(3)生日猜猜猜
    删除监听oracle单库彻底删除干净的方法
    类关闭由一个activity启动第二个activity
    代码文件android:向手机卡上写入文件时总是不成功,解决办法
    功能应用程序延迟发送:4款定时发送短信的应用程序
    监听事件android activity中键盘的监听
    nullnull爆笑百家讲坛之十大逆袭情侣.
  • 原文地址:https://www.cnblogs.com/jiebian/p/2785492.html
Copyright © 2020-2023  润新知