Web Form crashing with more than 1 timer - c#

Issue
I have a asp.net webForm that has several timers. When all the timers are active and running, the webform will "stop" running at exactly 82 seconds. There is no crash redirect or error, but if I look at the Dev Console it shows lots of 500 errors (screen shot below).
Other Information
The application has a total of 13 timers. 1 timer is just a clock, and ticks every 1 second (this is how I can see exactly when the page stops). The other 12 timers are panels that pull information on each tick. The ticks of these panels ranges from 5 minutes to 15 minutes.
When debugging/running the application from Visual Studios, it never stops or crashes, its when I publish it to an IIS Webserver that is has the problem.
I have removed all timers except the clock, and that fixes the issue, so it only seems to happen when there is more than 1 timer.
Single Timer Code
<asp:Timer runat="server" id="clockTimer" interval="1000" ontick="clockTimer_Tick" />
<asp:UpdatePanel runat="server" id="UpdatePanel2" updatemode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger controlid="clockTimer" eventname="Tick" />
</Triggers>
protected void clockTimer_Tick(object sender, EventArgs e)
{
clockLabel.Text = DateTime.Now.ToString();
}
Example of more than 1 timer code
<asp:Timer runat="server" id="clockTimer" interval="1000" ontick="clockTimer_Tick" />
<asp:UpdatePanel runat="server" id="UpdatePanel2" updatemode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger controlid="clockTimer" eventname="Tick" />
</Triggers>
protected void clockTimer_Tick(object sender, EventArgs e)
{
clockLabel.Text = DateTime.Now.ToString();
}
<asp:Timer runat="server" id="priTimer" interval="300000" ontick="priTimer_Tick" />
<asp:UpdatePanel runat="server" id="TimedPanel" updatemode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger controlid="priTimer" eventname="Tick" />
</Triggers>
<ContentTemplate>
<div class="card border-light mb-3">
<div class="card-header" style="font-size: 1vw; background-color:grey; color:white;" id="priHead">Primary <asp:Label ID="priBadge" runat="server"></asp:Label></div>
<asp:ListBox ID="priList" runat="server" style="font-size: 1vw; OVERFLOW:auto; width:100%"></asp:ListBox>
<div class="card-footer" style="font-size: .25vw; background-color:grey; color:white; max-height:30px;" id="priFoot"><asp:Label ID="priLastfire" runat="server" class="font-italic; text-light;" style="font-size: .5vw;" Text=""></asp:Label></div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
protected void priTimer_Tick(object sender, EventArgs e)
{
priList.Items.Clear();
try
{
string[] priRead = System.IO.File.ReadAllLines(#"D:\Data_Scripts\Primary\priDown.txt");
foreach (string line in priRead)
{
priList.Items.Add(line);
}
priBadge.Text = priList.Items.Count.ToString();
}
catch
{
priList.Items.Add("Error pulling data");
priBadge.Text = "NA";
}
DateTime nextFire = DateTime.Now.AddMilliseconds(priTimer.Interval);
priLastfire.Text = "Last Fire: " + DateTime.Now.ToShortTimeString() + " ----- Next Fire: " + nextFire.ToShortTimeString();
priColor();
}
Screenshot of the 500 error(s)
Thanks in advance - I tried to add as much information and make it as clear as possible.

Related

Why my Drop down list is not getting binded upon button click?

i am trying to rebind my Dropdown upon button click, in particular if Block but it doesn't rebind, i mean that it should reload the drop down, kinda refresh and first item should be selected, i am doing this but not getting rebinded
Code:(It si inside buttong click event)
if (NotAssignedConductors.Length > 0)
{
string[] NotAssignedConductorsArray = NotAssignedConductors.Split(':');
foreach (string str in NotAssignedConductorsArray)
{
ResultLabel.ResultLabelAttributes(str, ProjectUserControls.Enums.ResultLabel_Color.Red);
}
FillDropDownListDevices();
}
Method being called:
public void FillDropDownListDevices()
{
DropDownListDevices.DataSource = ManageTransport.ManageConductorDevices.GetDevices();
DropDownListDevices.DataTextField = "TerminalSNO";
DropDownListDevices.DataValueField = "DeviceID";
DropDownListDevices.DataBind();
DropDownListDevices.Items.Insert(0, new ListItem("None", "-1"));
}
Button:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownListDevices" AutoPostBack="true" runat="server" OnSelectedIndexChanged="DropDownListDevices_SelectedIndexChanged" CssClass="form-control">
</asp:DropDownList>
<ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnCreate" EventName="click" />
</Triggers>
</asp:updatePanel>
There are many possibilities
If you are using update panel make sure you trigger update-panel to update on button click
check your page_load/page_loadComplete event and make sure you are not binding drop-down again .
check your browser console it throws any error on button click?
Create Post back trigger on update panel
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownListDevices" AutoPostBack="true" runat="server" OnSelectedIndexChanged="DropDownListDevices_SelectedIndexChanged" CssClass="form-control">
</asp:DropDownList>
<ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
</asp:updatePanel>
Put your first time binding method on Postback
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//means no postback..page loaded first time
// Put your first time Binding method here
}
}

