我知道在VB.NET中可以这样做,但我无法在C#中找到它。怎么会在这里做到?
答案 0 :(得分:3)
您可以在Main
(默认情况下)更改Program.cs
例程(程序的入口点),以使用Application.Run()
代替Application.Run(Form)
。
或者,您可以指定自己的ApplicationContext
并覆盖OnMainFormClosed
,以提供与自动关闭不同的行为。
答案 1 :(得分:1)
您可以隐藏表单并在系统托盘中放置一个图标。覆盖结束事件并隐藏它,然后在用户点击托盘图标时显示它。
有关结束活动的更多信息以及如何取消关闭:http://msdn.microsoft.com/en-us/library/system.windows.forms.form.closing.aspx
答案 2 :(得分:0)
开始一个新的Thread
来做你想要的就足够了,但我不确定这是否能满足你的目标。
编辑:
我建议的一种技术示例:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
namespace HangApp
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
Runner r = new Runner();
}
class Runner
{
internal Runner()
{
_start = DateTime.Now;
Thread t = new Thread(ThreadStart);
t.Start();
}
DateTime _start;
void ThreadStart()
{
while (true)
{
// some stuff to do
if (ExitConditionMet())
{
break;
}
}
}
bool ExitConditionMet()
{
// will run the app for 5 seconds after main form was closed
return (DateTime.Now - _start).TotalSeconds > 5;
}
}
}
}
顺便说一句,上面的代码只是一个演示,绝不应该: