C# code to track the live entries in a file

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
namespace Live_Tracker
{
    public partial class Form1 : Form
    {
        Thread t;
        public static String fileLocation;
        public static String stringToSearch;
        public Form1()
        {
            InitializeComponent();
        }
        public class ProgExec
        {
            public ProgExec()
            {
                //Add Constructor Logic Here
            }
            public void execute()
            {
                long currentPosition = 0;
                FileStream FS = new FileStream(fileLocation, FileMode.Open,
                            FileAccess.Read, FileShare.ReadWrite);
                StreamReader SR = new StreamReader(FS);
                SR.BaseStream.Seek(currentPosition, SeekOrigin.End);
                currentPosition = SR.BaseStream.Position;
                SR.Close();
                FS.Close();
                for (int i = 0; i == 0; )
                {
                    FS = new FileStream(fileLocation, FileMode.Open,
                            FileAccess.Read, FileShare.ReadWrite);
                    SR = new StreamReader(FS);
                    SR.BaseStream.Seek(currentPosition, SeekOrigin.Current);
                    string text = "abc";
                    while (text != null)
                    {
                        text = SR.ReadLine();
                        if (text != null && text.Contains(stringToSearch))
                        {
                           
                                MessageBox.Show(text.ToString(), "Message", MessageBoxButtons.OK,
                              MessageBoxIcon.Information);
                                break;
                        }
                    }
                    currentPosition = SR.BaseStream.Position;
                    SR.Close();
                    FS.Close();
                }
            }
        }
        private void track_Click(object sender, EventArgs e)
        {
            track.Enabled = false;
            fileLocation = fileLoc.Text.ToString();
            stringToSearch = stringToTrack.Text.ToString();
            progressBar1.Style = ProgressBarStyle.Marquee;
            progressBar1.MarqueeAnimationSpeed = 20;
            ProgExec pro = new ProgExec();
            ThreadStart ts = new ThreadStart(pro.execute);
            t = new Thread(ts);
            t.Start();
        }
        private void stop_Click(object sender, EventArgs e)
        {
            if (t == null)
            {
                MessageBox.Show("Click Track First", "Exception", MessageBoxButtons.OK,
                          MessageBoxIcon.Error);
            }
            else
            {
                progressBar1.MarqueeAnimationSpeed = 0;
                t.Abort();
                t = null;
                track.Enabled = true;
            }
        }
        private void exit_Click(object sender, EventArgs e)
        {
            Dispose();
        }
    }
}
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s