Answered by:
Populating and Comparing an Array to another Array created a Timer interval apart from each other

Question
-
User1613081147 posted
Mission: Compare array (populated from dataset) every 5 seconds to the previous array.
Method: I'm using a ASP Timer.
Problem: array1 (SUPPOSED TO BE populated 10 seconds earlier than array2 BUT IS REPOPULATED EVERY TIME ARRAY2 IS) and array2 (populated 10 seconds after array1) both change/repopulate every 10 seconds. So, array1 and array2 are always identical.
Question: How can I keep array1 from changing? OR- How would you achieve the same "Mission" that I am?
<asp:ScriptManager runat="server" id="ScriptManager1" /> <asp:UpdatePanel runat="server" id="UpdateCams" UpdateMode="Conditional" ChildrenAsTriggers="false"> <ContentTemplate> <asp:Timer runat="server" id="Timer1" Interval="5000" OnTick="Timer1_Tick" ></asp:Timer> <asp:Label id="Label1" runat="server"></asp:Label> </ContentTemplate> </asp:UpdatePanel>
Sub Timer2_Tick(ByVal sender As Object, ByVal e As EventArgs) Dim dsArray As DataSet dsArray = fillDataset("SELECT machine_id FROM [machines] where machine_type ='" & Request.QueryString("machtype") & "'", connstring, False) Dim array2(dsArray.Tables(0).Rows.Count) As String For x As Integer = 0 To dsAfterArray.Tables(0).Rows.Count - 1 array2(x) = dsAfterArray.Tables(0).Rows(x).Item("active_override") Next For y=0 to dsArray.Tables(0).Rows.Count - 1 If array2(y) <> array1(y) Then 'Problem is that array2 is always equal to array1 UpdateCams.Update() 'Update only when array1 element is not equal to an array2 element End If End Sub
I am populating array1 like this and have tried calling LoadArray1() all over the ASP page. No matter where I call it, it re-calls LoadArray1() with every 5 second time interval.
Sub LoadArray1() Dim j As Integer = 0 Dim dsBeforeArray As DataSet dsBeforeArray = fillDataset("SELECT machine_id FROM machines WHERE mach_type='" & Request.QueryString("machtype") & "'", connstring, False) Dim row As DataRow For Each row In dsBeforeArray.Tables(0).Rows array1(j) = row.Item("machine_id") j = j + 1 Next End Sub
Thursday, February 12, 2015 10:22 AM
Answers
-
User-760709272 posted
You haven't shown where you are calling LoadArray1 so it's hard to give an answer.
Forget the updatepanel though, it makes no difference to the page lifecycle, everything is getting created then destroyed each 5 seconds. With the update panel it is no different to a normal page request, the only difference is that a limited set of the html is returned - it doesn't just call your click event, it runs everything, the whole page_load etc. Imagine every timer click is you opening the page in a new browser, that's what is happening, your page is not storing any state.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, February 12, 2015 11:05 AM -
User-271186128 posted
Hi bryanbankester,<!--?xml:namespace prefix = "o" ns = "urn:schemas-microsoft-com:office:office" /--><o:p></o:p>
As for this issue, I agree with AidyF, you could use session to store the array1 and array 2.<o:p></o:p>
Besides, I also have some problems about this issue.
After compare the two arrays, do you populate the array2? And, after 10 seconds, which event will be triggered first, compare event or repopulate event?
So, I suppose perhaps this issue it also related to the time. I suggest you could set the compare time greater than the Array repopulate time.
Best Regards,
Dillion- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, February 13, 2015 4:56 AM
All replies
-
User-760709272 posted
You have to remember that your objects only exist while the page is rendering, when your page is finished and sent to the client everything is destroyed. So your array1 is being built each time. If you want array1 to survive then store it in the session. c# but something like
arrayType array1; private void LoadArray1() { if (Session["a1"] != null) { array1 = (arrayType)Session["a1"]; return; } array1 = GetArrayFromDatabase(); Session["a1"] = array1; }
Thursday, February 12, 2015 10:41 AM -
User1613081147 posted
I thought the UpdatePanel.Update() every timer interval 5 seconds would cause only Label1 to "update" and also do whatever is inside Sub Timer1_Tick. But apparently it is causing my array1, the previous array to being built every 5 seconds. That does not make sense to me how Timer1 and UpdatePanel1 could interfere with Sub LoadArray1(). Since Im not calling LoadArray1() inside the UpdatePanel or inside Sub Timer1_Tick.
Thursday, February 12, 2015 11:00 AM -
User-760709272 posted
You haven't shown where you are calling LoadArray1 so it's hard to give an answer.
Forget the updatepanel though, it makes no difference to the page lifecycle, everything is getting created then destroyed each 5 seconds. With the update panel it is no different to a normal page request, the only difference is that a limited set of the html is returned - it doesn't just call your click event, it runs everything, the whole page_load etc. Imagine every timer click is you opening the page in a new browser, that's what is happening, your page is not storing any state.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, February 12, 2015 11:05 AM -
User-271186128 posted
Hi bryanbankester,<!--?xml:namespace prefix = "o" ns = "urn:schemas-microsoft-com:office:office" /--><o:p></o:p>
As for this issue, I agree with AidyF, you could use session to store the array1 and array 2.<o:p></o:p>
Besides, I also have some problems about this issue.
After compare the two arrays, do you populate the array2? And, after 10 seconds, which event will be triggered first, compare event or repopulate event?
So, I suppose perhaps this issue it also related to the time. I suggest you could set the compare time greater than the Array repopulate time.
Best Regards,
Dillion- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, February 13, 2015 4:56 AM