how to merge C# code of two similar but separate programs?
ok so i have two programs. Code at end.
one is a gmail notifier that outputs the number of unread mails over serial like: <02,>
etc.
then i have a program that gets system(pc) temps and outputs them to serial also like: <30,42,50,>
etc
they both output the same data and same format(<XX,>)
at different time intervals(with different values each time). so the mail one checks every 5 minutes for mail, then outputs to serial the number of mails. but the temp program outputs the data over serial at a much fater rate at 1 sec.
ive been trying to combine the two programs into one, so that it outputs like: <02,30,42,50,>
and it sends that every second so that the temps update. so the last three sets of digts change every second, but the first one wont change for every 5 minutes but 02 is still included in the output because it(the output) needs to be the same length/organization etc every time.
so what do you guys think? what is the best way to do it?
ive tried to add all the GMAIL code except static void Main
(all Main does in the gmail program is call get mail and write the data to serial) to the TEMPS program, and then call the getmail Unreadz = CheckMail();
where the temps program is writing to serial(in the new combined code), but that stops the writing of data to serial so that it can check mail, which is kinda slow, so then it deosnt write the whole <02,30,42,50,>
until mail is done checking. my attempt:
reader.ReadToFollowing("value");
port.Write(reader.ReadElementContentAsString() + ",");
Unreadz = CheckMail();
if (Convert.ToInt32(Unreadz) < 10) port.Write("0" + Unreadz + ",");
else port.Write("" + Unreadz + ",");
thanks.
GMAIL:
namespace GmailNotifier
{
class Gmail
{
public static void Main(string[] args)
{
try
{
SerialPort port = new SerialPort( "COM2", 9600, Parity.None, 8, StopBits.One );
port.Open();
string Unreadz = "0";
while ( true )
{
Unreadz = CheckMail();
port.Write("<");
Console.WriteLine("Unread Mails: " + Unreadz);
if (Convert.ToInt32(Unreadz) < 10) port.Write("0" + Unreadz + ",");
else port.Write("" + Unreadz + ",");
port.Write(">");
System.Threading.Thread.Sleep(1000);
}
} catch ( Exception ee ) { Console.WriteLine( ee.Message ); }
}
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 );
}
public static string CheckMail()
{
string result = "0";
try
{
var url = @"hsttps://gmail.google.com/gmail/feed/atom";
var USER = <uname>;
var PASS = <password>;
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 );
while ( reader.Read())
if ( reader.NodeType == XmlNodeType.Element )
if ( reader.Name == "fullcount" )
{
result = reader.ReadElementContentAsString();
return result;
}
} catch ( Exception ee ) { Console.WriteLine( ee.Message ); }
return result;
}
}
}
TEMPS:
namespace AIDA64_Reader_SerailOut
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Reading AIDA64 Shared Memory and Sending to Serial");
try
{
SerialPort port = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);
port.Open();
while (true)
using (var file = MemoryMappedFile.OpenExisting("AIDA64_SensorValues"))
{
using (var readerz = file.CreateViewAccessor(0, 0))
{
var bytes = new byte[195];
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));
port.Write("<");
var readerSettings = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment };
using (var reader = XmlReader.Create(stringz, readerSettings))
{
while (reader.Read())
{
using (var fragmentReader = reader.ReadSubtree())
{
if (fragmentReader.Read())
{
reader.ReadToFollowing("value");
//por开发者_如何转开发t.Write("<");
port.Write(reader.ReadElementContentAsString() + ",");
//port.Write(">");
}
}
}
}
port.Write(">");
}
System.Threading.Thread.Sleep(1000);
}
}
catch (Exception ee) { Console.WriteLine(ee.Message); }
Console.WriteLine("Done");
Console.ReadLine();
}
}
}
First of all, you need to run your mail-checker on a background thread. Try the BackgroundWorker
component on for size.
Second, use a Timer
to fire off your temperature updates instead of sleeping the thread.
To combine the two programs, I'd start by changing the existing classes to raise events when something important happens. For example, temp will raise an event every 1 second, and the event args will contain the data.
The mail checker on the other hand, will only raise an event when it has finished checking for mail; perhaps only when the number of unread messages has changed from the last check.
Finally, create a new class that subscribes to the events from each of the existing programs and writes to the serial port whenever an event is fired. This class can store the last reported unread message count in a private field and reuse that every time until a new event is fired to update that value.
精彩评论