Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.Timers.Timer不工作编辑[在asp.net web窗体中]_C#_Asp.net_System.timers.timer - Fatal编程技术网

C# System.Timers.Timer不工作编辑[在asp.net web窗体中]

C# System.Timers.Timer不工作编辑[在asp.net web窗体中],c#,asp.net,system.timers.timer,C#,Asp.net,System.timers.timer,我正在使用以下代码尝试Timer类:- protected void Page_Load(object sender, EventArgs e) { System.Timers.Timer tm = new System.Timers.Timer(); tm.Elapsed += new System.Timers.ElapsedEventHandler(tm_Elapsed); tm.Interval = 1000; tm.Start(); } void tm

我正在使用以下代码尝试Timer类:-

protected void Page_Load(object sender, EventArgs e)
{
    System.Timers.Timer tm = new System.Timers.Timer();
    tm.Elapsed += new System.Timers.ElapsedEventHandler(tm_Elapsed);
    tm.Interval = 1000;
    tm.Start();
}

void tm_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    int lbl = Convert.ToInt32(Label1.Text);
    Label1.Text = (lbl+1).ToString();
}
最初,Label1.Text是“1”

但是当我运行应用程序时,标签的文本显示为1,并且没有增加。

尝试以下操作:

    void tm_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        Label1.Invoke((Action)(() =>
        {
            int lbl = Convert.ToInt32(Label1.Text);
            Label1.Text = (lbl+1).ToString();
        }));
    }
问题是
System.Timers.Timer
在非UI线程中触发其事件,并且由于无法从非UI线程安全地访问或更新控件,因此它似乎无法工作


在UI元素上调用
.Invoke(…)
,可以将代码推送到UI线程上,使其安全。

System.Timers.Timer用于多线程环境。不允许您直接访问事件内的ui元素

如果您的应用程序是Windows窗体,请使用System.Windows.Forms.Timer。 如果您的应用程序是WPF,请使用System.Windows.Threading.Dispatchermer

如果出于任何原因希望使用System.Timers.Timer,请使用ui元素的Invoke方法。例如,在Windows窗体中:

Label1.Invoke(new Action(() =>
{
    int lbl = Convert.ToInt32(Label1.Text);
    Label1.Text = (lbl+1).ToString();
}));
你可以试试下面的

if(this.Label1.InvokeRequired)
     {
         this.Label1.BeginInVoke((MethodInvoker) delegate() { int lbl = Convert.ToInt32(Label1.Text);
Label1.Text = (lbl+1).ToString();});    
     }
     else
     {
         int lbl = Convert.ToInt32(Label1.Text);
         Label1.Text = (lbl+1).ToString();
     }

问题是,WPF/Windows窗体/Windows 8应用程序/WP 8(.1)应用程序不允许从不同线程更改UI。您需要UI线程来更新它。

正如其他答案中已经提到的,System.Timers.Timer在非GUI线程上启动。这将不允许您访问GUI元素,并将引发跨线程异常。您可以使用MethodInvoker访问
tm\u事件中的GUI元素。因为您有表单中的计时器,并且希望访问GUI元素,所以另一个计时器类最适合您,即

实现以用户定义的间隔引发事件的计时器。 此计时器针对Windows窗体应用程序进行了优化,必须 在窗口中使用

根据OP的评论编辑,按照加载事件的名称,他是在网页非赢表单中这样做的

若您不需要从服务器获取任何信息,可以使用javascript。如果您想要更新html控件,并且需要从服务器进行更新,那么您可以使用

Html(.aspx)

尝试将(线程安全)与@Enigmativity解决方案结合使用,以避免跨线程异常:

    private System.Threading.Timer m_timer = null;
    private const int DUE_TIME = 1000;
    private const int INTERVAL = 1000;

    private void Page_Load(object sender, EventArgs e)
    {
        System.Threading.AutoResetEvent autoEvent = new System.Threading.AutoResetEvent(false);
        System.Threading.TimerCallback tcb = new System.Threading.TimerCallback(timer_Elapsed);
        m_timer = new System.Threading.Timer(tcb, null, DUE_TIME, INTERVAL);
    }

    void timer_Elapsed(object sender)
    {
        label1.Invoke((Action)(() =>
        {
            int lbl = Convert.ToInt32(label1.Text);
            label1.Text = (lbl + 1).ToString();
        }));
    }

这是否在应用程序中?如果您想动态更改页面,则需要编写javascript,而不是在代码后面编写更多代码,因为另一个代码在名为
page\u Load
的方法中运行,我怀疑这是asp.net代码,在这种情况下,没有UI线程,这里存在更大的问题。我得到了以下错误:-“System.Web.UI.WebControls.Label”不包含“Invoke”的定义,并且找不到接受类型为“System.Web.UI.WebControls.Label”的第一个参数的扩展方法“Invoke”(是否缺少using指令或程序集引用?)@SohamBanerjee-抱歉,我假设这是Windows窗体。你需要编写javascript来更新网页上的UI。不是c#不允许。是Windows窗体/WPF不允许。哦……它在Windows 8和wp应用程序上也不起作用。所以,基本上,我在c#中工作过的任何东西都不允许我更新UI。这让我很生气我认为这是处理用户界面的c标准方式。感谢您的关注。相应地更新aanswer:)“页面加载”->这是asp.net
  <form id="form1" runat="server">             
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <asp:Timer runat="server" id="UpdateTimer" interval="5000" ontick="UpdateTimer_Tick" />
        <asp:UpdatePanel runat="server" id="TimedPanel" updatemode="Conditional">
            <Triggers>
                <asp:AsyncPostBackTrigger controlid="UpdateTimer" eventname="Tick" />
            </Triggers>
            <ContentTemplate>
                 <asp:Label id="Label1" runat="server" Text="1" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </form>
protected void UpdateTimer_Tick(object sender, EventArgs e)
{
    Label1.Text = int.Parse(Label1.Text) + 1;
}
    private System.Threading.Timer m_timer = null;
    private const int DUE_TIME = 1000;
    private const int INTERVAL = 1000;

    private void Page_Load(object sender, EventArgs e)
    {
        System.Threading.AutoResetEvent autoEvent = new System.Threading.AutoResetEvent(false);
        System.Threading.TimerCallback tcb = new System.Threading.TimerCallback(timer_Elapsed);
        m_timer = new System.Threading.Timer(tcb, null, DUE_TIME, INTERVAL);
    }

    void timer_Elapsed(object sender)
    {
        label1.Invoke((Action)(() =>
        {
            int lbl = Convert.ToInt32(label1.Text);
            label1.Text = (lbl + 1).ToString();
        }));
    }