• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Asp.Net多线程小实验

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
aspx页面
<form id="form1" runat="server">
    
<div>
        
<asp:ScriptManager ID="ScriptManager1" runat="server">
        
</asp:ScriptManager>
    
    
</div>
        
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
            
<ContentTemplate>
        
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="执行耗时操作" /><br />
        
&nbsp;&nbsp;
                
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Ping" />
                
<asp:Timer ID="Timer1" runat="server" Enabled="False" Interval="1000" OnTick="Timer1_Tick">
                
</asp:Timer>
                
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            
</ContentTemplate>
        
</asp:UpdatePanel>
        
<input id="Text1" type="text" />
    
</form>

 

public partial class ThreadTest : System.Web.UI.Page
{
    public static int Counter = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Counter = 0;
            Timer1.Enabled = false;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Timer1.Enabled = true;
        DateTime dtStart = DateTime.Now;

        NewThreadWork newThreadWork = new NewThreadWork();
        newThreadWork.ThreadStart += new System.Threading.ThreadStart(LongTimeOpt);
        newThreadWork.Run();

        TimeSpan ts = DateTime.Now - dtStart;
        this.TextBox1.Text = ts.Milliseconds.ToString();
    }


    private void LongTimeOpt()
    {
        int totle = 100000000;
        for (int i = 0; i < totle; i++)
        {
            for (int j = 0; j < totle; j++)
                Counter = i;
        }
    }

  
    protected void Button2_Click(object sender, EventArgs e)
    {
        this.TextBox2.Text = Counter.ToString();
    }

    public class NewThreadWork
    {
        private static object obj = new object();
        public System.Threading.ThreadStart ThreadStart;

        public void Run()
        {
            lock (obj)
            {
                System.Threading.Thread thread = new System.Threading.Thread(ThreadStart);
                thread.Start();
            }
        }
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        this.TextBox2.Text = Counter.ToString();
    }
}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap