parralel ASP.NET Timer Controls - asp.net

I have a problem and I'd really appreciate a little help.
On my .aspx I have four ASP.NET Timer Controls and when each of them Ticks I call asynchronously different web service method that returns me some photos. I have a image in Update Panel and update it src when I get a photo. I'm using a jQuery lightBox plugin to preview a image.
The problem is that: I would like the Timers work in the same time (simultaneously). So on page Load, after some seconds should appear a Tick of each Timers (parallel, more less in the same time). Both Timers have the same time Interval so it should work, but in practice I've noticed that the second Timer ticks only after the first web service callback appears, and the third Timer ticks after the second web service callback appears. So it all works linearly, not parallel.
Any solutions how to modify it to Tick simultaneously ?
my .aspx:
<center>
<asp:Timer runat="server" ID="UpdateTimer1" OnTick="UpdateTimer1_Tick" />
<asp:UpdatePanel runat="server" ID="TimedPanel2" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="UpdateTimer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:Literal runat="server" ID="Ltr1" />
<div id="div1" runat="server">
<img alt="" id="img1" runat="server" style="width: 85%; height: 85%" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
</center>
one of my Timers in .aspx.cs, the rest of Timers is similar - the call different webservice, but have the same events, updateTimer_Ticks etc:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
UpdateTimer1.Interval = 3000;
{
}
protected void UpdateTimer1_Tick(object sender, EventArgs e)
{
AsynchGetPhoto();
}
public void AsynchGetPhoto()
{
WS.Service Service = new WS.Service();
Service.getPhotoCompleted += new Service.getPhotoCompletedEventHandler(PhotoCompleted);
Service.getPhotoAsync();
}
public void PhotoCompleted(object sender, WS.Service.getPhotoCompletedEventArgs args)
{
img1.Attributes.Add("src", "data:image/jpg;base64," + args.Result);
}

What i can tell you about your problem is my understanding about how this timer is working and happens behind the scene and you can investigate more in your code.
When the timers tick an asynchronous request to your function is done so each timer shall fire it's own request and the browser shall handle all the requests in parallel so i suggest that you open the firebug and see your timers requests and see if they are depending on each other and each request is handled after the other or it is just the rendering mechanism of the browser causing the timers to delay.

Related

private static variable is replace with latest value when accessing from different browser - c#

My web page is showing the details of selected student(using the studentid) in a gridview. I have put an updatepanel with timer for this gridview to auto refresh the data within an interval of time. But now the issue is when a user1 is seeing the details of student1 from his system. and another user2 is seeing the details of student2 from his system. when the timer executes the updatepanel , both the user is seeing the latest student (student2) details.
In short, when same web page is used by multiple users at same time, it shows the student details of latest user selected.
I have set the student id as private static. Will it be individual for each browser? Or will it be the issue of updatepanel with timer?
<asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional">
<ContentTemplate>
<asp:Timer ID="Timer2" runat="server" Interval= "<%$appSettings:update_timer%>" OnTick="Timer1_Tick"></asp:Timer>
<asp:GridView ID="gv_studentdetails" runat="server" AutoGenerateColumns="true" Caption="Student Details"
CssClass="gridview_alter" >
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer2" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
In web.config file
<add key="update_timer" value="30000"/>
Code behind:
private static int student_id;
if (!IsPostBack)
{
student_id=Convert.ToInt32(Session["selected_studentID"]);
BindGridview(student_id);
}
protected async void Timer1_Tick(object sender, EventArgs e)
{
BindGridview(student_id);
UpdatePanel1A.Update();
}
NB: This issue is happening when run the timer of updatepanel only. When manually reload the page it is not .
private static int student_id;
The value will be the same within a single web-server. No matter the browser sessions. That is why both browsers start to see the last value for the server.
But whenever someone reloads the page it will set the private field to their won session's selected_studentID. So then all the browsers will get that value evenutally.
if (!IsPostBack)
{
student_id=Convert.ToInt32(Session["selected_studentID"]);
BindGridview(student_id);
}
Note:
Manipulating a static instance without lock, may cause an issue. You can read more here c# critical sections, lock

Anyway to wire a button without an updatepanel?

When dealing with update panels I normally am dealing with a single control and handle everything else through JavaScript.
<asp:UpdatePanel ID="updatepanel1" childrenastriggers="false" runat="server">
<ContentTemplate>
<asp:button id="button1" runat="server" onclick="update()" />
</ContentTemplate>
</asp:UpdatePanel>
This seems like too much code for a simple postback. If I remember right there's a way to just wire up the button to do the postback without requiring an updatepanel. I still want access to scriptmanager to call a JS function once the update is complete. Any ideas on a cleaner solution?
What are you trying to do here.. You don't need a updatepanel to trigger a button click event .. Just the button is enough..
Do you want to handle server side or client side code on the click of the button.. Also you are not closing the button tag properly.. It should be like this
OnClick is the server side event and OnClientClick is the client click event
// This is the server side code that has to be written
void Page_Load(Object sender, EventArgs e)
{
}
void Btn_Click(Object sender, EventArgs e)
{
}

cant update label using timer

