none
[Help] Check query (I do not clarify) RRS feed

  • Question

  • Hello,

    Recently create a topic about consulting from time to time to a function, saving a date (Here is the thread: To go to the wire)

    =========================

    All right. So far so perfect.
    Next, I want it to be resolved when this thread is cleared for privacy issues, if not a lot of hassle.

    =========================

    The code that I designed Frank L. Smith was something perfect and I worked. My problem is that I have converted the My.Setting to a personal serialized configuration. Since I have spent (I consecrate every 3h to the function) but my problem is that it saves the date but when it passes a time does not call again. Something I'm doing wrong.

    I would like it when I spend 3h with my settings, call the function.

    Can you help me?

    Friend Sub CheckStatusInternet()
    
            Try
    		    Dim WebPath As String = "http://myweb.com"
                Dim request As HttpWebRequest = HttpWebRequest.Create(WebPath)
                Dim response As HttpWebResponse = CType(request.GetResponse, HttpWebResponse)
    			
                If ((response Is Nothing) OrElse (response.StatusCode <> HttpStatusCode.OK)) Then
                    'THE WEB DOES NOT RESPOND
                Else
                    With CONF
                        Dim fecha As Date
                        'Gets the date of the Setting variable indicated. If there is no date of the current equipment set -4 hours
                        'It is returning the conversion to date as false. It may be that this is empty or has a text that does not fit as a date
                        If Date.TryParse(.Setting("LastRunDT"), fecha) = False Then fecha = DateAdd(DateInterval.Hour, 4, Now)
    
                        'Calculate the difference between two dates in hours. If it is more than 3 hours, perform the indicated tasks
    
                        If DateDiff(DateInterval.Hour, Now, fecha) >= 3 Then
                            .Setting("LastRunDT") = DateAdd(DateInterval.Hour, 3, Now)
    
                          'I call my function
    
                        End If
                    End With
                End If
    
            Catch ex As Exception
            End Try

    Friday, May 26, 2017 8:06 PM

