Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8858259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:47:16+00:00 2026-06-14T14:47:16+00:00

I have a simple c# console application that is scheduled after every 5 mins.

  • 0

I have a simple c# console application that is scheduled after every 5 mins. Every invocation of the program requires the output of the last run.

What I am doing right now is using a text file and store the result in it. next time when it runs it opens the text file and know the output of the previous run.

Is there any other way to do it that wont require any such text file ?
Like maintaining a session variable etc ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:47:17+00:00Added an answer on June 14, 2026 at 2:47 pm

    First use mutex lock solution to force app to run only one instance at same time.

    then create a serializable class to hold the app state and a helper class to load and save it to a file.see example:

    [XmlRoot("RegexTesterPersistantSettings")]
    [Serializable]
    public class State
    {
        public State()
        {
            this.Pattern = string.Empty;
            this.TestString = string.Empty;
            this.Options = 0;
        }
        [XmlElement("Pattern")]
        public string Pattern{get;set;}
    
        [XmlElement("TestString")]
        public string TestString{get;set;}
    
        [XmlElement("Options")]
        public int Options { get; set; }
    
        public override int GetHashCode()
        {
            return this.Options.GetHashCode() ^ this.Pattern.GetHashCode() ^ this.TestString.GetHashCode();
        }
    
        public override bool Equals(object obj)
        {
            State anotherState = obj as State;
            if (anotherState == null)
            {
                return false;
            }
    
            return this.Equals(anotherState);
        }
    
        public bool Equals(State anotherState)
        {
            return this.GetHashCode() == anotherState.GetHashCode();
        }
    
        public static bool operator ==(State a, State b)
        {
            // If both are null, or both are same instance, return true.
            if (System.Object.ReferenceEquals(a, b))
            {
                return true;
            }
    
            // If one is null, but not both, return false.
            if (((object)a == null) || ((object)b == null))
            {
                return false;
            }
            return a.Equals(b);
        }
    
        public static bool operator !=(State a, State b)
        {
            return !a.Equals(b);
        }
    
    }
    
    public class PersistantHelper
    {
        private string filename;
        private State _state;
    
        public PersistantHelper(string xmlFilename = "RegexTesterSettings")
        {
            string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            this.filename  = Path.Combine(appDataPath, xmlFilename);
        }
    
        private XmlSerializer _serializer;
        private XmlSerializer Serializer
        {
            get
            {
                if (this._serializer == null)
                {
                    this._serializer = new XmlSerializer(typeof(State));
                }
                return this._serializer;
            }
        }
    
        private void SaveState(State state)
        {
            if (File.Exists(this.filename))
            {
                File.Delete(this.filename);
            }
            var stream  = new FileStream(this.filename,  FileMode.OpenOrCreate, FileAccess.Write,FileShare.None);
            this.Serializer.Serialize(stream, state);
            stream.Close();
        }
    
        public State State
        {
            get
            {
                if (this._state == null)
                {
                    this._state = this.GetState();
                }
                return this._state;
            }
            set 
            {
                if (this.State != value)
                {
                    this.SaveState(value);
                }
            }
        }
    
        private State dummyState = new State() { Options = 0 };
        private State GetState()
        {
            if (!File.Exists(this.filename))
            {
                return this.dummyState;
            }
            Stream stream = null;
            try
            {
                stream = new FileStream(this.filename, FileMode.Open, FileAccess.Read,FileShare.None);
                var o = this.Serializer.Deserialize(stream);
                return (State)o;
            }
            catch
            {
                return this.dummyState;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
    
        }
    }
    

    then load and save the state from your app:

        private PersistantHelper persistantHelper;
        public frmTester()
        {
            InitializeComponent();
            this.persistantHelper = new PersistantHelper();
            .
            .
            .
        }
    
    private void LoadPersistantData()
    {
        State state = this.persistantHelper.State;
        this.txtPattern.Text = state.Pattern;
        this.txtTest.Text = state.TestString;
        foreach (Control c in this.pnlOptions.Controls)
        {
            if (c is CheckBox)
            {
                var chk = c as CheckBox;
                int tag = int.Parse(c.Tag.ToString());
                chk.Checked = (state.Options & tag) == tag;
            }
        }
    }
    
    
    private void SavePersistantData()
    {
        this.persistantHelper.State = new State()
        {
            Options = (int)this.GetOptions(),
            Pattern = txtPattern.Text,
            TestString = txtTest.Text
        };
    }       
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple C# application that must run a console application as one
I have a simple test application (C# console application) that does an HTTP GET
I have a very simple console application that creates a text file. Below is
I am writing a simple console application in Objective-C but I have heard that
I have a simple Console Project in Visual Studio that I want to run
I have a simple console application that is designed to read a custom Configuration
I have a simple console application that runs calculations in several threads (10-20 of
I have a simple console application that outputs a menu and waits for user
I have a simple piece of code in a .NET console application that tries
I have a console application that prints on standard output. I want to implement

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.