一个简单的实时处理机,RealTime+WebServices

说这个实时处理机简单,主要是因为开设的几个线程都是单一的。他们之间无互相通信。所以相对很简单。
不过每一个线程直接调用了一个独立类似的过程,所以这里就用委托代替。启动线程用一个简单的类封装。下面是封装代码:

None.gif using  System;
None.gif
using  System.Threading;
None.gif
None.gif
namespace  TRealTime
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// TopWork 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class TopWork
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//直接调用这些方法,可以用一个参数
InBlock.gif
        public delegate String DelegateRealTimeWebMethod();
InBlock.gif        
public delegate void DelegateSendLog(String msg,TRealTime.MainForm.LogFlag lf);
InBlock.gif        
public delegate void DelegateWork(TopWork t);
InBlock.gif
InBlock.gif        
public TopWork()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
public TopWork(DelegateRealTimeWebMethod m,int threadIndex,int sleepLong)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            RealTimeWebMethod 
= m;
InBlock.gif            ThreadIndex       
= threadIndex;
InBlock.gif            SleepLong         
= sleepLong;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 默认运行中
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private bool threadFlag = true;
InBlock.gif        
public bool ThreadFlag
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
lock(this)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return threadFlag;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
lock(this)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    threadFlag 
= value;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 委托方法
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        DelegateSendLog n;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 委托方法
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public DelegateSendLog DelSendLog
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return n;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{n = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 委托方法
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        DelegateRealTimeWebMethod m;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 委托方法
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public DelegateRealTimeWebMethod RealTimeWebMethod
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return m;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{m = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 线程索引
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private int threadIndex = 1;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 线程索引
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public int ThreadIndex
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return threadIndex;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{threadIndex = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 睡眠时间
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private int sleepLong = 1000;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 睡眠时间
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public int SleepLong
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return sleepLong;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{sleepLong = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Run()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            String Msg 
= String.Empty; 
InBlock.gif
InBlock.gif            DelSendLog(
"启动线程" + ThreadIndex, TRealTime.MainForm.LogFlag.Normal);
InBlock.gif
InBlock.gif            
while(ThreadFlag)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Msg 
= RealTimeWebMethod();                
InBlock.gif    
InBlock.gif                    
if(Msg != null && Msg.Length > 0)
InBlock.gif                        DelSendLog(Msg, TRealTime.MainForm.LogFlag.Normal);
InBlock.gif
InBlock.gif                    Thread.Sleep(SleepLong);
//休息1秒
ExpandedSubBlockEnd.gif
                }

InBlock.gif                
catch(ThreadAbortException e)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    DelSendLog(e.Message, TRealTime.MainForm.LogFlag.Warn);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            DelSendLog(
"停止线程" + ThreadIndex,  TRealTime.MainForm.LogFlag.Warn);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

下面是启动线程调用:

None.gif              // 启动线程1
None.gif
            T1.RealTimeWebMethod  =   new  TRealTime.TopWork.DelegateRealTimeWebMethod(RT.RealTimeWebMethod1);
None.gif            T1.DelSendLog        
=   new  TRealTime.TopWork.DelegateSendLog(SendLog);
None.gif            T1.ThreadIndex       
=   1 ;
None.gif            T1.SleepLong         
=   2000 ;
None.gif
None.gif            T1.ThreadFlag 
=   true ;
None.gif            thread1 
=   new  Thread( new  ThreadStart(T1.Run));
None.gif            thread1.IsBackground
= true ;
None.gif            thread1.Start();

代码基本大意这个样子,如果有其他不明白的地方,可以给我来email

转载于:https://www.cnblogs.com/veelee/archive/2004/08/25/36307.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值