c#控制台应用程序在大约8分钟后停止

本文关键字:8分钟 控制台 应用程序 | 更新日期: 2023-09-27 18:06:51

我有一个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
    }
}
}

谁能猜出是什么问题吗?

c#控制台应用程序在大约8分钟后停止

似乎计时器是由GC收集的。尝试在外部作用域中声明计时器。

private static Timer t;
private static Timer t2;

…等等