1
好吧,所以我有兩個類,每個定時器設置在不同的時間間隔。每2秒鐘關閉一次,另一次每2分鐘關閉一次。每次代碼在定時器下運行時,我都希望它通過代碼生成的數據字符串引發事件。那麼我想創建另一個類來訂閱來自其他類的事件參數,並在事件觸發時執行類似寫入控制檯的操作。並且由於一個類只會每2分鐘觸發一次,因此該類可以將最後一個事件存儲在專用字段中,並且每次都重用該事件,直到觸發新事件以更新該值。如何用計時器提高事件?
那麼,我該如何提高事件的數據字符串?以及如何訂閱這些事件並打印到屏幕或其他東西?
這是我到目前爲止有:
public class Output
{
public static void Main()
{
//do something with raised events here
}
}
//FIRST TIMER
public partial class FormWithTimer : EventArgs
{
Timer timer = new Timer();
public FormWithTimer()
{
timer = new System.Timers.Timer(200000);
timer.Elapsed += new ElapsedEventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
timer.Interval = (200000);
timer.Enabled = true; // Enable the timer
timer.Start(); // Start the timer
}
//Runs this code every 2 minutes, for now i just have it running the method
//(CheckMail();) of the code but i can easily modify it so it runs the code directly.
void timer_Tick(object sender, EventArgs e)
{
CheckMail();
}
public static string CheckMail()
{
string result = "0";
try
{
var url = @"https://gmail.google.com/gmail/feed/atom";
var USER = "usr";
var PASS = "pss";
var encoded = TextToBase64(USER + ":" + PASS);
var myWebRequest = HttpWebRequest.Create(url);
myWebRequest.Method = "POST";
myWebRequest.ContentLength = 0;
myWebRequest.Headers.Add("Authorization", "Basic " + encoded);
var response = myWebRequest.GetResponse();
var stream = response.GetResponseStream();
XmlReader reader = XmlReader.Create(stream);
System.Text.StringBuilder gml = new System.Text.StringBuilder();
while (reader.Read())
if (reader.NodeType == XmlNodeType.Element)
if (reader.Name == "fullcount")
{
gml.Append(reader.ReadElementContentAsString()).Append(",");
}
Console.WriteLine(gml.ToString());
// I want to raise the string gml in an event
}
catch (Exception ee) { Console.WriteLine(ee.Message); }
return result;
}
public static string TextToBase64(string sAscii)
{
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] bytes = encoding.GetBytes(sAscii);
return System.Convert.ToBase64String(bytes, 0, bytes.Length);
}
}
//SECOND TIMER
public partial class FormWithTimer2 : EventArgs
{
Timer timer = new Timer();
public FormWithTimer2()
{
timer = new System.Timers.Timer(2000);
timer.Elapsed += new ElapsedEventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
timer.Interval = (2000); // Timer will tick evert 10 seconds
timer.Enabled = true; // Enable the timer
timer.Start(); // Start the timer
}
//Runs this code every 2 seconds
void timer_Tick(object sender, EventArgs e)
{
using (var file = MemoryMappedFile.OpenExisting("AIDA64_SensorValues"))
{
using (var readerz = file.CreateViewAccessor(0, 0))
{
var bytes = new byte[194];
var encoding = Encoding.ASCII;
readerz.ReadArray<byte>(0, bytes, 0, bytes.Length);
//File.WriteAllText("C:\\myFile.txt", encoding.GetString(bytes));
StringReader stringz = new StringReader(encoding.GetString(bytes));
var readerSettings = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment };
using (var reader = XmlReader.Create(stringz, readerSettings))
{
System.Text.StringBuilder aida = new System.Text.StringBuilder();
while (reader.Read())
{
using (var fragmentReader = reader.ReadSubtree())
{
if (fragmentReader.Read())
{
reader.ReadToFollowing("value");
//Console.WriteLine(reader.ReadElementContentAsString() + ",");
aida.Append(reader.ReadElementContentAsString()).Append(",");
}
}
}
Console.WriteLine(aida.ToString());
// I want to raise the string aida in an event
}
}
}
}