我有C#控制台应用程序,它仅工作两次。 当我使用单例模式调用我的类和方法时,我不知道问题出在我的代码中什么地方,而且当我使用外部url服务读取数据时,我也使用两个One xml文件来写入和读取数据,当我只有两个c#类时:
Programe.cs,Tanks_Controller.cs
这是我使用的代码:
using System;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Timers;
using System.Xml;
using log4net;
using NHibernate;
using Configuration = NHibernate.Cfg.Configuration;
namespace Tank_Gauging.Franklin
{
public class TankController
{
private static readonly ILog Log =
LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private static Timer aTimer;
private static int tankCode;
private static string WaterHeight { get; set; }
private static string WaterVolume { get; set; }
private static double ProductHeight { get; set; }
private static double ProductVolume { get; set; }
private static double Temperature { get; set; }
private static double ProductTcVolume { get; set; }
private static double ProductUllage { get; set; }
static Timer _timer; // From System.Timers
private NameValueCollection customConfig =
ConfigurationManager.GetSection("CustomConfiguration") as NameValueCollection;
private static string _urlService = null;
private Timer timer1;
private static TankController instance;
static int alarmCounter = 1;
static bool exitFlag = false;
private TankController()
{
// init();
}
public static TankController Instance
{
get
{
if (instance == null)
{
instance = new TankController();
}
<p> return</p>instance;
}
}
public void init()
{
_urlService = customConfig["DemoUrlService"];
// onStart();
Timer aTimer = new Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = Convert.ToDouble(getIntervialTimeIntervalTime());
aTimer.AutoReset = true;
aTimer.Enabled = true;
}
// Specify what you want to happen when the Elapsed event is raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
getTankStatus();
Log.Warn($@"Timer is set to : {e.SignalTime}");
}
private static bool getTankStatus()
{
ServicePointManager.Expect100Continue = false;
string responseStr = null;
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(_urlService);
byte[] bytes;
bytes = Encoding.ASCII.GetBytes(
"<TSA_REQUEST_LIST PASSWORD=''><TSA_REQUEST COMMAND=''/></TSA_REQUEST_LIST>"); // header request .
request.ContentType = "text/xml; encoding=utf-8";
request.ContentLength = bytes.Length;
request.Method = "POST";
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
HttpWebResponse response;
response = (HttpWebResponse) request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = response.GetResponseStream();
responseStr = new StreamReader(responseStream).ReadToEnd();
}
File.WriteAllText(@"C:\user\tanks_data.xml", string.Empty); // Remove everything in file .
File.WriteAllText(@"C:\user\tanks_data.xml", responseStr); // add new data response from service .
XmlTextReader reader = new XmlTextReader(@"C:\user\tanks_data.xml"); // write data from xml file .
var counter = 1;
var sessionFactory = new Configuration().BuildSessionFactory();
var mySession = sessionFactory.OpenSession();
var n = 1;
while (reader.Read())
{
if (reader.Name == "TANK_STATUS")
{
Console.WriteLine($"------------------------------ {counter} ----------------------------------");
while (reader.MoveToNextAttribute())
{
/*reader.Name == "TANK_ID" */
if (reader.Name == "TANK_ID")
{
tankCode = Int32.Parse(reader.Value);
Log.Debug("TANK_ID = " + tankCode);
}
if (reader.Name == "WATER_LEVEL")
{
WaterHeight = reader.Value;
Convert.ToDouble(reader.Value);
Log.Debug("WATER_LEVEL = " + WaterHeight);
}
if (reader.Name == "WATER_VOLUME")
{
WaterVolume = reader.Value; //Convert.ToDouble();
Log.Debug("WATER_VOLUME = " + WaterVolume);
}
if (reader.Name == "PRODUCT_LEVEL")
{
ProductHeight = Convert.ToDouble(reader.Value);
Log.Debug("WATER_VOLUME = " + WaterVolume);
}
if (reader.Name == "TEMPERATURE")
{
Temperature = Convert.ToDouble(reader.Value);
Log.Debug("TEMPERATURE = " + Temperature);
}
if (reader.Name == "ULLAGE_VOLUME")
{
ProductUllage = Convert.ToDouble(reader.Value);
Log.Debug("ULLAGE_VOLUME = " + ProductUllage);
}
if (reader.Name == "GROSS_PRODUCT_VOLUME")
{
ProductTcVolume = Convert.ToDouble(reader.Value);
Log.Debug("GROSS_PRODUCT_VOLUME = " + ProductTcVolume);
}
if (reader.Name == "NET_PRODUCT_VOLUME")
{
ProductVolume = Convert.ToDouble(reader.Value);
Log.Debug("NET_PRODUCT_VOLUME = " + ProductVolume);
}
}
counter++;
n++;
try
{
mySession.BeginTransaction();
mySession.CreateSQLQuery(
@" INSERT INTO GA_TANK_TRANSACTIONS (
BRANCH_ID,
WATER_VOLUME,
ULLAGE_VOLUME,
LIQUID_VOLUME,
YEAR_CODE,
SEQ,
TRANS_DATE,
TANK_CODE,
WATER_HEIGHT,
LIQUID_TEMPERATURE,
TYPE_CODE,
USER_ID,
LIQUID_HEIGHT,
LIQUID_MASS,
LIQUID_DENSITY,
LIQUID_TC_VOLUME,
TRANS_HOUR,
TRANS_MINUTE)
VALUES (:branchId,
(:waterVolume ),
:ullageVolume,
(:liquidVolume ),
:yearCode,
(SELECT NVL(MAX(SEQ), 0) + 1
FROM GA_TANK_TRANSACTIONS
WHERE BRANCH_ID = :branchId
AND YEAR_CODE = :yearCode
AND TYPE_CODE = 1066),
TRUNC(SYSDATE),
:tankCode,
(:waterHeight*1000),
:liquidTemperature,
1066,
NULL,
(:liquidHeight *1000),
:liquidMass,
:liquidDensity,
:liquidTCVolume,
TO_NUMBER(TO_CHAR(SYSDATE, 'hh24')),
TO_NUMBER(TO_CHAR(SYSDATE, 'mi')))")
.SetParameter("branchId", "1")
.SetParameter("tankCode", tankCode + "")
.SetParameter("yearCode", DateTime.Today.Year)
.SetParameter("waterHeight", WaterHeight)
.SetParameter("liquidMass", getLiquidMass(tankCode, ProductVolume))
.SetParameter("liquidHeight", ProductHeight)
.SetParameter("liquidTemperature", Temperature)
.SetParameter("liquidDensity", 0)
.SetParameter("liquidTCVolume", Temperature)
.SetParameter("waterVolume", WaterVolume)
.SetParameter("ullageVolume", ProductUllage)
.SetParameter("liquidVolume", ProductVolume)
.ExecuteUpdate();
mySession.Transaction.Commit();
mySession.Clear();
}
catch (Exception e)
{
Log.Debug(e);
throw;
}
}
}
Log.Debug("Successfully Commit Script .");
return true;
}
private string getIntervialTimeIntervalTime()
{
try
{
var sessionFactory = new Configuration().BuildSessionFactory();
var mySession = sessionFactory.OpenSession();
var probeTimeInterval =
mySession.CreateSQLQuery(
"SELECT NVL(GE_PKG.GET_GE_VALUE(1, 47, 313), 1) AS \"probeTimeInterval\" FROM dual")
.AddScalar("probeTimeInterval", NHibernateUtil.Int64)
.UniqueResult<long?>() * 100;
Log.Debug($"Success Get Prop Interval Time {probeTimeInterval} sec ");
return probeTimeInterval + "";
}
catch (Exception e)
{
Log.Debug(e);
throw;
}
<p> return</p>null;
}
private static double getLiquidMass(int tankCode, double liquidVolume)
{
try
{
var sessionFactory = new Configuration().BuildSessionFactory();
var mySession = sessionFactory.OpenSession();
var liquidMass =
mySession.CreateSQLQuery(
$@"SELECT NVL(FUEL_TYPE_DENSITY, 0)* NVL(:liquidVolume, 0) AS liquidMass
FROM GA_FUEL_TYPE
WHERE EXISTS(
SELECT 1
FROM GA_FUEL_TANKS
WHERE GA_FUEL_TANKS.FUEL_CODE = GA_FUEL_TYPE.TYPE_CODE
AND TANK_CODE = :tankCode
AND BRANCH_ID = :branchId)"
)
.AddScalar("liquidMass", NHibernateUtil.Double)
.SetParameter("tankCode", tankCode)
.SetParameter("branchId", "1")
.SetParameter("liquidVolume", liquidVolume)
.UniqueResult<double>();
Log.Debug($"Success Get Liquid Mass {liquidMass} ");
<p> return</p> liquidMass;
}
catch (Exception e)
{
Log.Debug(e);
throw;
}
<p> return</p> 0;
}
}
}
我的计时器代码:
_urlService = customConfig["DemoUrlService"]; // onStart(); Timer aTimer = new Timer(); aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); aTimer.Interval = Convert.ToDouble(getIntervialTimeIntervalTime()); aTimer.AutoReset = true; aTimer.Enabled = true;
答案 0 :(得分:1)
您正在设置Timer的LOCAL变量,而应使用GLOBAL实例,如下所示:
public void init()
{
_urlService = customConfig["DemoUrlService"];
// onStart();
aTimer = new Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = Convert.ToDouble(getIntervialTimeIntervalTime());
aTimer.AutoReset = true;
aTimer.Enabled = true;
}
当您使用本地Timer时,问题在于方法退出时,Timer会被垃圾回收,并且当然会停止工作。