vendredi 29 juillet 2016

Add a timer in a checkbox in C#

I've a C# program who allow to export data from a format (txt, dat ..) to another. To do this, the last step is to click on a Export button. I would like to add a timer in the export button, so export can be performed automatically each 18minutes. I'm working with following codes to try to display message when the box is checked:

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {

        do_checked();
    }

    private void do_checked()
    {
        buttonExportData.Enabled = checkBox1.Checked;
    }

    private class auto_export
    {
        private static System.Timers.Timer aTimer;

        private void MainForm()
        {
            // Normally, the timer is declared at the class level, 
            // so that it stays in scope as long as it is needed. 
            // If the timer is declared in a long-running method,   
            // KeepAlive must be used to prevent the JIT compiler  
            // from allowing aggressive garbage collection to occur  
            // before the method ends. You can experiment with this 
            // by commenting out the class-level declaration and  
            // uncommenting the declaration below; then uncomment 
            // the GC.KeepAlive(aTimer) at the end of the method. 
            //System.Timers.Timer aTimer; 

            // Create a timer with a 30 minutes (1200 second interval).
            aTimer = new System.Timers.Timer(1800000);

            // Hook up the Elapsed event for the timer.
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

            // Set the Interval to 18minutes (1080 seconds or 1080000 milliseconds).
            aTimer.Interval = 1080000;
            aTimer.Enabled = true;

            Console.WriteLine("Press the Enter key to exit the program.");
            Console.ReadLine();

            // If the timer is declared in a long-running method, use 
            // KeepAlive to prevent garbage collection from occurring 
            // before the method ends. 
            //GC.KeepAlive(aTimer);
        }

        // Specify what you want to happen when the Elapsed event is  
        // raised. 
        private static void OnTimedEvent(object source, ElapsedEventArgs e)

        {
            if (checkBox1.Checked == true)
            { Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime); }
        }

But, Visual Studio 2015 display errors: Severity Code Description Project File Line Error CS0120 An object reference is required for the non-static field, method, or property 'MainForm.checkBox1'




Aucun commentaire:

Enregistrer un commentaire