Click here to Skip to main content
15,917,968 members
Home / Discussions / C#
   

C#

 
GeneralRe: Need help with error checking for Dns.GetHostEntry Pin
turbosupramk311-Aug-11 4:52
turbosupramk311-Aug-11 4:52 
GeneralRe: Need help with error checking for Dns.GetHostEntry Pin
BobJanova11-Aug-11 5:39
BobJanova11-Aug-11 5:39 
GeneralRe: Need help with error checking for Dns.GetHostEntry Pin
Shameel11-Aug-11 5:48
professionalShameel11-Aug-11 5:48 
QuestionWhat is the best method to figure out elapsed time in a BackgroundWorker thread? Pin
bbranded10-Aug-11 5:05
bbranded10-Aug-11 5:05 
AnswerRe: What is the best method to figure out elapsed time in a BackgroundWorker thread? Pin
Ian Shlasko10-Aug-11 5:15
Ian Shlasko10-Aug-11 5:15 
GeneralRe: What is the best method to figure out elapsed time in a BackgroundWorker thread? [modified] Pin
bbranded10-Aug-11 5:27
bbranded10-Aug-11 5:27 
GeneralRe: What is the best method to figure out elapsed time in a BackgroundWorker thread? Pin
Ian Shlasko10-Aug-11 5:43
Ian Shlasko10-Aug-11 5:43 
GeneralRe: What is the best method to figure out elapsed time in a BackgroundWorker thread? [modified] Pin
bbranded11-Aug-11 7:17
bbranded11-Aug-11 7:17 
Thanks very much!

This is very far from perfect, but will give others a good starting point to address this issue.
I'll open source the final project over at mbrownnyc.wordpress.com shortly.


C#
        private void _bw_DoWork(object sender, DoWorkEventArgs e)
        {
            //break up and assign the arguments passed by the methods:
            string[] CommandArguments = e.Argument.ToString().Split('#');

            string TargetDirectory = CommandArguments[0];
            bool RecursivePlz = Boolean.Parse(CommandArguments[1]);
            string LogFilename = CommandArguments[2];

            ReadFiles(TargetDirectory, RecursivePlz, LogFilename);

        }

        public void ReadFiles(string TargetDirectory, bool RecursivePlz, string LogFilename)
        {

            
            //take readsize and divide it by 1048576 (1MB)...
            // calculate how many operations it takes to complete the read of 1MB
            int readsize = 65536;
            double ReadOpsPerMB = readsize / 1048576;
            //see "use the ReadOpsPerMB" below

            TargetDirectory = TargetDirectory.ToLower();

            //exclude dfsrprivate folders
            if (!TargetDirectory.Contains("dfsrprivate"))
            {

                //find the directory
                if (Directory.Exists(TargetDirectory))
                {
                    //find the files in this directory
                    string[] FileList = Directory.GetFiles(TargetDirectory);

                    int BytesReadInRound = 0;

                    //check files in this directory
                    foreach (string ffile in FileList)
                    {

                        FileInfo finfo = new FileInfo(ffile);
                        if (finfo.Exists)
                        {
                            //get the full file size
                            int SizeOfFile = Int32.Parse(finfo.Length.ToString());

                            //adjust filestream buffer to be larger than the readsize, this will not force a read, and allow us to count the enumerations
                            FileStream readStream = new FileStream(ffile, FileMode.Open, FileAccess.Read, FileShare.None, readsize + 1);
                            BinaryReader binReader = new BinaryReader(readStream);

                            string Status = "";

                            bool Readthefile = true;

                            try
                            {
                                if (binReader.PeekChar() <= -1)  //is file empty... this peekchar() sucks 
                                {
                                    Status = "EMPTY;" + ffile;
                                    _bw.ReportProgress(0, Status);
                                    Readthefile = false;
                                }
                            }
                            catch (Exception e) //the file isn't empty, just 1 byte
                            {
                                Readthefile = true;
                                _bw.ReportProgress(0, ffile + " caused an exception, but was added to the total count: " + e.Message);
                            }


                            if (Readthefile)
                            {
                                byte[] valuebytes;

                                bool donereading = false;


                                _bw.ReportProgress(0, "reading " + Decimal.Divide(SizeOfFile, 1024).ToString() + " file: " + ffile);


                                while (donereading == false)
                                {
                                    //in here we measure how long it takes to read the size of /readsize/ from the given file
                                    DateTime StartOperationTime = DateTime.Now;

                                    valuebytes = binReader.ReadBytes(readsize);
                                    BytesReadInRound = BytesReadInRound + valuebytes.Length;
                                    donereading = (valuebytes.Length < readsize);

                                    if (BytesReadInRound >= 1048576)
                                    {
                                        _bw.ReportProgress(2, "read|" + BytesReadInRound + "|" + LogFilename + "|" + DateTime.Now);
                                        BytesReadInRound = 0;
                                    }

                                    // Create instance of array type instance then add values to it
                                    DateTime EndOperationTime = DateTime.Now;
                                    TimeSpan OperationTime = EndOperationTime.Subtract(StartOperationTime);


                                }
                            }

                            readStream.Close();
                            readStream.Dispose();
                            binReader.Close();
                        }



                    }


                    //find all the sub-directories in the directory
                    string[] SubDirectorylist = Directory.GetDirectories(TargetDirectory);

                    //reverse the entire list
                    Array.Reverse(SubDirectorylist);


                    //if user has requested recursive action, go ahead...
                    if (RecursivePlz == true)
                    {
                        if (Directory.Exists(TargetDirectory))
                        {
                            //trigger ReadFiles() on these as well
                            foreach (string ddir in SubDirectorylist)
                            {
                                ReadFiles(ddir, RecursivePlz, LogFilename);
                            }
                        }
                    }
                }
            }
        }

        struct SnapshotData
        {
            public string What;
            public int HowManyBytes;
            public string LogFilename;
            public DateTime When;
        }

        List<SnapshotData> SnapShots = new List<SnapshotData>();
        
        private void _bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

            string UserState = e.UserState.ToString();


            if (UserState.Contains("|")) //sent down every 1MB of read
            {
                
                string[] UserStateCommands = UserState.Split('|');

                SnapshotData Snapshot = new SnapshotData();

                Snapshot.What = UserStateCommands[0];
                Snapshot.HowManyBytes = Convert.ToInt32(UserStateCommands[1]);
                Snapshot.LogFilename = UserStateCommands[2];
                Snapshot.When = Convert.ToDateTime(UserStateCommands[3]);
                SnapShots.Add(Snapshot);

                

                //check how many seconds between snapshot.when and SnapShots[0].when
                TimeSpan Snapshotdifference = Snapshot.When.Subtract(SnapShots[0].When);

                textBox2.Text = Snapshotdifference.ToString();

                if (Snapshotdifference.TotalSeconds >= 10) //sent down every sixty
                {
                    // add up all the bytes read, which are contained in all the members of Snapshots
                    decimal ReadAfterLastFlushToLog = new decimal();
                    foreach (SnapshotData member in SnapShots)
                    {
                        ReadAfterLastFlushToLog = (ReadAfterLastFlushToLog + member.HowManyBytes);
                    }

                    ReadAfterLastFlushToLog = Decimal.Divide(Decimal.Divide(Decimal.Divide(ReadAfterLastFlushToLog,60),1024),1024);

                    //write to log
                    StreamWriter LogFileWriter = new StreamWriter(Snapshot.LogFilename, true);
                    textBox5.Text = DateTime.Now.ToString() + ";" + ReadAfterLastFlushToLog.ToString();
                    LogFileWriter.WriteLine(DateTime.Now.ToString() + ";" + ReadAfterLastFlushToLog.ToString());
                    LogFileWriter.Close();

                    //clear all previous snapshots
                    textBox2.Text = "";
                    SnapShots.Clear();
                }
}