Update panel from a server side thread

I am trying to develop a chat using aspx. I already got it working with asmx + winform but now I have problems with asmx + aspx.
Basically what I want to do is call the WebMethod CheckForMessages every two seconds and, if there are messages, add them to a list box inside an update panel and do UpdatePanel1.Update();.
The problem is that apparently I can' t do this using a thread like I do in my winform.
void check() {
while (true) {
Thread.Sleep(2000);
string message = CheckForMessages();
if (message != "") {
ListBox1.Items.Add(message);
UpdatePanel1.Update();
}
}
}
I start the thread like this:
protected void Page_Load(object sender, EventArgs e) {
timer = new Thread(new ThreadStart(check));
timer.Start();
}
It doesn't throw an exception or anything, the program runs as intended. The web service is called, a string is returned if there is a message, the thread adds the message to the list and calls UpdatePanel1.Update();. Yet the panel is not updated.
What could the problem be ?
In ASP.Net you can use timer control along with updatepanel.
<asp:UpdatePanel runat="server" ID="uxUpdatePanel" UpdateMode="Conditional" EnableViewState="true">
<ContentTemplate>
<div>
<asp:Label ID="Label1" runat="server" style="display:none;"><%=GetMessageLog()%></asp:Label>
<asp:ListBox runat="server" ID="ListBox1" EnableViewState="true">
</asp:ListBox>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="uxTimer" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:Timer runat="server" ID="uxTimer" Interval="2000">
</asp:Timer>
And in Code-behind you to code like:
public string GetMessageLog()
{
string message = CheckForMessages();
if (message != "") {
ListBox1.Items.Add(message);
return DateTime.Now.ToLongTimeString();
}
This should work for you. But I will recommend you to use Javascript and JQuery to call Web-Service asynchronously with setTimeout function.
Instead of the server actively pushing changes to the client, try the opposite : make the client actively query the server, and update its state accordingly.
One way to do that is create a button to query the server manually. After you verify that it works, make the button hidden using css, and set a function to click the button in a regular interval. You can use javascript setInterval for this.
To click a button in javascript, you can do something like this :
setInterval(function(){ document.getElmtById(“<%= theButton.ClientID %>").click(); }, 2000);
The above code should click theButton every 2 seconds.
You should have a look at SignalR.net. Jabbr.net is based on signalR and exactly what you need. https://github.com/davidfowl/JabbR
yes Vishal... maybe less complex:
<asp:UpdatePanel ID="updatePanelPromptB" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="labelPlayback" runat="server"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="timerPromptB" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:Timer runat="server" ID="timerPromptB" OnTick="timerPromptB_Tick" Enabled="false" Interval="4000" />
private void promptBottom(string text)
{
labelPlayback.Text = text;
updatePanelPromptB.Update();
timerPromptB.Enabled = true;
}
protected void timerPromptB_Tick(object sender, EventArgs e)
{
labelPlayback.Text = "OK";
timerPromptB.Enabled = false;
}

unable to solve Timer_Tick

i have same problem as this question.se the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation i havent any problem with <%# Page EnableEventValidation="false" %>.I solve it and i try
protected void Timer1_Tick(object sender, EventArgs e)
{
Label2.Text = Convert.ToString((Convert.ToInt32(Label2.Text) - 1));
if (Convert.ToInt32(Label2.Text) == 0)
{
Timer1.Dispose();
Submit();
}
}
Code work fine means submit () is work if i call from submit button.If its call from Timer_Tick its not work.And timer is not stop or dispose.What is a problem plz suggest?
timer:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick">
</asp:Timer>
<asp:Label ID="Label1" runat="server" Text="Remaining Time:(Min)"></asp:Label>
<asp:Label ID="Label2" runat="server" Text="100"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
where are you Enabling or Starting the timer on the page? The timer will not automatically start upon page load, you will need to explicitly call Start() or set Enabled = true
Label2.Text = Convert.ToString((Convert.ToInt32(Label2.Text) - 1));
Does that line works?
Maybe the if
Convert.ToInt32(Label2.Text) == 0
doesn't return true

Using a Timer post back, which controller to use that doesn't post back to server?

Being new to ASP.NET I have run into trouble building my own Whack-a-mole program. I think my problem comes from using Buttons, which by themselves send post backs to the server, making the software unusable. The looks are in place, making new buttons show up in the grid, in different places by random. However, when a button is pushed – the score doesn’t change (which I feel is strange).
Not so strange is that the Button doesn’t work since it sends post back to the server – reloading the UpdatePanel. I think I should use a different controller like the CheckBox and style it hard using CSS (which isn’t a problem). Is this the correct way to go, or should I make use of JavaScript AJAX instead?
Note to self: This technique shouldn’t be used in a public application since it put too much unwanted pressure on the web server. Use in test and learning only!
Thanx for listening!
<%# Page Language="C#" MasterPageFile="~/MasterPage.master"
AutoEventWireup="true" CodeFile="Whack-a-mole.aspx.cs"
Inherits="Whack_a_mole" %>
<asp:Content ID="Content" ContentPlaceHolderID="cphContent" runat="server">
<h1>Whack-a-Mole</h1>
<asp:ScriptManager ID="smgrTime" runat="server" />
<asp:Timer ID="Timer" OnTick="Timer_Tick" runat="server" Interval="2000" />
<asp:UpdatePanel ID="updpButtons" runat="server" UpdateMode="Always">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer" />
</Triggers>
<ContentTemplate>
<asp:Label ID="lblPoints" runat="server" Text="Score: 0p" />
<asp:Button ID="Button1" runat="server" BorderStyle="Outset"
BorderWidth="3px" CssClass="mole" Text="Mole"
onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" BorderStyle="Outset"
BorderWidth="3px" CssClass="mole" Text="Mole"
onclick="Button2_Click" />
<asp:Button ID="Button3" runat="server" BorderStyle="Outset"
BorderWidth="3px" CssClass="mole" Text="Mole"
onclick="Button3_Click" />
<!-- continues the same way down to Button25 -->
<asp:Button ID="Button25" runat="server" BorderStyle="Outset"
BorderWidth="3px" CssClass="mole" Text="Mole"
onclick="Button25_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Excerpt from the Code file:
// points vairable
private int points;
// Property for points
public int Points
{
get { return points; }
set { points = value; }
}
protected void Timer_Tick(object sender, EventArgs e)
{
Random random = new Random();
int visible;
foreach (Button button in buttonList)
{
visible = random.Next(1, 10);
if (visible == 3)
button.Visible = true;
else
button.Visible = false;
}
lblPoints.Text = "Score: " + Points + " p";
}
protected void Button1_Click(object sender, EventArgs e)
{
Points = Points + 1;
}
Where is your Points variable declared?
I think it might be a member of the Whack_a_mole WebForm class and that is why it doesn't update. The instance of the WebForm is only used for 1 Postback, and then it is destroyed.
But more important, for a game like this you should use JavaScript or SilverLight.

Asp.net Timer resets on page refresh

I'm using a simple asp.net timer with this code as a backend
protected void Timer1_Tick(object sender, EventArgs e)
{
int seconds = int.Parse(Label1.Text);
if (seconds > 0)
Label1.Text = (seconds - 1).ToString();
else
Timer1.Enabled = false;
}
The timer is grouped along with a Label and a Button inside a UpdatePanel in my front-end.
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Image ID="RedBox" runat="server" ImageUrl="~/images/redbox.png" Visible="false" CssClass="redbox1" ClientIDMode="Static" />
<asp:Button ID="schlbtn1" runat="server" Text="Go To Lectures" CssClass="btn btn-lg btn-success btn-block" OnClick="schlbtn1_Click" Visible="true" ForeColor="Black" ViewStateMode="Enabled" ClientIDMode="Static" />
<asp:Label ID="Label1" runat="server" Visible="false" CssClass="timerlbl" Font-Size="XX-Large">60</asp:Label>
<asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick" Enabled="false">
</asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
I'm looking for a way to make the timer NOT to reset on every page refresh. I need the timer to still count down until it reach 0 even if the client is not on the same page. I tried Sleep methods but think this is not the solution. Please make any kind of suggestions.
Store the timeout in the session:
Add to page load:
if (Session["timer"] == null)
{
InitTimer(120); // or whatever initial value is
}
Add method:
public void InitTimer(int secs)
{
Session.Add("timer", secs);
Timer1.Enabled = true;
}
Change tick method:
protected void Timer1_Tick(object sender, EventArgs e)
{
int seconds = (int)Session("timer");
if (seconds > 0)
{
seconds--;
Session["timer"] = seconds;
Label1.Text = seconds.ToString();
}
else
Timer1.Enabled = false;
}

Categories