线程工作过程给出问题.. [英] Threading work process giving issue..

查看:43
本文介绍了线程工作过程给出问题..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,



我的要求很简单,我需要在我的网络应用程序上使用进度条在Dot Net framework 2.0中开发。



我尝试使用更新面板但没有用。



所以我按照我的逻辑尝试,我创建了一个web表单添加一个服务器端按钮控件,一个服务器端DIV控件,具有服务器端图像控制一个gif图像,作为我的应用程序和服务器端计时器控件的进度条图像。



现在我需要在一个单独的事件中完成两项工作,所以我在服务器端代码中包含了线程。



当我点击Button时它启动Thread调用我的程序并启动我的计时器,同时我将我的DIV控件可见属性设置为true一旦程序完成它返回一些输出,我存储在表单级变量_msg。在每个时间段之后我的计时器检查我的_ msg变量,如果它填充了一些数据我的计时器停止线程并将DIV控制可见属性更改为假。



所有工作正常但我的计时器无法成功将我的DIV可见性更改为false。



我的网页表格如下



Dear Friends,

My requirement is simple, I need progress bar on my Web Application develop in Dot Net framework 2.0.

I have try using Update Panel but no use.

So i try according to my Logic, I have create one web form add one Server Side 'Button' Control, One Server Side 'DIV' control having Server Side 'Image' Control one gif Image which is work as Progress Bar image for my application and server side timer control.

Now i need two work in one single event, so i include Threading in my Server Side code.

When i click Button it start Thread which call my procedure and start my timer and same time i set my DIV control visible property to "true" once the procedure complete it return some output which i store in form level variable "_msg". after every time period my timer check my "_msg" variable if it fill with some data my timer stop thread and change DIV control visible property to false.

All work fine but my timer can not success to change my DIV visibility to false.

My Web Form as below

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" 
                    Text="Start Long Task!" />
             <br />
        <br />
        <div id="ProgressBar"  runat="server" visible="false">
            <asp:Image ID="Image1" runat="server" Height="100px" 
            ImageUrl="~/media/loading.gif" Width="99px" />
        </div>    
    </div>
    </form>
</body>
</html>





我的服务器端代码如下





My Server Side Code as below

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Threading;
using System.Text;

public partial class Default5 : System.Web.UI.Page
{
    object _Msg;
    Control MyControl = new Control();
    Thread thread;
    private System.Timers.Timer aTimer = new System.Timers.Timer(1000);

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private void LongTask()
    {
        using (SqlConnection _cn = new SqlConnection("Data Source=MyServer;Initial Catalog=MyDatabase;User ID=sa;Password=abc@123"))
        {
            _cn.Open();
            using (SqlCommand _cmd = new SqlCommand("WAITFOR DELAY '00:00:10'; select 'Hi'", _cn))
            {
                _Msg = _cmd.ExecuteScalar();
            }
        }

        
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        aTimer.Enabled = true;
        aTimer.Elapsed += new System.Timers.ElapsedEventHandler(aTimer_Elapsed);

        _Msg = "";
        OpenProgressBar(this.Page);
        thread = new Thread(new ThreadStart(LongTask));
        thread.Start();

    }

    void aTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        if (_Msg.ToString().Length > 0)
        {
            thread.Abort();

            aTimer.Enabled = false;
            aTimer.Dispose();

            Control MyControl = Page.FindControl("ProgressBar");

            MyControl.Visible = false;
        }
    }

    public static void OpenProgressBar(System.Web.UI.Page Page)
    {
        Control MyControl = Page.FindControl("ProgressBar");

        MyControl.Visible = true;
    }
}

推荐答案

我建​​议您使用BackgroundWorker更新进度条状态。 http://msdn.microsoft.com/en-us /library/system.componentmodel.backgroundworker(v=vs.110).aspx [ ^ ]
I suggest you can use BackgroundWorker to update your progressbar status. http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker(v=vs.110).aspx[^]


我建​​议您使用BackgroundWorker更新进度条状态。 http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker(v = vs.110).aspx
I suggest you can use BackgroundWorker to update your progressbar status. http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker(v=vs.110).aspx


谢谢肖玲..



我已经使用了Ajax扩展的Update_Progress和Update_Panel以及web配置文件中的一些更改(从Google获取)它现在正在工作......



感谢您的帮助。
Thanks Xiao Ling..

I have use Update_Progress and Update_Panel of Ajax Extension with some changes in web config file (Get from Google) its working now...

Thanks for your help.


这篇关于线程工作过程给出问题..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