none
Constantly runing code in the background help.

    Question

  • Alright. I'm working on a project, and I'm stuck on this next part. I need my Form to constantly be running a bit of code (CODE BELOW). I was thinking about just a time to constantly check, but I feel like that's going to lag out the program. I wanted to know what options I have to run this code constantly without any problems. Thanks.

    CODE: The code I'm talking about it running all the time.

    If IsUserlandConnected.Equals(True) Then
                'Connected
                Label1.Text = "Connected."
            Else
                If IsUserlandConnected.Equals(False) Then
                    'Not Connected
                    Label1.Text = "Not Connected."
                End If
            End If


    Tuesday, March 28, 2017 12:56 AM

All replies

  • I wanted to know what options I have to run this code constantly without any problems.

    Use a timer.  Put that code in the timer tick event.   You can adjust the impact on the system by adjusting the timer interval.

    Tuesday, March 28, 2017 1:13 AM
  • Well I did do that at first, but I ran into problems. I need it to be running that code any time something's plugged into my computer. If I use a timer I run into the problem of having a lagged form, also if I adjust the timer it's slow at checking if my Phone is connected to my computer. Is there a better method maybe with checking when a device is plugged into the computer? Or maybe when the form is moved or touched?

    • Edited by TimeFluid Tuesday, March 28, 2017 1:17 AM
    Tuesday, March 28, 2017 1:16 AM
  • Another consideration is to create a Windows service. Apple uses a windows service for things such as when a device is connected or disconnected along with other operations.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, March 28, 2017 1:23 AM
    Moderator
  • How would I create a Windows service? Thanks. :D
    Tuesday, March 28, 2017 1:26 AM
  • Well I did do that at first, but I ran into problems.

    Then you can't use that code.

    The alternative is to use code that detects the device Insertion.  AFAIK the framework does not support that directly - you need to code it for yourself based on the Windows API. See here for a detailed discussion:  https://www.codeproject.com/kb/system/hwdetect.aspx

    Tuesday, March 28, 2017 1:40 AM
  • The following article (with a sample in vb.net) explains the basics in C# and the download has a VB.NET sample also. In the example they have the app.config setup to run daily but also explains how to run every minute but once you get the hang of things you can have it run more often.

    To install a service you need to execute InstallUtil.

    To debug you can use "attach to process" which is located under the debug menu in the IDE.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, March 28, 2017 2:32 AM
    Moderator
  • Well I did do that at first, but I ran into problems. I need it to be running that code any time something's plugged into my computer. If I use a timer I run into the problem of having a lagged form, also if I adjust the timer it's slow at checking if my Phone is connected to my computer. Is there a better method maybe with checking when a device is plugged into the computer? Or maybe when the form is moved or touched?

    Then you use probably a wrong timer. A windows forms timer is the timer to go with a form. 

    Be aware you say "background" that can have more meanings. Your problem needs no background process but probably you want your form to stay in the "background". 

    If you write background here people comes always with difficult solutions. 

    For instance if you take a windows service for it and it is not for friends the chance is 80 people will remove it because they think it is a virus. 


    Success
    Cor

    Tuesday, March 28, 2017 6:00 AM
  • Thank you. I'm sorry that I didn't better enplane. I just meant background as in the form. Not to run while the form isn't open. I just want it to run that code anytime my iPhone's plugged in. Or all the time when the forms opened.
    Tuesday, March 28, 2017 6:23 AM
  • Here is an example using Task. For the example IsUserlandConnected is a boolean but it could be a method that returns a boolean.  If IsUserlandConnected is a method AND it accesses the UI it will need to be modified.

    Public Class Form1
    
        Private IsUserlandConnected As Boolean
        Private CheckUserlandConnected As Task
        Private UserlandConnectedStop As New Threading.ManualResetEvent(False)
        Private UserlandConnectedStopped As New Threading.ManualResetEvent(False)
        Private UserlandConnectedTimeBetweenChecks As New TimeSpan(0, 0, 10) 'how long between checks
    
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            UserlandConnectedStop.Set() 'stops task
            UserlandConnectedStopped.WaitOne() 'wait for task to end
        End Sub
    
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            CheckUserlandConnected = Task.Run(Sub()
                                                  Dim lblVal As String
                                                  Do
                                                      If IsUserlandConnected Then
                                                          lblVal = "Connected."
                                                      Else
                                                          lblVal = "Not Connected."
                                                      End If
                                                      Me.BeginInvoke(Sub()
                                                                         Label1.Text = lblVal
                                                                     End Sub)
                                                  Loop While Not UserlandConnectedStop.WaitOne(UserlandConnectedTimeBetweenChecks)
                                                  UserlandConnectedStopped.Set()
                                              End Sub)
    
        End Sub
    End Class

    Because the task is not running on the UI it uses BeginInvoke to update the label.



    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.


    • Edited by dbasnett Tuesday, March 28, 2017 11:16 AM
    Tuesday, March 28, 2017 11:09 AM
  • Well I did do that at first, but I ran into problems. I need it to be running that code any time something's plugged into my computer. If I use a timer I run into the problem of having a lagged form, also if I adjust the timer it's slow at checking if my Phone is connected to my computer. Is there a better method maybe with checking when a device is plugged into the computer? Or maybe when the form is moved or touched?

    I found this code marked as "IsUserlandConnected"

        Public Function IsUserlandConnected() As Boolean
            Dim forever As Boolean = True
            Dim USBName As String = String.Empty
            Dim USBSearcher As New ManagementObjectSearcher(
                      "root\CIMV2",
                      "SELECT * FROM Win32_PnPEntity WHERE Description = 'Apple Mobile Device USB Driver'")
            For Each queryObj As ManagementObject In USBSearcher.Get()
                USBName += (queryObj("Description"))
            Next
            If USBName = "Apple Mobile Device USB Driver" Then
                Return True
            Else
                Return False
            End If
        End Function

    Here, that function takes around 1/2 second to fail, i have no device to make it pass so I cannot say how long that takes. What if you set the timer interval to 1000 (One second) ?

    PS - that code I found could use some improvement.

    Wednesday, March 29, 2017 4:42 AM
  • I tired that with the timer set to 1000 (One second) and it still lags every second.
    Thursday, March 30, 2017 12:30 AM