如果您希望它们并行运行,可以使用Parallel.Invoke :
while (true)
{
Parallel.Invoke(BinaryConversion, ValidateEmails);
System.Threading.Thread.Sleep(1000);
}
根据您的编辑,一个更长的示例:
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()
{
}
}
}
根据您的评论,两者同时运行而不受对方干扰:
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()
{
}
}
}