![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
private System.ComponentModel.BackgroundWorker worker;
1.第一步。也是非常常见的 实现btnOK_Click的功能(也就是点击登陆的功能)
private void btnOK_Click(object sender, EventArgs e)
{
this.Login();
}
2.第二步。实现Login()函数
private void Login()
{
string[] credential = new string[] { txtUserName.Text, txtPassword.Text };
lblProgress.Visible = true;
lblProgress.Refresh();
loginProgress.Visible = true;
loginProgress.Value = loginProgress.Minimum;
worker.RunWorkerAsync(credential); //这里采用BackGroundWork,去了解这个控件,到这一步的时候,它会去执行DoWork这个操作
}
3。第三步: 实现DoWork函数
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
// This method will run on a thread other than the UI thread.
// Be sure not to manipulate any Windows Forms controls created
// on the UI thread from this method.
string[] credential = (string[])e.Argument;
string userName = credential[0];
string password = credential[1];
this.Authenticate(userName, password); //这里我们调用验证函数
}
4.第四步: //在改使用类下新建函数来进行使用该验证
private void Authenticate(string userName, string password)
{
AuthenticateCommand command = new AuthenticateCommand(userName, password); //注意这里,这里我们已经去调用命令模式里面的东西了
((ICommand)command).Execute(); //执行操作
worker.ReportProgress(1, "Loading
![](https://www.cnblogs.com/Images/dot.gif)
}
5.第 五步: //这是 命令模式里面的东西。。。。
public class AuthenticateCommand : ICommand
{
public AuthenticateCommand( string userName, string password )
{
this._UserName = userName;
this._Password = password;
}
void ICommand.Execute()//this is user for Execute The Application
{
UserInfo user = _Application.Settings.UserInfo; //这个是在ObjectModel里面建立的模型
user.UserName = this._UserName;
string passwordHash = HashStringMD5(this._Password);
user.UserPassword = passwordHash;
using (SecurityService service = ServiceProxyFactory.GetSecurityService())
{
user.SecurityKey = service.AuthenticateUser();//这里才是关键,归根到底是调用WebService的
if (null != user.SecurityKey && user.SecurityKey.Length > 0)
{
this._IsSuccess = true;
}
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
//如何利用 Cache Application Block
第一步。。。加入引用
using Microsoft.Practices.EnterpriseLibrary.Caching;
第二步
private const string GENERAL_CACHE_MANAGER = "General Cache"; //这个是为了维护方便
private static CacheManager manager = CacheFactory.GetCacheManager(GENERAL_CACHE_MANAGER); //读取默认的CacheManager,CacheManager内部创建一个Cache对象,Cache内部维护一个Hashtable
public static void StoreInCache( string key, object data )
{
if( null == data ) manager.Remove( key );
else manager.Add( key, data ); ////Add内部有进行线程同步处理;如果Key已经存在,则先删掉再添加;如果容量超过预设值,将有BackgroundScheduler对象执行清理工作(每次清理掉numberToRemoveWhenScavenging个记录)。AbsoluteTime设置绝对过期时间,SlidingTime设置滑动的过期时间(两次被调用之间的时间间隔)。
}
第三步
public static object GetCachedObject( string key ) //读取
{
if( manager.Contains( key ) )
return manager.GetData( key ); ////如果Key不存在或已过期,则返回null
else
return null;
}
cacheManager.Remove(key); //这样子可以删除一个key对应的值