Xavier Dennis
Xavier Dennis

Reputation: 39

Continuous Execution Multiple Functions independently c#

I want to call multiple functions continuously each minute inside a while loop. Even any function execution time takes more than one minute, other functions should not wait for other functions to complete. If I write like the below,

        while (true)
        {
            if(executebinary) BinaryConversion();
            if(emailvalidation) ValidateEmails();

            System.Threading.Thread.Sleep(60000);
        }

any delay in function BinaryConversion() will also delayed execution of function ValidateEmails(). Is there any way to run both functions simultaneously?

Upvotes: 0

Views: 1640

Answers (2)

nvoigt
nvoigt

Reputation: 77304

You can use Parallel.Invoke if you want them to run in parallel:

while (true)
{

    Parallel.Invoke(BinaryConversion, ValidateEmails);

    System.Threading.Thread.Sleep(1000);
}

Based on your edit a longer example:

namespace CSharp
{
  using System;
  using System.Threading.Tasks;

  class Program
  {
    static void Main()
    {
      bool executebinary = true;
      bool emailvalidation = false;

      while (true)
      {
        Parallel.Invoke(
          () =>
            {
              if (executebinary)
              {
                BinaryConversion();
              }
            },
          () =>
            {
              if (emailvalidation)
              {
                ValidateEmails();
              }
            });

        System.Threading.Thread.Sleep(1000);
      }
    }

    private static void ValidateEmails()
    {

    }

    private static void BinaryConversion()
    {

    }
  }
}

Based on your comment both running simultanously without interference from the other:

namespace CSharp
{
  using System;
  using System.Threading.Tasks;

  class Program
  {
    static void Main()
    {
      bool executebinary = true;
      bool emailvalidation = false;

      Parallel.Invoke(
        () =>
          {
            while(true) if (executebinary) BinaryConversion();
          },
        () =>
          {
            while(true) if (emailvalidation) ValidateEmails();
          });
      }
    }

    private static void ValidateEmails()
    {

    }

    private static void BinaryConversion()
    {

    }
  }
}

Upvotes: 1

seshuk
seshuk

Reputation: 182

There are multiple ways: If you are using C# 5 then two ways:

if you create a methods using async, you can

Timer: Kick off a timer with 1 minute interval and then call the methods using async & await: http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx or As mentioned by nvoigt

In this case you don;t need Thread.Sleep.

If you are using a earlier versions:

You can still use timer and then create a delegate for these methods and then invoke them asynchronously:

http://msdn.microsoft.com/en-us/magazine/cc301332.aspx

The reason you might want to avoid thread.sleep is if its a UI application, it would block the thread of a minute, which you want to avoid.

Upvotes: 0

Related Questions