大家好, 首先我要说的是,我无论如何都不是C#开发人员,我的上司已责成我“让它工作”。
我想要的是拥有一个可以旋转的线程,而不中断main(),调用函数CcnDirSearch()并在一定时间后重新执行此操作。
我的代码当前在控制台中运行大约1次(有时是6次),然后停止。我认为线程(或类似的东西)在函数完成之前就结束了。
这是我的代码:
public int Run()
{
Task.Factory.StartNew(() => CcnDirFireAway());
...
...
//continues main();
>
public void CcnDirFireAway()
{
if (ScanDir != "")
{
Console.WriteLine("Starting Initial Scan on Directory: " + ScanDir + "\n\n\n");
TimerCallback tmCallback = CheckEffectExpiry;
Timer timer = new Timer(tmCallback, "test", 1000, 1000);
}
}
>
public void CheckEffectExpiry(object objectInfo)
{
//TODO put your code
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(DateTime.Now + " Starting Scan.....\n");
Console.ForegroundColor = ConsoleColor.White;
//Here is a call to my function that I want to call.
// I noticed that If I don't call it the programs continues to run harmoniously
Searcher.CcnDirSearch(ScanDir);
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(DateTime.Now + " Finished Scan.....\n");
Console.ForegroundColor = ConsoleColor.White;
}
> 这是我需要取消的功能代码。
public static void CcnDirSearch(string sDir)
{
try
{
foreach (string file in Directory.EnumerateFiles(sDir, "*.*", SearchOption.AllDirectories))
{
using (var stream = File.OpenRead(file))
{
// Console.WriteLine(DateTime.Now + " Checking File : " + file);
bool Mcard = Searcher.CCNSearch(file, De.Thekid.INotify.Runner.MASTERCARD, false);
bool VCARD = Searcher.CCNSearch(file, De.Thekid.INotify.Runner.VISA, false);
bool ACARD = Searcher.CCNSearch(file, De.Thekid.INotify.Runner.AMEX, false);
if (Mcard)
{
Console.WriteLine(DateTime.Now + " MasterCard Number Found In File >> " + file);
//Inotifywatch.EventForward.UDPSend(512, RServer, ("<30>" + DateTime.Now + " MasterCard Number Found In File >> " + fullpath+ "\n"));
Logger.WriteEvent(DateTime.Now + " MasterCard Number Found In File >> " + file + "\n");
}
else if (VCARD)
{
Console.WriteLine(DateTime.Now + " Visa Card Number Found In File >> " + file);
//Inotifywatch.EventForward.UDPSend(512, RServer, ("<30>" + DateTime.Now + " Visa Card Number Found In File >> " + fullpath+ "\n"));
Logger.WriteEvent(DateTime.Now + " Visa Card Number Found In File >> " + file + "\n");
}
else if (ACARD)
{
Console.WriteLine(DateTime.Now + " AMEX Card Number Found In File >> " + file);
//Inotifywatch.EventForward.UDPSend(512, RServer, ("<30>" + DateTime.Now + " AMEX Card Number Found In File >> " + fullpath+ "\n"));
Logger.WriteEvent(DateTime.Now + " Amex Card Number Found In File >> " + file + "\n");
}
}
}
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
Console.Write("Finished the Search\n");
}
答案 0 :(得分:0)
您可以使用DispatcherTimer
在给定的时间间隔内调用函数,然后在该函数中创建并启动新的Thread
,在其中执行函数。
public static void Main(string[] args)
{
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(5000);;
timer.IsEnabled = true;
timer.Tick += OnTimerTick;
}
private void OnTimerTick(object sender, EventArgs e)
{
var thread = new Thread(new ThreadStart(()=>yourMethodToCall()));
thread.Start();
}
答案 1 :(得分:0)
我要感谢大家的所有帮助。我发现有一项对我有用的工作。
正如我之前所说,我不是C#开发人员(我在T.V上玩游戏),因此我可以确定在某些方面可以改进此代码库。
如果有人可以写出更好的答案,我会很乐意接受。
我只是决定以不同的方式启动代码。
Timer x = new Timer(state => CheckEffectExpiry(1), null, 5000 /* When to start*/, 300000 /* when to retry */);
>
public void CheckEffectExpiry(object objectInfo)
{
//I hate C#'s way of accessing variables and such .
//So I am doing this...
Console.Write(DateTime.Now + " I was hit\n");
if (lockf == 1)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(DateTime.Now + " Starting Scan.....\n");
Console.ForegroundColor = ConsoleColor.White;
lockf = 0;
Searcher.CcnDirSearch(ScanDir);
lockf = 1;
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(DateTime.Now + " Finished Scan.....\n");
Console.ForegroundColor = ConsoleColor.White;
}
}