All replies

  • Symantecc,

    I'm not following all of that, but I think the issue is this:

    What I did then is based on a TimeSpan - an elapsed period of time. I think the confusion is because it's using DateTime to work out the TimeSpan, but it's the TimeSpan that makes it work.

    That's not what you want though?

    *****

    Start all over again and explain what you ultimately want please?


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, May 26, 2017 8:14 PM
  • Symantecc,

    Let's briefly talk about what a TimeSpan is.

    I can post links and all that but to "get it", sometimes you have to see it for yourself. The following leaves a lot to be desired, but it'll show you what I mean about a TimeSpan:

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
        Private startTime As DateTime
    
        Private Sub Form1_Load(sender As System.Object, _
                               e As System.EventArgs) _
                               Handles MyBase.Load
    
            startTime = Now
    
        End Sub
    
        Private Sub Form1_FormClosing(sender As Object, _
                                      e As System.Windows.Forms.FormClosingEventArgs) _
                                      Handles Me.FormClosing
    
            Dim elapsed As TimeSpan = Now - startTime
    
            Stop
    
        End Sub
    End Class

    Just turn the program on like usual and wait a bit, then close the form (with the "X"). It will "stop" where you see and when it does, hover your mouse over the variable "elapsed".

    Does that help clarify what a TimeSpan is?

    ***** EDIT *****

    Screenshot:


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    Friday, May 26, 2017 8:22 PM
  • I understood. But when I convert the code I have spoiled it and now I do not know where to start or how I can save it so I can compare it since from the My.Setting to my serialized configuration, it will not let me do the same of (TotalHours)
    Friday, May 26, 2017 8:27 PM
  • I understood. But when I convert the code I have spoiled it and now I do not know where to start or how I can save it so I can compare it since from the My.Setting to my serialized configuration, it will not let me do the same of (TotalHours)

    Tell me - from the start - what you want it to do please.

    Assume nothing.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, May 26, 2017 8:29 PM
  • I understood. But when I convert the code I have spoiled it and now I do not know where to start or how I can save it so I can compare it since from the My.Setting to my serialized configuration, it will not let me do the same of (TotalHours)
    See the code in the next post; I feel very sure that I misunderstood
    Friday, May 26, 2017 10:10 PM
  • As an alternate - and this may be the confusion now that I think about it - use the following instead:

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As System.Object, _
                               e As System.EventArgs) _
                               Handles MyBase.Load
    
            Using s3d As New Simple3Des
                With My.Settings
                    If Not String.IsNullOrWhiteSpace(.LastRunDT) Then
                        Dim dt As New DateTime(CLng(s3d.DecryptData(.LastRunDT)))
    
                        If (Now - dt).TotalHours > 24 Then
                            CheckMyRegistration()
                        End If
                    End If
                End With
            End Using
    
        End Sub
    
        Private Sub CheckMyRegistration()
    
            ' Run your method and now, once done, run this:
    
            Using s3d As New Simple3Des
                With My.Settings
                    .LastRunDT = s3d.EncryptData(Now.Ticks.ToString)
                    .Save()
                End With
            End Using
    
        End Sub
    
        'Based on: https://msdn.microsoft.com/en-us/library/ms172831.aspx
        Public NotInheritable Class Simple3Des
            Implements IDisposable
    
            Private TripleDes As New Security.Cryptography.TripleDESCryptoServiceProvider
    
            Sub New()
                ' Initialize the crypto provider.
                TripleDes.Key = TruncateHash(st2CraPRuBRa, TripleDes.KeySize \ 8)
                TripleDes.IV = TruncateHash("", TripleDes.BlockSize \ 8)
            End Sub
    
            Public Function EncryptData(ByVal plaintext As String) _
                As String
    
                ' Convert the plaintext string to a byte array. 
                Dim plaintextBytes() As Byte = _
                    System.Text.Encoding.Unicode.GetBytes(plaintext)
    
                ' Create the stream. 
                Dim ms As New System.IO.MemoryStream
                ' Create the encoder to write to the stream. 
                Dim encStream As New Security.Cryptography.CryptoStream(ms, _
                    TripleDes.CreateEncryptor(), _
                    System.Security.Cryptography.CryptoStreamMode.Write)
    
                ' Use the crypto stream to write the byte array to the stream.
                encStream.Write(plaintextBytes, 0, plaintextBytes.Length)
                encStream.FlushFinalBlock()
    
                ' Convert the encrypted stream to a printable string. 
                Return Convert.ToBase64String(ms.ToArray)
            End Function
    
            Public Function DecryptData(ByVal encryptedtext As String) _
                As String
    
                ' Convert the encrypted text string to a byte array. 
                Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext)
    
                ' Create the stream. 
                Dim ms As New System.IO.MemoryStream
                ' Create the decoder to write to the stream. 
                Dim decStream As New Security.Cryptography.CryptoStream(ms, _
                    TripleDes.CreateDecryptor(), _
                    System.Security.Cryptography.CryptoStreamMode.Write)
    
                ' Use the crypto stream to write the byte array to the stream.
                decStream.Write(encryptedBytes, 0, encryptedBytes.Length)
                decStream.FlushFinalBlock()
    
                ' Convert the plaintext stream to a string. 
                Return System.Text.Encoding.Unicode.GetString(ms.ToArray)
            End Function
    
            Private Function TruncateHash(ByVal key As String, _
                                          ByVal length As Integer) _
                                          As Byte()
    
                Dim sha1 As New Security.Cryptography.SHA1CryptoServiceProvider
    
                ' Hash the key. 
                Dim keyBytes() As Byte = _
                    System.Text.Encoding.Unicode.GetBytes(key)
                Dim hash() As Byte = sha1.ComputeHash(keyBytes)
    
                ' Truncate or pad the hash. 
                ReDim Preserve hash(length - 1)
                Return hash
            End Function
    
            Private st2CraPRuBRa As String = "tHUw=Ta8a2wU"
    
            Public Sub Dispose() Implements IDisposable.Dispose
                If TripleDes IsNot Nothing Then
                    TripleDes.Dispose()
                    TripleDes = Nothing
                End If
            End Sub
        End Class
    End Class

    That's the same as the first one ten minutes ago but this time, it's only reset when you check your registration; not every time the form is closed.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, May 26, 2017 10:21 PM
  • Thanks for replying Frank,

    But how can I use it in my code?
    Saturday, May 27, 2017 5:22 PM
  • Thanks for replying Frank,

    But how can I use it in my code?

    Wrong thread, wrong reply!

    ;-)

    What you see shown should be explanation enough? It's nearly the same as the first time, but this way, it's reset every time you check it rather than each time the user closes the form.

    Other than that, use it the very same way.

    If that's not what you want, explain what you do want and we'll go from there.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Saturday, May 27, 2017 5:32 PM
  • We have not understood, for privacy issues can you delete my topic?
    Thank you
    Sunday, May 28, 2017 6:46 PM
  • We have not understood, for privacy issues can you delete my topic?
    Thank you

    I'm not sure who you're talking to, but only a moderator can do that.

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Sunday, May 28, 2017 6:56 PM