Hot File

Status Reporter using text file and fileSystemWatcher

View: 552    Dowload: 0   Comment: 0   Post by: naruto  
Author: none   Category: Autodesk AutoCAD   Fields: Computers - Technology

0 point/2 review File has been tested

Download   status-reporter-using-text.rar (93.97 KB)

You need to Sign In to download the file status-reporter-using-text.rar
If you do not have an account then Sign up for free here

Download error   Report copyright violation

This program was written to allow easy progress or status reporting of another programs activities. It operates on the premis that the program being monitored will write numbered status messages to a text file. It uses a FileSystemWatcher to detect changes in the file, and reads the messages and displays them in a dialog. I first tried the program where the main program would just write one message at a time to the message text file. I quickly found that was fragile as really fast operations could not be written fast enough to the file. So I tried having the main program keep a list of the last 20 messages, and try writing to file each time a new message was generated. If a few writes failed, it is no problem as the next ones will have the messages that got missed. The watcher program reads the file each time it changes, and filters out past messages by their number, then displays the new ones. The reporting dialog closes after a time delay, or if the message text file is deleted. Those details are what makes this tool a bit more valuable. It allows you to take a program written in any other language, maybe one that does not easily support multi-threading, and dependably see a progress report of some activity.

Introduction

Background

I wrote this to help with programs written for Autodesk's AutoCad program. Tools that displayed progress bars well on windows XP, acted differently on Windows 7, so I had to do something to deal with the UI "blocking" going on for long processes. The AutoCad tools are written in AutoLisp, and I specifically wanted an external process doing the reporting, so no chance of blocking whatsoever.

Using the code

To use the status watcher, you will run the StatusByFileWatch.exe as an external process. It takes two command line arguments, in this order:

  • the full text file filename
  • the delay in microseconds, for the dilaog to close on its own when no activity happens

The Demo program is written in C# but the principles here apply to any language. In any program that needs its messages to be watched, set up variables for:

  • full text filename
  • StatusByFileWatch.exe full filename
  • message buffer list
  • buffer length

      string _exeLoc = @"C:+StorageProgrammingDotNetStatusByFileWatchinReleaseStatusByFileWatch.exe"; //modify as needed
  string _txtFilename = Environment.GetEnvironmentVariable("TEMP") + @"Logfile for Demo.txt";
  List _msgs = new List();
  int _bufflen = 20;

