Created
May 10, 2015 22:55
-
-
Save cmpunches/fc091bfee9d377957f7c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
using System.IO; | |
using System.Threading; | |
namespace ADLOGGER | |
{ | |
public partial class MainWindow : Form | |
{ | |
public string LogLine | |
{ | |
set | |
{ | |
context.Send(new SendOrPostCallback((s) => txtLogBox.AppendText(value + Environment.NewLine)), null); | |
} | |
} | |
public void listenlogAsync () | |
{ | |
context.Send(new SendOrPostCallback((s) => loglisten()), null); | |
} | |
private void loglisten() | |
{ | |
// string path = "C:\\Windows\\debug\\NetSetup.LOG"; | |
string path = "C:\\Users\\Master\\text.txt"; | |
using ( | |
StreamReader reader = new StreamReader( | |
new FileStream( | |
path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | |
) | |
) | |
) | |
{ | |
//start at the end of the file | |
long lastMaxOffset = reader.BaseStream.Length; | |
LogLine = "Listening..."; | |
while (true) | |
{ | |
System.Threading.Thread.Sleep(100); | |
//if the file size has not changed, idle | |
if (reader.BaseStream.Length == lastMaxOffset) | |
continue; | |
//seek to the last max offset | |
reader.BaseStream.Seek(lastMaxOffset, SeekOrigin.Begin); | |
//read out of the file until the EOF | |
string line = ""; | |
while ((line = reader.ReadLine()) != null) | |
LogLine = line; | |
//update the last max offset | |
lastMaxOffset = reader.BaseStream.Position; | |
} | |
} | |
} | |
private SynchronizationContext context; | |
public static MainWindow _formHandle; | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
_formHandle = this; | |
//set up the SynchronizationContext | |
context = SynchronizationContext.Current; | |
if (context == null) | |
{ | |
context = new SynchronizationContext(); | |
} | |
listenlogAsync(); | |
} | |
private void Form1_Load(object sender, EventArgs e) | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment