C#控制台应用程序在将近8分钟后停止

时间:2015-08-07 05:01:15

标签: c# timer console directory

我有一个C#控制台应用程序,它使用计时器每15秒读取一个文件夹。它工作正常,但在将近8分钟后暂停并停止阅读文件夹。有什么理由吗?

using System;
sing System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
using System.Data;
using System.Data.OracleClient;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace FolderFileReader
{
class Program
{
    static void Main()
    {

        if (SingleInstance.SingleApplication.Run() == false)
        {
            return;
        }

        Thread thread1 = new Thread(new ThreadStart(startNewBusiness));
        thread1.Start();

        Thread thread2 = new Thread(new ThreadStart(startEndorsement));
        thread2.Start();

        Thread thread3 = new Thread(new ThreadStart(startRenewal));
        thread3.Start();

        Thread thread4 = new Thread(new ThreadStart(startCancellation));
        thread4.Start();

        Console.ReadLine();
    }

    private static void startNewBusiness()
    {
        Timer t = new Timer(ProcessNewBusinessFolder, null, 0, 5000);

    }
    private static void startEndorsement()
    {

        Timer t2 = new Timer(ProcessEndorsementFolder, null, 0, 5000);

    }
    private static void startRenewal()
    {
        Timer t3 = new Timer(ProcessRenewalFolder, null, 0, 5000);

    }
    private static void startCancellation()
    {
        Timer t4 = new Timer(ProcessCancellationFolder, null, 0, 5000);

    }


    private static void ProcessNewBusinessFolder(Object o)
    {



        Console.Clear();
        Console.WriteLine("Do not close this console (New Business).... ");


        DirectoryInfo d = new DirectoryInfo(@"E:\HNBGI\SCN_DOCS\NEW\");
        DirectoryInfo dest = new DirectoryInfo(@"E:\HNBGI\QUEUED_SCN_DOCS\NEW\");


        if (!d.Exists)
        {
            return;
        }

        FileInfo[] Files = d.GetFiles("*.pdf");
        string quotationNo = "";
        string branchCode = "";
        foreach (FileInfo file in Files)
        {
            quotationNo = file.Name;

            DirectoryInfo newDir = null;

            if (!Directory.Exists(dest.FullName + quotationNo.ToUpper()))
            {
                // newDir = Directory.CreateDirectory(dest.FullName + quotationNo.ToUpper() + "\\");

                System.IO.Directory.CreateDirectory(dest.FullName + quotationNo.Substring(0, file.Name.LastIndexOf(".")).ToUpper());
            }



            if (quotationNo.Length > 10)
            {
                branchCode = quotationNo.Substring(2, 3);
            }

            Console.WriteLine(quotationNo + " -     " + branchCode);
            try
            {

                File.Move(file.FullName, dest.FullName + quotationNo.Substring(0, file.Name.LastIndexOf(".")).ToUpper() + "\\" + file.Name.ToUpper());
                InsertNewBusiness(quotationNo.Substring(0, file.Name.LastIndexOf(".")).ToUpper(), branchCode, "N");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
        Console.ReadKey();



        GC.Collect();
    }
    private static void ProcessEndorsementFolder(Object o)
    {



        Console.Clear();
        Console.WriteLine("Do not close this console (Endorsement).... ");


        DirectoryInfo d = new DirectoryInfo(@"E:\HNBGI\SCN_DOCS\ENDORSEMENT\");
        DirectoryInfo dest = new DirectoryInfo(@"E:\HNBGI\QUEUED_SCN_DOCS\ENDORSEMENT\");



        if (!d.Exists)
        {
            return;
        }


        FileInfo[] Files = d.GetFiles("*.pdf");
        string jobNo = "";
        string branchCode = "";
        foreach (FileInfo file in Files)
        {
            jobNo = file.Name;

            DirectoryInfo newDir = null;

            if (!Directory.Exists(dest.FullName + jobNo.ToUpper()))
            {

                System.IO.Directory.CreateDirectory(dest.FullName + jobNo.Substring(0, file.Name.LastIndexOf(".")).ToUpper());
            }




            Console.WriteLine(jobNo + " -     " + branchCode);
            try
            {

                File.Move(file.FullName, dest.FullName + jobNo.Substring(0, file.Name.LastIndexOf(".")).ToUpper() + "\\" + file.Name.ToUpper());
                UpdateEndorsement(jobNo.Substring(0, file.Name.LastIndexOf(".")).ToUpper());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
        Console.ReadKey();



        // Force a garbage collection to occur for this demo.
        GC.Collect();
    }

    private static void ProcessRenewalFolder(Object o)
    {


        Console.Clear();
        Console.WriteLine("Do not close this console (Renewal).... ");

        DirectoryInfo d = new DirectoryInfo(@"E:\HNBGI\SCN_DOCS\RENEWAL\");
        DirectoryInfo dest = new DirectoryInfo(@"E:\HNBGI\QUEUED_SCN_DOCS\RENEWAL\");



        if (!d.Exists)
        {
            return;
        }


        FileInfo[] Files = d.GetFiles("*.pdf");
        string jobNo = "";
        string branchCode = "";
        foreach (FileInfo file in Files)
        {
            jobNo = file.Name;

            DirectoryInfo newDir = null;

            if (!Directory.Exists(dest.FullName + jobNo.ToUpper()))
            {

                System.IO.Directory.CreateDirectory(dest.FullName + jobNo.Substring(0, file.Name.LastIndexOf(".")).ToUpper());
            }




            Console.WriteLine(jobNo + " -     " + branchCode);
            try
            {

                File.Move(file.FullName, dest.FullName + jobNo.Substring(0, file.Name.LastIndexOf(".")).ToUpper() + "\\" + file.Name.ToUpper());
                UpdateRenewal(jobNo.Substring(0, file.Name.LastIndexOf(".")).ToUpper());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
        Console.ReadKey();



        // Force a garbage collection to occur for this demo.
        GC.Collect();
    }

    private static void ProcessCancellationFolder(Object o)
    {



        Console.Clear();
        Console.WriteLine("Do not close this console (Cancellation).... ");


        DirectoryInfo d = new DirectoryInfo(@"E:\HNBGI\SCN_DOCS\CANCELLATION\");
        DirectoryInfo dest = new DirectoryInfo(@"E:\HNBGI\QUEUED_SCN_DOCS\CANCELLATION\");



        if (!d.Exists)
        {
            return;
        }


        FileInfo[] Files = d.GetFiles("*.pdf");
        string jobNo = "";
        string branchCode = "";
        foreach (FileInfo file in Files)
        {
            jobNo = file.Name;

            DirectoryInfo newDir = null;

            if (!Directory.Exists(dest.FullName + jobNo.ToUpper()))
            {
                // newDir = Directory.CreateDirectory(dest.FullName + quotationNo.ToUpper() + "\\");

                System.IO.Directory.CreateDirectory(dest.FullName + jobNo.Substring(0, file.Name.LastIndexOf(".")).ToUpper());
            }



            string outputFileWithPath = "";
            outputFileWithPath = dest.FullName + jobNo.Substring(0, file.Name.LastIndexOf(".")).ToUpper() + "\\" + file.Name.ToUpper();
            Console.WriteLine(jobNo + " -     " + branchCode);
            try
            {


                File.Move(file.FullName, outputFileWithPath);


                UpdateCancellation(jobNo.Substring(0, file.Name.LastIndexOf(".")).ToUpper());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
        Console.ReadKey();


        GC.Collect();
    }







    public static void InsertNewBusiness(string quotationNo, string branchCode, string JobType)
    {
       \\Code to put Database Entry
    }
    public static void UpdateEndorsement(string jobNo)
    {
      \\Code to put Database Entry
    }

    public static void UpdateRenewal(string jobNo)
    {
     \\Code to put Database Entry
    }


    public static void UpdateCancellation(string jobNo)
    {
      \\Code to put Database Entry
    }

}
}

有人可以猜出这个问题吗?

1 个答案:

答案 0 :(得分:1)

似乎计时器是由GC收集的。尝试在外部范围内声明计时器。

private static Timer t;
private static Timer t2;

......等等。