我想在不暂停的情况下启动程序。例如
private void button1_Click(object sender, EventArgs e)
{
test();
}
protected void test()
{
label1.Text = "";
for (int i = 0; i < 10; i++)
{
label1.Text += i;
Thread.Sleep(500);
}
}
当我运行该程序时,表单将在循环结束时挂起。
如何使函数test()
在不暂停表单的情况下运行,例如作为后台运行?
答案 0 :(得分:3)
您可以使用Asynchronous programming,因此使您的方法async
像这样:
protected async Task test()
{
label1.Text = "";
for (int i = 0; i < 10; i++)
{
label1.Text += i;
await Task.Delay(500);
}
}
最好一直使用它:
private async void button1_Click(object sender, EventArgs e)
{
await test();
}
答案 1 :(得分:1)
尝试异步等待方法(我也将[main]
sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManager
;cacheManager = org.apache.shiro.cache.MemoryConstrainedCacheManager
cacheManager = se.cehis.sob.cache.EfsobCacheManager
sessionDAO = se.cehis.efsob.web.authorization.shiro.CustomShiroSessionDAO
securityManager.sessionManager = $sessionManager
securityManager.sessionManager.sessionIdCookie.name = e_session_id
securityManager.sessionManager.sessionIdCookie.path = /
;securityManager.sessionManager.sessionIdCookie.secure = true
securityManager.cacheManager = $cacheManager
securityManager.sessionManager.sessionDAO = $sessionDAO
;jdbcRealm=se.cehis.efsob.common.security.JdbcRealmImpl
jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm
jdbcRealm.authenticationQuery = SELECT ID FROM ACL_USER WHERE ID = ?
jdbcRealm.userRolesQuery = SELECT ROLE_NAME FROM ACL_ROLES where USER_ID = ?
jdbcRealm.permissionsQuery = SELECT PERMISSION_NAME FROM ACL_PERMISSION WHERE ROLE_NAME = ?
jdbcRealm.permissionsLookupEnabled = true
ds = org.apache.shiro.jndi.JndiObjectFactory
ds.resourceName = java:jboss/datasources/efobDS
jdbcRealm.dataSource= $ds
替换为Thread.Sleep
):
Task.Delay
如果您要执行大量CPU工作,则应执行以下操作:private async void button1_Click(object sender, EventArgs e)
{
await test();
}
protected async Task test()
{
label1.Text = "";
for (int i=0;i<10;i++)
{
label1.Text += i;
await Task.Delay(TimeSpan.FromMilliseconds(500)); //Thread.Sleep(500);
}
}