Results 1 to 7 of 7

Thread: Restarting an app without actually restarting it

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2016
    Posts
    5

    Post Restarting an app without actually restarting it

    Hi All
    A desktop app that communicates via USB with a scanner running Windows CE 6.00. The code shown (MS Example) works perfectly if the scanner is connected before the app is loaded. But if the scanner is connected after the app is loaded, the device connects and some commands work but not most. Restarting the app with the scanner connected gets everything working correctly again. Error given is device not connected, although it obviously is and recognized. I suspect it has something to do with connections to the driver when the app is loaded.

    Rather than have the user restart the application I would like to try and reload the elements of the app that need it. I've tried reloading the form as well as running InitializeComponent, removing and replacing the handlers and disposing and recreating the RemoteDeviceManager. Nothing works. Is it possible to do this without an application restart? Would appreciate any suggestions.

    Code:
    Imports System.Devices
    Partial Public Class Form1
        Inherits Form
        Public mgr As RemoteDeviceManager = Nothing
        Public dev As RemoteDevice = Nothing
    
        Public Sub New()
            InitializeComponent()
            mgr = New RemoteDeviceManager()
            AddHandler mgr.DeviceConnected, AddressOf mgr_DeviceConnected
            AddHandler mgr.DeviceDisconnected, AddressOf mgr_DeviceDisconnected
        End Sub
    
        Private Sub mgr_DeviceConnected(ByVal sender As Object, ByVal e As RemoteDeviceConnectEventArgs)
            Me.OnConnection(e.Device, True)
        End Sub
    
        Private Sub mgr_DeviceDisconnected(ByVal sender As Object, ByVal e As RemoteDeviceConnectEventArgs)
            Me.OnConnection(e.Device, False)
        End Sub
    
        Private Sub OnConnection(ByVal dev As RemoteDevice, ByVal connected As Boolean)
            If connected Then
                StatusLabel.Text = dev.Name & " is now connected"
            Else
                StatusLabel.Text = "No devices are connected."
            End If
        End Sub
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
            dev = mgr.Devices.FirstConnectedDevice
            OnConnection(dev, dev IsNot Nothing)
        End Sub
    
        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As EventArgs)
            RemoveHandler mgr.DeviceConnected, AddressOf mgr_DeviceConnected
            RemoveHandler mgr.DeviceDisconnected, AddressOf mgr_DeviceDisconnected
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Try
                Using r As New RemoteDeviceManager()
                    Using dev As RemoteDevice = r.Devices.FirstConnectedDevice
                        If dev Is Nothing Then
                            Return
                        End If
                        Console.WriteLine(dev.Name & ":" & dev.Platform)  '''''This works
                        Console.WriteLine("Remaining power: {0}%", dev.PowerStatus.BatteryLifePercent)  ''''This doesnt
                    End Using
                End Using
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
    
    End Class

  2. #2
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: Restarting an app without actually restarting it

    do you see the statuslabel change when connecting/disconnecting the device? i.e. is the event fired and OnConnection executed?

    the device connects and some commands work but not most.
    how do you know it connects? what do you mean with "some commands work but not most."?

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2016
    Posts
    5

    Re: Restarting an app without actually restarting it

    Hi
    Thank you for your reply.
    Yes, the device is connecting - the status does change. There are numerous commands available such as name of device, IP Number, battery status etc
    As I mentioned, you can see that it does connect as some of the commands, as shown remed in the code, do work, for instance the device name. But some commands
    throw the error. As examples, I only showed two commands in the code - the name and the power status. If I rem out the problematic ones, it all works fine.
    Also, if I stop the app and view dev I can see that all are available or some are available depending on when the device was plugged in.
    Restarting the app without removing the device solves the issues. Hence my original question - how restart everything without having to restart
    Many thanks again for the

  4. #4
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: Restarting an app without actually restarting it

    Hi, i'd use the "restart app" only as last Resort if nothing else works. have you tried to use the original RemoteDeviceManager instance instead of creating a new one, or using the e.Device that you get on the connected/disconnected Events instead of using Devices.FirstConnectedDevice?

    something like this:
    Code:
        Private Sub OnConnection(ByVal dev2 As RemoteDevice, ByVal connected As Boolean)
            If connected Then
                StatusLabel.Text = dev2.Name & " is now connected"
                dev=dev2
            Else
                StatusLabel.Text = "No devices are connected."
                dev=nothing
            End If
        End Sub
    
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Try
                If dev Is Nothing Then
                    Return
                End If
                Console.WriteLine(dev.Name & ":" & dev.Platform)  '''''This works
                Console.WriteLine("Remaining power: {0}%", dev.PowerStatus.BatteryLifePercent)  ''''This doesnt
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
    wrappers can behave strange sometimes if they are not used as intended by the creator of the wrapper. i am thinking that maybe by creating a new RemoteDeviceManager instead of using the existing one, something does not get wired up correctly.
    Last edited by digitalShaman; Dec 9th, 2016 at 07:40 AM.

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2016
    Posts
    5

    Re: Restarting an app without actually restarting it

    Hi
    Many Thanks again for coming back.
    Your suggestion makes a lot of sense but I'm struggling to get the syntax right, when I try your code I get Boolean cannot be converted to RemoteDevice. I'm obviously doing something wrong
    but cant get it right. Could you perhaps suggest how to adjust it with my code example above
    Many Thanks

  6. #6
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: Restarting an app without actually restarting it

    i'm not sure why that error talks about boolean but after reviewing the code changes i suggested the only issue i see is that it should be:
    Code:
    StatusLabel.Text = dev2.Name & " is now connected"
    i will change the code in post #4.

    but otherwise i think it should integrate into your existing code from post #1 well enough to not throw an error. if it still does, it would be good to know on what code line it fails.

    but after all the code is just scientific. Yours just displays when a device is connected and sends commands to the .FirstConnectedDevice which is certainly not correct in the real field. after the changes i suggest, it sends the command to the last recently connected device which is no better.
    i dont know but there could also be some virtual devices in between whenever you connect the real hw... maybe thats part of the issue?
    Last edited by digitalShaman; Dec 9th, 2016 at 07:45 AM.

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2016
    Posts
    5

    Re: Restarting an app without actually restarting it

    Many thanks. That made the difference

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width