modified on Monday, August 22, 2011 1:09 PM

GeneralRe: What is the best method to figure out elapsed time in a BackgroundWorker thread? Pin
Shameel10-Aug-11 9:32
professionalShameel10-Aug-11 9:32 
AnswerRe: What is the best method to figure out elapsed time in a BackgroundWorker thread? Pin
PIEBALDconsult10-Aug-11 15:34
mvePIEBALDconsult10-Aug-11 15:34 
GeneralRe: What is the best method to figure out elapsed time in a BackgroundWorker thread? Pin
bbranded10-Aug-11 15:36
bbranded10-Aug-11 15:36 
GeneralRe: What is the best method to figure out elapsed time in a BackgroundWorker thread? Pin
PIEBALDconsult10-Aug-11 17:19
mvePIEBALDconsult10-Aug-11 17:19 
GeneralRe: What is the best method to figure out elapsed time in a BackgroundWorker thread? Pin
bbranded11-Aug-11 1:27
bbranded11-Aug-11 1:27 
QuestionFiltering Problem in Sql Syntax using C#.net Pin
nassimnastaran10-Aug-11 0:17
nassimnastaran10-Aug-11 0:17 
AnswerRe: Filtering Problem in Sql Syntax using C#.net Pin
Shameel10-Aug-11 0:38
professionalShameel10-Aug-11 0:38 
AnswerRe: Filtering Problem in Sql Syntax using C#.net Pin
MicroVirus10-Aug-11 3:30
MicroVirus10-Aug-11 3:30 
GeneralRe: Filtering Problem in Sql Syntax using C#.net Pin
GenJerDan10-Aug-11 3:54
GenJerDan10-Aug-11 3:54 
GeneralRe: Filtering Problem in Sql Syntax using C#.net Pin
BobJanova10-Aug-11 3:58
BobJanova10-Aug-11 3:58 
GeneralRe: Filtering Problem in Sql Syntax using C#.net Pin
GenJerDan10-Aug-11 4:01
GenJerDan10-Aug-11 4:01 
GeneralRe: Filtering Problem in Sql Syntax using C#.net Pin
MicroVirus10-Aug-11 4:05
MicroVirus10-Aug-11 4:05 
AnswerRe: Filtering Problem in Sql Syntax using C#.net Pin
BobJanova10-Aug-11 3:57
BobJanova10-Aug-11 3:57 
GeneralRe: Filtering Problem in Sql Syntax using C#.net Pin
nassimnastaran10-Aug-11 4:23
nassimnastaran10-Aug-11 4:23 
GeneralRe: Filtering Problem in Sql Syntax using C#.net Pin
BobJanova10-Aug-11 8:25
BobJanova10-Aug-11 8:25 
AnswerRe: Filtering Problem in Sql Syntax using C#.net Pin
Ian Shlasko10-Aug-11 4:01
Ian Shlasko10-Aug-11 4:01 
AnswerRe: Filtering Problem in Sql Syntax using C#.net Pin
loyal ginger10-Aug-11 4:13
loyal ginger10-Aug-11 4:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.