我写的一个服务,利用静态变量,怎么每次都是从0开始的,而不能保存值。(200分)

D

dcsdcs

Unregistered / Unconfirmed
GUEST, unregistred user!
我写的一个服务,利用静态变量,怎么每次都是从0开始的,而不能保存值。
代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Threading;
using System.Diagnostics;
using System.ServiceProcess;
using System.IO;
namespace Serv1
{
public class Service1 : System.ServiceProcess.ServiceBase
{
public Thread threadwork;
private System.ComponentModel.Container components = null;
public Service1()
{
InitializeComponent();
}
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
private void InitializeComponent()
{
ServiceName = "csharpsrv";
AutoLog=false;
glb.bz=0;
threadwork= new Thread(new ThreadStart(threadexec));
threadwork.Start();
threadwork.Suspend();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
threadwork.Abort();
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// 设置具体的操作,以便服务可以执行它的工作。
/// </summary>
protected override void OnStart(string[] args)
{
/*EventLog myLog = new EventLog();
myLog.Source =ServiceName;
myLog.WriteEntry("服务已经启动");
myLog.Dispose();
glb.writetx("Service Started "+DateTime.Now.ToString());
*/
threadwork.Resume();
}
protected override void OnStop()
{
threadwork.Suspend();
//glb.writetx("Service Stopped "+DateTime.Now.ToString());
}
public void threadexec()
{
int i;
while (true)
{
i=glb.bz;
i=i+1;
glb.bz=i;
glb.writetx("thread write "+glb.bz.ToString()+" "+DateTime.Now.ToString());
Thread.Sleep(2000);
}
}
}
public class glb
{
public static int bz;
public static void writetx(string inx)
{
FileStream fs = new FileStream(@"D:/c#test/Serv1/bin/Release/log.txt" , FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine(inx);
m_streamWriter.Flush();
m_streamWriter.Close();
fs.Close();
}
}
}
然后安装服务,启动,停止,启动,停止,启动,停止,启动,停止
结果D:/c#test/Serv1/bin/Release/log.txt文件如下:
thread write 1 2006-11-23 22:43:21
thread write 2 2006-11-23 22:43:23
thread write 3 2006-11-23 22:43:25
thread write 1 2006-11-23 22:43:30
thread write 2 2006-11-23 22:43:32
thread write 3 2006-11-23 22:43:34
thread write 4 2006-11-23 22:43:36
thread write 1 2006-11-23 22:44:01
thread write 2 2006-11-23 22:44:03
thread write 1 2006-11-23 22:44:20
thread write 2 2006-11-23 22:44:22
thread write 3 2006-11-23 22:44:24
thread write 4 2006-11-23 22:44:26
怎么结果不是下面呢,
thread write 1 2006-11-23 22:43:21
thread write 2 2006-11-23 22:43:23
thread write 3 2006-11-23 22:43:25
thread write 4 2006-11-23 22:43:30
thread write 5 2006-11-23 22:43:32
thread write 6 2006-11-23 22:43:34
thread write 7 2006-11-23 22:43:36
thread write 8 2006-11-23 22:44:01
thread write 9 2006-11-23 22:44:03
thread write 10 2006-11-23 22:44:20
thread write 11 2006-11-23 22:44:22
thread write 12 2006-11-23 22:44:24
thread write 13 2006-11-23 22:44:26
 
这也行?
 
人品好~~[:)]
 
进来看看。
 
领分来了,[:)]
 
多给点!
 
晕..什么问题啊?来送分的?
 
呵呵,路过。
 
偶接分就是了。。。。
 
接分咯~~~~
 
天上掉陷饼啊.
 
什么东西理解错误,说出来大家都听听,也好前车之鉴
 
我一笑而过[:)]
 
强烈领分
 
接分了。嘿嘿:)
 
接分了。嘿嘿:)
 
厉害和好人?:)厉害和好人?:)
 
顶部