Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使Windows应用程序保持活动状态,直到进程停止..C#_C#_Visual Studio - Fatal编程技术网

使Windows应用程序保持活动状态,直到进程停止..C#

使Windows应用程序保持活动状态,直到进程停止..C#,c#,visual-studio,C#,Visual Studio,我创建了一个控制台应用程序,每2秒写入一个文件。它工作得很好: static void Main(string[] args) { m_streamWriter.BaseStream.Seek(0, SeekOrigin.End); m_streamWriter.Write(" File Write Operation Starts : "); m_streamWriter.WriteLine("{0} {1}", DateTim

我创建了一个控制台应用程序,每2秒写入一个文件。它工作得很好:

     static void Main(string[] args)
    {
        m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
        m_streamWriter.Write(" File Write Operation Starts : ");
        m_streamWriter.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
        m_streamWriter.WriteLine(" ===================================== \n");
        m_streamWriter.Flush();
        timer1 = new Timer(TimerCallback, null, 0, 2000);
        Console.ReadLine();
    }
    private static void TimerCallback(Object o)
    {
        m_streamWriter.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
            DateTime.Now.ToLongDateString());
        m_streamWriter.Flush();
    }
但是,我需要它在后台运行,而不出现命令提示符。因此,我将输出类型更改为“Windows应用程序”。现在,当我运行它时,它会将第一行写入文件,然后就是它。程序在一次迭代后终止。计时器当然也会终止

在手动停止进程之前,如何使Windows应用程序保持活动状态


(注意)-我不能使用windows服务,而且我不知道有任何其他.Net程序可以在后台运行。如果有人知道,请分享。

Console.Readline不会阻止进程终止。在windows应用程序中,Console.In设置为NullStreamReader,这意味着调用Console.ReadLine将立即返回,因为ReadLine的NullStreamReader实现只是“
返回null

下面的实现在迭代1000次后终止,但如果您从未发出取消信号,它将无限期运行

我确信存在更好的实现,我只是把它从我的头顶上一扫而光

有许多方法可以构建此结构,以下是一个示例:

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    class Program
    {
        [STAThread]
        static void Main()
        {
            CancellationTokenSource cts = new CancellationTokenSource();
            Program instance = new Program(cts);

            Task.Run(() => { instance.MyApp(); });

            while (!cts.Token.IsCancellationRequested)
            {
                Application.DoEvents();
                Thread.Sleep(500);
            };



        }

        private CancellationTokenSource _cts = null;
        public Program(CancellationTokenSource cts)
        {
            this._cts = cts;
        }

        public void MyApp()
        {
            FileInfo fi = new FileInfo(Environment.ExpandEnvironmentVariables(@"%USERPROFILE%\dummydata\test.txt"));
            fi.Directory.Create();
            int i = 0;
            while(i < 1000)
            {
                i++;
                File.AppendAllText(fi.FullName, i + "=" + DateTime.Now.ToString() + "\r\n");
            }
            this._cts.Cancel();
        }
    }
}
使用系统;
使用System.IO;
使用系统线程;
使用System.Threading.Tasks;
使用System.Windows.Forms;
命名空间WindowsFormsApp1
{
班级计划
{
[状态线程]
静态void Main()
{
CancellationTokenSource cts=新的CancellationTokenSource();
程序实例=新程序(cts);
运行(()=>{instance.MyApp();});
而(!cts.Token.IsCancellationRequested)
{
Application.DoEvents();
睡眠(500);
};
}
私有取消令牌源_cts=null;
公共程序(取消令牌源cts)
{
这个._cts=cts;
}
public void MyApp()
{
FileInfo fi=newfileinfo(Environment.ExpandEnvironmentVariables(@“%USERPROFILE%\dummydata\test.txt”);
fi.Directory.Create();
int i=0;
而(i<1000)
{
i++;
File.AppendAllText(fi.FullName,i+“=”+DateTime.Now.ToString()+“\r\n”);
}
这个。_cts.Cancel();
}
}
}

感谢您的回复。这是我最想做的。我想不断写入文件,直到手动关闭应用程序。这只会写入文件一次。我会看看我如何调整你的代码来做到这一点。是的,这只是一个模板供你开始。用你想要的任何代码替换我的MyApp代码。主循环将保持应用程序运行。如果你使用计时器,你可能还想在MyApp部分放置一个无限循环。