[英]Keep Windows application alive until process in stopped..C#
I created a console application and I write to a file every 2 seconds.我创建了一个控制台应用程序,每 2 秒写入一个文件。 it works fine:
它工作正常:
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();
}
But, I need this to be something that runs in the background without the command prompt appearing.但是,我需要它是在后台运行而不出现命令提示符的东西。 So I changed the output type to "Windows Application".
所以我将 output 类型更改为“Windows 应用程序”。 Now when I run it, it writes the first line to the file and then that's it.
现在,当我运行它时,它会将第一行写入文件,然后就是这样。 The program terminates after one iteration.
程序在一次迭代后终止。 The timer of course is terminated as well.
当然定时器也被终止。
How do I keep my Windows Application alive until the process is manually stopped?在手动停止进程之前,如何使我的 Windows 应用程序保持活动状态?
(Note) - I cannot do a windows service, and i do not know of any other.Net programs that can run in the background. (注)- 我不能做 windows 服务,而且我不知道任何其他可以在后台运行的.Net 程序。 If anyone knows of one, please share.
如果有人知道,请分享。
Console.Readline won't block your process from terminating. Console.Readline 不会阻止您的进程终止。 In a windows application the Console.In is set to a NullStreamReader, which means calling Console.ReadLine will return immediately since the NullStreamReader implementation of ReadLine is simply "
return null
"在 windows 应用程序中,Console.In 设置为 NullStreamReader,这意味着调用 Console.ReadLine 将立即返回,因为 ReadLine 的 NullStreamReader 实现只是“
return null
”
The implementation below terminates after it iterates 1000 times, but if you never signal the cancellation it will run indefinitely.下面的实现在迭代 1000 次后终止,但如果您从未发出取消信号,它将无限期地运行。
I'm sure better implementations exist, I just whipped this out off the top of my head.我确信存在更好的实现,我只是把它从我的头顶上甩了出来。
There are many ways to structure this, here is one example:有很多方法来构建它,这里是一个例子:
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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.