i have a master page which uses script manager and on another page i want to show a digital clock which counts from 0 to 10 minutes.
asp.net code:
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
</asp:ScriptManagerProxy>
<asp:Timer ID="Timer1" runat="server" Interval="5000" ontick="Timer1_Tick">
</asp:Timer>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
C# code:
int min=0,sec=0;
protected void Timer1_Tick(object sender, EventArgs e)
{
if (sec == 55)
{
min = min + 1;
sec = 0;
}
else
{
sec = sec + 5;
}
Label1.Text = min.ToString() +":"+ sec.ToString();
if(min==10&&sec==0)
{
Timer1.Enabled=false;
Label1.Text = "clock over!";
}
the page starts with 0:5 and doesn't change at all. pls point out if there is an error in d code!
My bet is that it will be your update panel. For debug purposes
Try without the upadte panel at all - does it work then
Put the timer in the update panel and forget about the trigger
In fact point 2 could be done anyway - I'm not sure of the benefit of having it as a postback trigger. It would do just as well with the timer inside
However
Honestly I wouldn't do this. Won't the Tick method perform a postback (albeit partial) just to get the timer display going.
There are lots of freely available components that will run purely on the client side which will be a lot (lot) more efficient e.g.
JQuery Timer
EDIT
Actually I think I've spotted the problem. It is how you record mins and secs. These are in variables and would be reset every postback (Timer tick). Youu would need to store them into something that would perist postback e.g. ViewState
protected void Timer1_Tick(object sender, EventArgs e) {
ViewState["min"] //not int min
ViewState["sec"] //not int sec
//.. you would need to use Convert (or Int32.Parse) to get
//.. then as an int to work with
}
But homestly - still don't do it. Use the JQuery component or similar

Need some idea about upload a file using ajax

i want to upload an image on my host in my aspx page.but can i do that with ajax?(i mean,no postback done during the upload).
i look at the gmail file uploader,the classic mode used flax(this is bad idea) but i can understand about the modern mode?!!
according to my search in Google it is impossible for security reasons may be occure
some body help me about this issue."upload a file using ajax"
you can do like this.. using ajax .....
STEP 1: first create an upload form in separate page which is like this (myIframe.html)
<form method="POST" target="_self" enctype="multipart/form-data" action="main.aspx">
<input type="file" name="fileUpload" />
<input type="submit" value="UPLOAD" />
</form>
STEP 2: In main page (main.aspx), you have to put an iframe and loaded that page in it. you can also made borders invisible so it did not look like it is an iframe.
<iframe name="iUploadFrame" src="myIframe.html" frameborder="0" onload="iUploadFrameLoad();"></iframe>
Note that this is specifying onload event handler function of javascript. that one will explain it below.
STEP 3: For testing, in main.aspx and in UpdatePanel with a button and label like this:
<asp:UpdatePanel ID="pnlMain" runat="server">
<ContentTemplate>
<asp:Label id="lblMessage" runat="server" />
<asp:Button id="btnUploadComplted" runat="server" style="visibility:hidden;" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnUploadCompleted" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
Note that if you want you can hide the button using CSS style, but not setting visible=”False” attribute. This is because if you want to have the button html in the output document and would want to trigger the ajax callback via that button.
STEP 4: you can define the click event hander of the button in the codebehind like this :
protected void btnUploadCompleted_Click(object s, EventArgs e) {
lblMessage.Text = "UPLOAD PROCESS COMPLETED SUCCESSFULLY";
}
STEP :5 Now, to save file you can do write the code in the code-behind like this:
protected void Page_Load(object s, EventArgs e) {
if ((Request.Files.Count == 1)) {
// process to save file
Response.Write("SUCCESS");
Response.End();
}
}
STEP:6 Now all that we need to do is create javascript function to integrate the process into ajax implementation. The event will trigger when upload process is complete, and it will, in turn, triggers btnUploadCompleted’s click event, which is responsible for making our ajax call.
<script type="text/javascript">
function iUploadFrameLoad() {
if (window.iUploadFrame.document.body.innerHTML == "SUCCESS") {
document.forms[0].elements[btnUploadCompleted].click();
}
}
</script>
I hope it will helps you...

asp.net update UI using multi-thread

I have an ASP.NET website containing the following
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
I have created a thread function. And during the execution of this function I would like to update some controls on the user interface.
protected void Page_Load(object sender, EventArgs e)
{
new Thread(new ThreadStart(Serialize_Click)).Start();
}
protected void Serialize_Click()
{
for (int i = 1; i < 10; i++)
{
Label1.Text = Convert.ToString(i);
UpdatePanel1.Update();
System.Threading.Thread.Sleep(1000);
}
}
How could I update a web-control during a thread-execution? do I need to force the "UpdatePanel1" to post-back? how?
You'll need to use a client-side timer (or some other method) to have the browser ask the server for the update, such as this simplified example:
<asp:UpdatePanel ID="up" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="timer_Ticked" />
<asp:Label ID="Label1" runat="server" Text="1" />
</ContentTemplate>
</asp:UpdatePanel>
Then in your codebehind:
protected void timer_Ticked(object sender, EventArgs e)
{
Label1.Text = (int.Parse(Label1.Text) + 1).ToString();
}
If you have a background process that is updating some state, you will either need to store the shared state in the session, http cache, or a database. Note that the cache can expire due to many factors, and background threads can be killed any any tie if IIS recycles the application pool.
This won't do what you think it will.
It would make more sense to use a js timer on the front end to call a webmethod that updates the page.
Remember that to the server, once the page is rendered to you, it does not exist anymore until called again. The web is a stateless medium. Postback and viewstate make it feel like it's not, but it really is.
As such, you won't be able to call the client from the server, which is what you are attempting to do.
You would have to use javascript/jquery to poll the server for changes. This is one of the more unfortunate side-effects of all this fancy-schmancy jquery/ajax stuff: people forget that the browser must poll the web server - it's a one way connection only, even if it does sometimes feel like a client winforms app. Ultimately, the browser must make a http post/get request of its own accord. Yes, you can emit client javascript ahead of time to tell it to do so, but this requires that you know when exactly to call back. It sounds like you have some arbitrary code that will be done when it's done, so in this case you must continually poll.
-Oisin

Resources