Check if file exists for 20 seconds C#

Asked

Viewed 271 times

0

I’m making a module that works with file exchange and the documentation asks to wait 20 seconds for the status file xxxxxxxx.However, I do not know how to implement this in C#. I was trying something on that line, but it turns asynchronously, so I can’t get the result bool if the file exists or not at the time I want:

public void ValidarArquivoStatus(int numeroSequencialDoArquivo)
{
    seq = numeroSequencialDoArquivo;
    aTimer = new Timer(1000);
    aTimer.Elapsed += OnTimedEvent;
    aTimer.AutoReset = true;
    aTimer.Enabled = true;

}

private static void OnTimedEvent(Object source, ElapsedEventArgs e)
{
    Principal.ValidouArquivoDeStatus = false;
    previousTime = e.SignalTime;
    //int tempoMaximoTentativaExecucao = Convert.ToInt32(Funcoes.LeParametro(14, "7", false));
    int tempoMaximoTentativaExecucao = 20; 
    if (ExisteArquivoDeStatus(seq))
    {
        //aTimer.Enabled = false;
        Principal.ValidouArquivoDeStatus = true;
        aTimer.Enabled = false;
    }
    nEventsFired++;
    if ((nEventsFired == tempoMaximoTentativaExecucao) || (Principal.ValidouArquivoDeStatus == true))
    {
        aTimer.Enabled = false;
    }
}
  • What is the signature of the method to which you perform the verification?

2 answers

1


Taking into account that your validation method is not asynchronous, I will assume that you need to check every 20 seconds if there is a file in a given directory.

Following your line of reasoning the code below executes the method DoWork() through the timer_Elapsed.

At the end of the implementation of DoWork(), the timer is "reset" (finally) and the DoWork() is called again.

using System;
using System.Timers;

namespace LearnTemporizador
{
    class Program
    {
        private static Timer timer;

        static void Main(string[] args)
        {
            timer = new Timer();

            timer.Elapsed += timer_Elapsed;

            timer.AutoReset = false;

            timer.Enabled = true;

            timer.Interval = TimeSpan.FromSeconds(20).TotalMilliseconds;

            timer.Start();

            Console.ReadKey();
        }

        private static void DoWork()
        {
            Console.WriteLine($"Validou! {Validar()}");
        }

        private static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                DoWork();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                timer.Start();
            }
        }

        private static bool Validar()
        {
            return true;
        }
    }
}

0

If you’re just waiting that long, you can use the thread timer.

just add the code below before the file check:

Thread.Sleep(20000);

Remark: In case you are not using, you need to import below:

using System.Threading;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.