简体   繁体   English

C#在控制台应用程序中管理线程

[英]C# Managing threads in console application

Good day everyone, I need to say firstly, that I am not a C# developer in anyway, I have been tasked from my boss to "Make it work". 大家好,首先我要说的是,我无论如何都不是C#开发人员,我的老板已责成我“让它工作”。

What I want is to have a thread that will spin off, not interrupt main(), call a function CcnDirSearch() and re perform this action after a certain amount of time. 我想要的是拥有一个可以旋转的线程,而不中断main(),调用函数CcnDirSearch()并在一定时间后重新执行此操作。

My code currently runs in console about 1 time (sometimes 6 times) and then stops. 我的代码当前在控制台中运行大约1次(有时6次),然后停止。 I think the threads(or something like this) are ending before the function is completing. 我认为线程(或类似的东西)在函数完成之前就结束了。

Here is my code: 这是我的代码:

 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;
        }

> Here is the code of the function I need to call off . >这是我需要取消的功能代码。

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");
    }

You could use a DispatcherTimer to call a function on a given time interval, then in that function create and start a new Thread in which you execute your function. 您可以使用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();
}

I want to Thanks everyone for all your help. 我要感谢大家的所有帮助。 I found a work around that worked for me. 我找到了适合我的工作。

As I said before , I am not a C# developer (I play one on TV) so I am certain there are somethings where this code base can be improved . 就像我之前说过的,我不是C#开发人员(我在电视上玩),所以我可以确定可以改进此代码库。

If someone can write a better answer, I will happily accept it. 如果有人可以写出更好的答案,我会很乐意接受。

I just decided to launch the code differntly . 我只是决定以不同的方式启动代码。

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;
            }

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM