.net阅读心得(一)

来源:互联网 发布:淘宝装修市场网址 编辑:程序博客网 时间:2024/05/16 17:34

 1)怎么样写一个retry机制

 

class retryclass
{
public delegate void RetryMethod();

public void retry(int retrytime,TimeSpan interval,bool throwiffall,RetryMethod function)
{
if(function!=null)
{
for(int i=0;i<retrytime;++i)
{
try
{
function();
break;
}

catch(Exception e)
{
if(i==retrytime-1)
{
if(throwiffall)
{
throw;
}

else
{
break;
}

}

else
{
if(intelval!=null)
{
System.Theading.Thead.sleep(intelval);
}

}

}

}

}

}


public void newadd()
{
retry(
10,TimeSpan.FormSeconds(10),true,delegate 
{
//代码区
}

)
}


}

 

2)进程的安全访问机制

 

class ThreadSafe
{
public delegate void ThreadMethod();

public void dothreadsafe(Control con,ThreadMethod function)
{
if(function!=null)
{
if (control.InvokeRequired)
 
{
 control.Invoke(function); 
 }

 
else
 
{
 function(); 
 }


}

}


public void testthreadsage(int value)
{
dothreadsafe(TextBox1,
delegate {TextBox1.value=value;})
}

}