Using Queue

        protected monitor.monitorSoapClient client;
        protected Queue<float> CPU30;
        protected Queue<float> MEM30;
        protected Queue<String> TIME30;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["time"] == null)
            {
                TIME30 = new Queue<String>();
                CPU30 = new Queue<float>();
                MEM30 = new Queue<float>();
                Session.Add("time", TIME30);
                Session.Add("cpu", CPU30);
                Session.Add("mem", MEM30);
            }
            else
            {
                TIME30 = (Queue<String>)Session["time"];
                CPU30 = (Queue<float>)Session["cpu"];
                MEM30 = (Queue<float>)Session["mem"];
            }

            client = new monitor.monitorSoapClient();
            
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            float cpu = client.getHostCpuUsage();
            float mem = client.getHostMemUsage();
            String time = DateTime.Now.ToUniversalTime().ToString();

            Label1.Text = time + "<br/>CPU Usage: " + cpu + "% <BR/>"
                        + "MEM Usage: " + mem + "MB<br/>";

            CPU30.Enqueue(cpu);
            MEM30.Enqueue(mem);
            TIME30.Enqueue(time);

            if (TIME30.Count > 10)
                TIME30.Dequeue();
            if (MEM30.Count > 10)
                MEM30.Dequeue();
            if (CPU30.Count > 10)
                CPU30.Dequeue();


            Label1.Text += "first cpu:" + CPU30.Peek().ToString() + ".. first mem:" + 
MEM30.Peek().ToString() + ".. time:" + TIME30.Peek() + ".. count:" + TIME30.Count;
        }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
using clock_type = std::chrono::system_clock; struct message { clock_type::time_point when; std::function<void()> callback; std::string param; }; class message_loop { public: message_loop(): _stop(false) { // } message_loop(const message_loop&) = delete; message_loop& operator=(const message_loop&) = delete; void run() { while (!_stop) { auto msg = wait_one(); msg.callback(); } } void quit() { post({clock_type::now(), this{ _stop = true; } }); } void post(std::function<void()> callable) { post({clock_type::now(), std::move(callable)}); } void post(std::function<void()> callable, std::chrono::milliseconds delay) { post({clock_type::now() + delay, std::move(callable)}); } private: struct msg_prio_comp { inline bool operator() (const message& a, const message& b) { return a.when > b.when; } }; using queue_type = std::priority_queue<message, std::vector<message>, msg_prio_comp>; std::mutex _mtx; std::condition_variable _cv; queue_type _msgs; bool _stop; void post(message msg) { auto lck = acquire_lock(); _msgs.emplace(std::move(msg)); _cv.notify_one(); } std::unique_lockstd::mutex acquire_lock() { return std::unique_lockstd::mutex(_mtx); } bool idle() const { return _msgs.empty(); } const message& top() const { return _msgs.top(); } message pop() { auto msg = top(); _msgs.pop(); return msg; } message wait_one() { while (true) { auto lck = acquire_lock(); if (idle()) _cv.wait(lck); else if (top().when <= clock_type::now()) return pop(); else { _cv.wait_until(lck, top().when); // 可能是新消息到达,再循环一次看看 } } } }; int main(int argc, char *argv[]) { using namespace std; using namespace std::chrono; message_loop *pLoop = new message_loop; thread th(pLoop{ pLoop->run(); }); cout << "POST 1"<<endl;; pLoop->post({ cout << "1"<<endl; }); cout << "POST 2"<<endl;; pLoop->post({ cout << "2"<<endl; }, milliseconds(500)); cout << "POST 3"<<endl;; pLoop->post({ cout << "3"<<endl; }); cout << "POST 4"<<endl;; pLoop->post({ cout << "4"<<endl; }, milliseconds(1000)); this_thread::sleep_for(milliseconds(1500)); // pLoop->quit(); cout << "Quit"<<endl; th.join(); cout << "here"<<endl; } 请优化一下,可以传参
最新发布
05-30

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值