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

  • 相关阅读:
    sql函数 StringSplit(SELECT * from Split('黄色,蓝色,黑色',','))
    跨表循环写插入sql语句
    将ExCel导入数据库
    行转列
    js正则匹配
    ASP.NET下载远程图片保存到本地的方法、保存抓取远程图片
    C# 解析json类型字符串
    上传图片
    存储过程学习
    //js验证数字输入,以及保留俩位
  • 原文地址:https://www.cnblogs.com/jiebian/p/2785492.html
Copyright © 2011-2022 走看看