⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 servicetemperature.cs

📁 这是一个Multi-agent典型的C++的代码
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using CommunicationClasses;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Timers;

namespace WindowsServiceTemperatureAgent
{
    public partial class ServiceTemperature : ServiceBase
    {
        private ClassDateTime _dateTime;
        private ClassTemperature _temperature;
        private DataTable _globalDB;
        private Random _autoRand;
        private System.Timers.Timer _timerPublishTemperature;
        private System.Timers.Timer _timerWriteToXml;
        
            public ServiceTemperature()
        {
            InitializeComponent();
            //server for the ClassTemperature
            TcpChannel serverChannel = new TcpChannel(8080);
            
            // Register the server channel.
            ChannelServices.RegisterChannel(serverChannel);            
            
            // Expose an object for remote calls.
            RemotingConfiguration.RegisterWellKnownServiceType(
                typeof(ClassTemperature), "RemoteObject2.rem",
                WellKnownObjectMode.Singleton);
            _temperature = (ClassTemperature)Activator.GetObject(
                        typeof(ClassTemperature),
                            "tcp://localhost:8080/RemoteObject2.rem");
            
            //client for the ClassDateTime
            _dateTime = (ClassDateTime)Activator.GetObject(
            typeof(ClassDateTime),
                "tcp://localhost:9090/RemoteObject.rem");
            
            //dbCreation
            _globalDB = new DataTable();
            _globalDB.TableName = "RecordTable";
            _globalDB.Columns.Add("Record DateTime", typeof(DateTime));
            _globalDB.Columns.Add("Temperature", typeof(Double));
            //random number generator
            _autoRand = new Random();
            
        }

        protected override void OnStart(string[] args)
        {            
            _timerPublishTemperature = new System.Timers.Timer();
            _timerWriteToXml = new System.Timers.Timer();
            _timerPublishTemperature.Interval = 2000;
            _timerWriteToXml.Interval = 10000;
            _timerPublishTemperature.AutoReset = true;
            _timerWriteToXml.AutoReset = true;
            _timerPublishTemperature.Elapsed += new ElapsedEventHandler(PublishTemperature);
            _timerWriteToXml.Elapsed += new ElapsedEventHandler(WriteToXml);
            _timerPublishTemperature.Start();
            _timerWriteToXml.Start();
        }

        protected override void OnStop()
        {
            _timerWriteToXml.Stop();
            _timerPublishTemperature.Stop();
            
        }

        private void PublishTemperature(object sender, ElapsedEventArgs e)
        {
            int tmp;
            tmp = Convert.ToInt32(_autoRand.NextDouble() * 10);
            _temperature.setTemperature(tmp);
        }

        private void WriteToXml(object sender, ElapsedEventArgs e)
        {
            int tmp;
            tmp = Convert.ToInt32(_autoRand.NextDouble() * 10);
            //add to the db
            DataRow rowToAdd = _globalDB.NewRow();
            rowToAdd[0] = _dateTime.getDateTime();
            rowToAdd[1] = tmp;
            _globalDB.Rows.Add(rowToAdd);
            _globalDB.AcceptChanges();
            _globalDB.WriteXml("C:\\globalDB.xml");
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -