如何在关闭应用程序后,该进程继续运行!
如何在关闭应用程序后,该进程继续运行! 问题点数:0、回复次数:12Top
1 楼o1o(两蛋一枪)回复于 2003-12-02 19:05:12 得分 0
这个对你有帮助:
在主窗体的 Closed 事件上处理。Top
2 楼MyLf(不睡觉的鱼)回复于 2003-12-02 19:18:18 得分 0
这个好像有些自相矛盾吧?
把界面隐藏起来不就行了吗?Top
3 楼wuyeniao(巷陌·清茶)回复于 2003-12-02 19:26:52 得分 0
我想实现的方法有很多,但是效果不大
1、分两个进程,界面和商务逻辑,关闭界面不影响另一个
2、在一个进程中起用线程(非backgroud)关闭界面不影响线程,但可能不稳定
3、楼上说的:在主窗体的 Closed 事件上处理,再次调用应用程序自身或不响应关闭或隐藏界面,这样将无法正常关闭
效果不大,因为win2k及以上版本可以在进程管理器中杀死进程,除非该进程与其他进程绑定而且被绑定进程应该为操作系统的关键进程无法被停止(取消)EXPl:杀毒软件的监视服务进程
关注中。。。Top
4 楼rroo(天之痕)回复于 2003-12-02 19:39:18 得分 0
分两个进程,界面和商务逻辑,关闭界面不影响另一个??
不對吧,
[STAThread]
static void Main()
{
Application.Run(new MainForm());
}
在Form.cs中,关闭窗体必然导致主进程退出,
建议將窗体的Enable设为false,然後將Visible=false,就可以达到阴藏功能,
其实,当对一个子窗体执行Close后,该窗体的Visible=false,这个时候假如你将该窗体的Visible设为true,则又可以显示该窗体,将到这里,我想大家应该知道它是怎么工作了吧!Top
5 楼xiaha3(夏)回复于 2003-12-02 19:39:52 得分 0
用windows服务把Top
6 楼ligonggong(李公公)回复于 2003-12-02 19:43:42 得分 0
请看此段代码如何实现!:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace ChatServer
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private int listenport = 5555;
private TcpListener listener;
private ArrayList clients;
private Thread processor;
private Socket clientsocket;
private Thread clientservice;
private System.Windows.Forms.ListBox lbClients;
private System.Windows.Forms.LinkLabel linkLabel1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
clients = new ArrayList();
processor = new Thread(new ThreadStart(StartListening));
processor.Start();
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.lbClients = new System.Windows.Forms.ListBox();
this.linkLabel1 = new System.Windows.Forms.LinkLabel();
this.SuspendLayout();
//
// lbClients
//
this.lbClients.ItemHeight = 12;
this.lbClients.Location = new System.Drawing.Point(24, 24);
this.lbClients.Name = "lbClients";
this.lbClients.Size = new System.Drawing.Size(248, 232);
this.lbClients.TabIndex = 0;
//
// linkLabel1
//
this.linkLabel1.AutoSize = true;
this.linkLabel1.Location = new System.Drawing.Point(40, 272);
this.linkLabel1.Name = "linkLabel1";
this.linkLabel1.Size = new System.Drawing.Size(184, 14);
this.linkLabel1.TabIndex = 1;
this.linkLabel1.TabStop = true;
this.linkLabel1.Text = "此程序代码从:网络精英 下载!";
this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 301);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.linkLabel1,
this.lbClients});
this.Name = "Form1";
this.Text = "服务器端";
this.ResumeLayout(false);
}
#endregion
private void StartListening()
{
listener = new TcpListener(listenport);
listener.Start();
while (true)
{
try
{
Socket s = listener.AcceptSocket();
clientsocket = s;
clientservice = new Thread(new ThreadStart(ServiceClient));
clientservice.Start();
}
catch(Exception e)
{
Console.WriteLine(e.ToString() );
}
}
}
private void ServiceClient()
{
Socket client = clientsocket;
bool keepalive = true;
while (keepalive)
{
Byte[] buffer = new Byte[1024];
client.Receive(buffer);
string clientcommand = System.Text.Encoding.ASCII.GetString(buffer);
string[] tokens = clientcommand.Split(new Char[]{'|'});
Console.WriteLine(clientcommand);
if (tokens[0] == "CONN")
{
for(int n=0; n<clients.Count; n++)
{
Client cl = (Client)clients[n];
SendToClient(cl, "JOIN|" + tokens[1]);
}
EndPoint ep = client.RemoteEndPoint;
Client c = new Client(tokens[1], ep, clientservice, client);
clients.Add(c);
string message = "LIST|" + GetChatterList() +"\r\n";
SendToClient(c, message);
lbClients.Items.Add(c);
}
if (tokens[0] == "CHAT")
{
for(int n=0; n<clients.Count; n++)
{
Client cl = (Client)clients[n];
SendToClient(cl, clientcommand);
}
}
if (tokens[0] == "PRIV")
{
string destclient = tokens[3];
for(int n=0; n<clients.Count; n++)
{
Client cl = (Client)clients[n];
if(cl.Name.CompareTo(tokens[3]) == 0)
SendToClient(cl, clientcommand);
if(cl.Name.CompareTo(tokens[1]) == 0)
SendToClient(cl, clientcommand);
}
}
if (tokens[0] == "GONE")
{
int remove = 0;
bool found = false;
int c = clients.Count;
for(int n=0; n<c; n++)
{
Client cl = (Client)clients[n];
SendToClient(cl, clientcommand);
if(cl.Name.CompareTo(tokens[1]) == 0)
{
remove = n;
found = true;
lbClients.Items.Remove(cl);
}
}
if(found)
clients.RemoveAt(remove);
client.Close();
keepalive = false;
}
}
}
private void SendToClient(Client cl, string message)
{
try
{
byte[] buffer = System.Text.Encoding.ASCII.GetBytes(message.ToCharArray());
cl.Sock.Send(buffer,buffer.Length,0);
}
catch(Exception)
{
cl.Sock.Close();
cl.CLThread.Abort();
clients.Remove(cl);
lbClients.Items.Remove(cl.Name + " : " + cl.Host.ToString());
}
}
private string GetChatterList()
{
string chatters = "";
for(int n=0; n<clients.Count; n++)
{
Client cl = (Client)clients[n];
chatters += cl.Name;
chatters += "|";
}
chatters.Trim(new char[]{'|'});
return chatters;
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("http://www.chinanetboy.com");
}
}
}
Top
7 楼ligonggong(李公公)回复于 2003-12-02 19:45:56 得分 0
但不还是不明白在哪实现,如何实现的~!我从代码本身看没看出什么特别~!Top
8 楼Edifier0709(腦袋重構中.....)回复于 2003-12-02 20:56:57 得分 0
在关闭时不要Dispose,将它Close后,进程还在内存中...Top
9 楼stephenli(翔)回复于 2003-12-02 21:14:33 得分 0
学习Top
10 楼wuyeniao(巷陌·清茶)回复于 2003-12-05 17:58:46 得分 0
up
Top
11 楼simanh()回复于 2003-12-05 23:16:53 得分 0
关闭了还不退出thread?那谁去关这个thread啊......
想做这种效果建议做成winServerTop
12 楼tfhu2000(唐方虎)回复于 2003-12-05 23:27:41 得分 0
simanh()说的有道理,I Agree!Top