Then wherever you have a process that want to have messaged watched, you will:

  • delete the textfile
  • clear the message list variable
  • write a new message to the text file using the writeStatusMsg function.
  • start the StatusByFileWatch.exe with desired arguments
  • use WriteStatusMsg to add messages as some process proceeds
  • delete the text file when done displaying the messages (or do nothing and let the dialog close on its own based on the close delay argument

  private void button1_Click(object sender, EventArgs e) {
   try {
    File.Delete(_txtFilename);
   }
   catch { }
   _msgs = new List();
   writeStatusMsg("Starting Loop, be patient....");
   Thread.Sleep(2000);
   //run the watcher exe
   try {
    System.Diagnostics.Process.Start(_exeLoc, """ + _txtFilename + "" 7000");
   }
   catch { }
   //run a loop that does enough work to block the UI from updating
   for (int i = 1; i < 30; i++) {
    //write message to the text file
    writeStatusMsg("Doing Loop " + i.ToString() + " be patient....");
    Thread.Sleep(500);
   }
   //delete message file to close watcher dialog
   try {
    File.Delete(_txtFilename);
   }
   catch { }
  }

Notice that this automatically trims off the first message if the message list length is exceeded.


private void writeStatusMsg (string msg) {
   //add to message list, but do not exceed buffer
   if (_msgs.Count >= _bufflen)
    _msgs.RemoveAt(0);
   //add message
   _msgs.Add(_msgIndex.ToString() + "," + msg);
   _msgIndex++;
   //try to write to file
   try {
    TextToFile(_txtFilename, _msgs.ToArray());
   }
   catch { }
  }

The TextToFile function is in the demo code.

Every language will have tools for starting an external exe, and writing to and deleting text files, so this pattern can be adopted to any language.

Points of Interest

One very important point is that the watcher must read the text file in a way that does not prevent the program being watched, from writing to the file. This function does that nicely:


public static bool TextFiletoList(string fName, out List fileLines) {
   fileLines = new List();
   if (File.Exists(fName)) {
    FileStream fs = new FileStream(fName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    using (StreamReader sReader = new StreamReader(fs)) {
     string text = sReader.ReadLine();
     while (text != null) {
      fileLines.Add(text);
      text = sReader.ReadLine();
     }

    }
   }
   if (fileLines.Count > 0) { return true; }
   else { return false; }
  }

the line that makes the FileStream is critical.

There are many improvements and customizations that could be done, such as a progress bar in the message dialog. If you established certain keywords in the messages, you could do all kinds of things besides just show messages. Take that to the extreme and you are essentially serializing what you want the state of the dialog to be, and the dialog updates each time the state is posted.

The tricky part about that is mixing it with other languages that may not have deserialization functions waiting to go. I had to write my own for AutoLisp, because the default way of talking between AutoCad and .net (called a resultbuffer...) does unkind things to your data as it passes back and forth.

This prog does not get complicated though, just uses simple numbered messages in the text file.

History

1st Release  10-18-12.

Status Reporter using text file and fileSystemWatcher

Status Reporter using text file and fileSystemWatcher Posted on 21-03-2014  This program was written to allow easy progress or status reporting of another programs activities. It operates on the premis that the program being monitored will write numbered status messages to a text file. It uses a FileSystemWatcher to detect changes in the file, and reads the messages and displays them in a dialog. I first tried the program where the main program would just write one message at a time to the message text file. I quickly found that was fragile as really fast operations could not be written fast enough to the file. So I tried having the main program keep a list of the last 20 messages, and try writing to file each time a new message was generated. If a few writes failed, it is no problem as the next ones will have the messages that got missed. The watcher program reads the file each time it changes, and filters out past messages by their number, then displays the new ones. The reporting dialog closes after a time delay, or if the message text file is deleted. Those details are what makes this tool a bit more valuable. It allows you to take a program written in any other language, maybe one that does not easily support multi-threading, and dependably see a progress report of some activity. 5/10 552

Comment:

To comment you must be logged in members.

Files with category

  • Ground Modelling information using AutoCAD

    View: 222    Download: 1   Comment: 0   Author: none  

    Ground Modelling information using AutoCAD

    Category: Autodesk AutoCAD
    Fields: Other

    2.25/2 review
    This tutorial describes how to create a 3D surface model from contour information using AutoCAD and Key TERRA-FIRMA. The requirement to visualise terrain in 3D, whether existing or proposed is very common in the environmental design professions.

  • 3D Tree Exercise in AutoCAD

    View: 254    Download: 1   Comment: 0   Author: none  

    3D Tree Exercise in AutoCAD

    Category: Autodesk AutoCAD
    Fields: Other

    2.25/2 review
    It is quite difficult to construct convincing looking 3D trees in AutoCAD, however, with a few simple 3D commands at your disposal you should be able to create something which is identifiable as a tree and perhaps hint at a species.

  • Create Basic 3D and Surface Modelling

    View: 257    Download: 1   Comment: 0   Author: none  

    Create Basic 3D and Surface Modelling

    Category: Autodesk AutoCAD
    Fields: Other

    2.25/2 review
    Although AutoCAD has a number of commands for creating special 3D objects, a lot can be achieved by changing the properties of basic 2D objects like polylines. Most 2D objects can be given a thickness using the thickness option in the Properties...

  • Document Model AutoCAD Meshes to Bryce

    View: 261    Download: 0   Comment: 0   Author: none  

    Document Model AutoCAD Meshes to Bryce

    Category: Autodesk AutoCAD
    Fields: Other

    1.5/3 review
    Although you can create very realistic terrain objects in Bryce, it is often difficult to get them to look just like a real place. However, you can use Key Terra-Firma with AutoCAD to create an accurate terrain model from contour or other height...

  • Using AutoCAD to Bryce

    View: 322    Download: 1   Comment: 0   Author: none  

    Using AutoCAD to Bryce

    Category: Autodesk AutoCAD
    Fields: Other

    1.3333333333333/3 review
    This tutorial explains how to take a model built in AutoCAD into Bryce, how to assign materials to the various components of your model,how to use image textures, how to create a simple setting and how to render the resulting scene. Although the key...

  • DXF Import .NET: Read and View AutoCAD Format Files

    View: 2366    Download: 4   Comment: 0   Author: none  

    DXF Import .NET: Read and View AutoCAD Format Files

    Category: Autodesk AutoCAD
    Fields: none

    0/5 review

  • Writing AutoCad DXF files

    View: 1991    Download: 2   Comment: 0   Author: none  

    Writing AutoCad DXF files

    Category: Autodesk AutoCAD
    Fields: Art - Photography

    0/3 review
    This article demonstrate how to write an ASCII DXF files. A DXF file is a file composed of sections and associated values. They are read by AutoCad and then converted to a drawing.

  • Catalogo ETABS v9.6 Documentation

    View: 857    Download: 1   Comment: 0   Author: none  

    Catalogo ETABS v9.6 Documentation

    Category: Autodesk AutoCAD
    Fields: Interior Design - Building

    0/2 review
    For nearly 30 years, ETABS has been recognized as the industry standard for Building Analysis and Design Software. Today, continuing in the same tradition, ETABS has evolved into a completely Integrated Building Analysis and Design Environment. The...

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd