Subsequent Connections and null RemoteDevice Properties

Developer
Jan 27, 2011 at 5:25 PM

Using RAPI2 v 1.1 in MTAThread WinForm application under framework 3.0. When application is started, first read of FirstConectedDevice properties reads valid values, if the device is disconnected and reconnected, properties will return 0 or null. The application needs restart to get valid values again. Any thoughts?

Coordinator
Jan 27, 2011 at 11:16 PM

Watch for the DeviceConnected and DeviceDisconnected events on RemoteDeviceManager. They will fire on different thread than your UI, but in a test app in the same environment (using 1.2 library though), I was able to get it to recognize the new device calling FirstConnectedDevice on receiving the DeviceConnected event. Test project is in Source Code update 62056.

Developer
Jan 28, 2011 at 1:59 PM
Edited Jan 28, 2011 at 2:06 PM

Yes, I can reliably detect the device presence. Its the properties that don't seem to reload correctly. Below is a console app to demonstrate the problem. Run the app, establish connection and note that the app displays "Connected" and then the Name, OSVersion and PowerStatus.BatteryLifePercent properties successfully. The app will repeat this response every 2 seconds. Disconnect the device and note the app responds "Disconnected". Reconnect the device and note that the app detects connection but that the same properties do not report correctly. If the app is restarted, it will produce the correct results the first time, but subsequent connections will not.

This problem also occurs when using the DeviceConnected event.

My workaround is to use a primary user interface that spawns a hidden console app at each connection to effect what I am trying to accomplish.

On rare occasion, the properties read correctly after reconnection. Thought I might have been reading the properties too quickly after connection detection but delaying my procedure after connection didn't seem to help. 

using System.Devices;
using System.Diagnostics;

namespace RAPITest
{
    class Program
    {
        static void Main(string[] args)
        {
            RemoteDeviceManager rdm = new RemoteDeviceManager();
            RemoteDevice device = null;
            System.Console.WriteLine("\nDock or remove remote device.");
            System.Console.WriteLine("  (Press any key to quit)");

            while (!System.Console.KeyAvailable)
            {
                device = rdm.Devices.FirstConnectedDevice;
                if (device != null)
                {
                    System.Console.WriteLine("\nConnected.");
                    System.Console.WriteLine("  Name:    {0}", device.Name);
                    System.Console.WriteLine("  OS Ver:  {0}", device.OSVersion);
                    System.Console.WriteLine("  Battery: {0}%", device.PowerStatus.BatteryLifePercent);
                }
                if (device == null)
                {
                    System.Console.WriteLine("\nDisonnected.");
                }
                System.Threading.Thread.Sleep(2000);
            }
            while (System.Console.KeyAvailable) 
                System.Console.ReadKey(true);
        }
    }
}
Coordinator
Jan 28, 2011 at 6:22 PM

I took your code from above and with very few modifications have it working just fine with the 1.2 library. I can detach and reattach the device over and over and it will always find the device.

private static void Main(string[] args)
{
   RemoteDeviceManager rdm = new RemoteDeviceManager();
   RemoteDevice device = null;
   System.Console.WriteLine("\nDock or remove remote device.");
   System.Console.WriteLine("  (Press any key to quit)");

   while (!System.Console.KeyAvailable)
   {
      try
      {
         device = rdm.Devices.FirstConnectedDevice;
         if (device != null)
         {
            System.Console.WriteLine("\nConnected.");
            System.Console.WriteLine("  Name:    {0}", device.Name);
            System.Console.WriteLine("  OS Ver:  {0}", device.OSVersion);
            System.Console.WriteLine("  Battery: {0}%", device.PowerStatus.BatteryLifePercent);
         }
         else
         {
            System.Console.WriteLine("\nDisonnected.");
         }
      }
      catch { }
      System.Threading.Thread.Sleep(5000);
   }
   while (System.Console.KeyAvailable)
      System.Console.ReadKey(true);
}
Developer
Jan 28, 2011 at 6:33 PM

I get connection, its the properties that are not correct on subsequent connections.

I would like to test under version 1.2. When I download from the Downloads tab, I receive version 1.1.

 

Coordinator
Jan 28, 2011 at 11:12 PM

My bad. I forgot to change the version of the DLL to 1.2 before posting it. Make sure the creation date is ‎December ‎18, ‎2009. That version works under Windows 7 with both the connection and the properties.

Developer
Jan 31, 2011 at 3:05 PM

I get the same results with the 1.2 build.

Seems like the Sink interface needs to be accessible seperately from the Desktop interface. It seems like if I try to use the Desktop interface while ActiveSync is connecting, the device properties nolonger update. (this is after DeviceConnected event occurs)

Occasionally, on a reconnect, I see the device information updated to correct values several seconds after ActiveSync establishes the connection.

I've got to look into this a bit more.

Feb 22, 2011 at 2:01 PM
chriscourson wrote:

I get the same results with the 1.2 build.

Seems like the Sink interface needs to be accessible seperately from the Desktop interface. It seems like if I try to use the Desktop interface while ActiveSync is connecting, the device properties nolonger update. (this is after DeviceConnected event occurs)

Occasionally, on a reconnect, I see the device information updated to correct values several seconds after ActiveSync establishes the connection.

I've got to look into this a bit more.

Have you solved this problem yet? I'm having this exact same problem on Windows XP with ActiveSync 4.5 but not with Windows 7 and WMDC.

Developer
Feb 22, 2011 at 2:19 PM
Edited Feb 22, 2011 at 2:21 PM

The solution is to use only a MTA thread model. RemoteDevice properties are dynamic and may need to be read repeatedly until they return valid results. Use threadsafe delegates in every class that will access RAPI2 directly or be called by a class that accesses RAPI2. Its all about the threading. Very nice library. The code on the home page doesn't do it justice. I am using it pretty extensively now. Made the source Framework 2.0 compatible and corrected the assembly version.

Feb 23, 2011 at 9:42 AM

How many times, and how fast, do you check if a property has a valid result before throwing an error or something?

Developer
Feb 23, 2011 at 1:05 PM

About one second. I let the event handler for connection pause for 200 milliseconds before assigning RemoteDeviceManager.FirstConnectedDevice to RemoteDevice. I don't want my app to rush in there. I then start a seperate thread to communicate to the device. This thread loops 5 times with 200 millisecond pauses and waits for OSVersion to become anything but 0.0.0.0. It is possible to throw an ObjectReference error at this point. Very rare but it happens to ActiveSync as well. The connection event handler will assign RemoteDevice only if it is null. The disconnection event handler terminates threads using RemoteDevice then assignes it null. As long as you use thread-safe calls by checking InvokeRequired in your methods, you should have no problems. As soon as you throw any thread related error with the RAPI COM object itself, it seems to become unstable. You will then have no choice but to restart the app. Be careful with try-catch. Don't let it consume errors and continue without debugging them. Try-catch can easily hide a thread issue.

And I must add, that's some beautifully written C#.

Developer
Feb 28, 2011 at 5:27 PM
Edited Feb 28, 2011 at 5:28 PM

Here's my WinForms template. Use either MTA or STA. Use of the RAPI2 object is performed by an invocation of Form1.ActiveForm. This keeps it all in the same thread.

using System.Devices;
using System.Windows.Forms;

namespace MyRAPI2App
{
    public partial class Form1 : Form
    {
        private RemoteDeviceManager DeviceManager;
        private RemoteDevice Device;

        private delegate void DeviceConnectedThreadSafeDelegate();
        void DeviceManager_DeviceConnected(object sender, System.EventArgs e)
        {
            Device = DeviceManager.Devices.FirstConnectedDevice;
            DeviceConnectedThreadSafeDelegate d = new DeviceConnectedThreadSafeDelegate(DeviceConnected);
            Form1.ActiveForm.Invoke(d);
        }

        void DeviceConnected()
        {
            if (Device != null)
                Label1.Text = Device.OSVersion.ToString();
        }

        private delegate void DeviceDisconnectedThreadSafeDelegate();
        void DeviceManager_DeviceDisconnected(object sender, System.EventArgs e)
        {
            Device = DeviceManager.Devices.FirstConnectedDevice;
            DeviceDisconnectedThreadSafeDelegate d = new DeviceDisconnectedThreadSafeDelegate(DeviceDisconnected);
            Form1.ActiveForm.Invoke(d);
        }

        void DeviceDisconnected()
        {
            Label1.Text = "Not Connected";
        }

        public Form1()
        {
            InitializeComponent();

            DeviceManager = new RemoteDeviceManager();
            DeviceManager.DeviceConnected += new System.EventHandler(DeviceManager_DeviceConnected);
            DeviceManager.DeviceDisconnected += new System.EventHandler(DeviceManager_DeviceDisconnected);
        }
    }
}
Coordinator
Mar 1, 2011 at 5:13 AM

Nice sample code. Thank you for contributing.

Developer
Dec 21, 2011 at 2:50 PM

Update:

Recently, my desktop was upgraded from an XP machine to a Windows 7 machine. Started a new RAPI2 project and decided to test this subsequent connection issue i had experienced on the old machine. Guess what. No reconnection issue. However, when I try to read RemoteDevice.OSVersion.Build I get (Exception from HRESULT: 0x80040155).

 

Developer
Dec 30, 2011 at 5:43 PM
Edited Dec 30, 2011 at 5:59 PM

Update:

I spoke too soon. Must have just gotten lucky. The threading issue persists but I think I figured it out.

Take a look at the code below. Two distinct points to make. Firstly, note that on DeviceConnect, I lock the RemoteDeviceManager before using it. Secondly, on DeviceDisconnect, I dispose of the RemoteDevice object explicitly. Ported to WinForms app and different threading models, windows XP and Windows 7 and it is running solid. Without this, subsequent device connections would show 0.0.0.0 as the OS version.

using System;
using System.Devices;
using System.Threading;

namespace RAPI2Console
{
    class Program
    {
        static RemoteDeviceManager rdm = new RemoteDeviceManager();
        static RemoteDevice dev = null;

        static void Main(string[] args)
        {
            rdm.DeviceConnected += new EventHandler(rdm_DeviceConnected);
            rdm.DeviceDisconnected += new EventHandler(rdm_DeviceDisconnected);

            Console.WriteLine("Main's Thread Name: {0}", Thread.CurrentThread.ManagedThreadId);
            while (true) { }
        }

        static void rdm_DeviceDisconnected(object sender, EventArgs e)
        {
            Console.WriteLine("\nDisconnected.\n");
            if (dev != null)
                dev.Dispose();
            Console.WriteLine("\nAwaiting connection...");
        }

        static void rdm_DeviceConnected(object sender, EventArgs e)
        {
            Console.WriteLine("rdm_DeviceConnected's Thread Name: {0}", Thread.CurrentThread.ManagedThreadId);
            lock (rdm)
            {
                dev = rdm.Devices.FirstConnectedDevice;
                if (dev != null)
                {
                    Console.WriteLine("\nConnected.");
                    Console.WriteLine("  Name:    {0}", dev.Name);
                    Console.WriteLine("  Platform {0}", dev.Platform);
                    Console.WriteLine("  OS       {0}", dev.OSVersion.ToString());
                }
            }
        }
    }
}

Feb 8, 2012 at 7:36 PM

chriscourson,

Thanks for your sample code.

I used your WinForms template and implemented the two remarks of your last post.
- On DeviceConnect I lock the RemoteDeviceManager before using it.
- On DeviceDisconnect I dispose of the RemoteDevice object explicitly.

However i'm still having the same problem with subsequent connections.
On the first connection the RemoteDevice properties are read correctly (OSVersion = 5.0.1400.0).
On the next connections the RemoteDevice properties are empty (OSVersion = 0.0.0.0).

What am i doing wrong ? Can anyone point me in the right direction ?

Specifications:
Windows 7 (32-bit)
.NET Framework 4.0
Windows Mobile Device Center version 6.1.6965.
RAPI2 version 1.3.1
Motorola MT2070 scanner

Below is my code:


using System.Windows.Forms;
using System.Drawing;
using System.Devices;

namespace RAPI2Form
{
    public partial class FormMain : Form
    {
        private RemoteDeviceManager DeviceManager = new RemoteDeviceManager();
        private RemoteDevice Device = null;

        public FormMain()
        {
            // Initialize component
            InitializeComponent();

            // Initialize event handlers
            DeviceManager.DeviceConnected += new System.EventHandler(DeviceManager_DeviceConnected);
            DeviceManager.DeviceDisconnected += new System.EventHandler(DeviceManager_DeviceDisconnected);
        }

        private delegate void DeviceConnectedThreadSafeDelegate();
        void DeviceManager_DeviceConnected(object sender, System.EventArgs e)
        {
            lock (DeviceManager)
            {
                // Set device
                Device = DeviceManager.Devices.FirstConnectedDevice;

                // Invoke connection
                DeviceConnectedThreadSafeDelegate d = new DeviceConnectedThreadSafeDelegate(DeviceConnected);
                this.Invoke(d);
            }
        }

        private delegate void DeviceDisconnectedThreadSafeDelegate();
        void DeviceManager_DeviceDisconnected(object sender, System.EventArgs e)
        {
            // Set device
            Device = DeviceManager.Devices.FirstConnectedDevice;
            if (Device != null) Device.Dispose();

            // Invoke disconnection
            DeviceDisconnectedThreadSafeDelegate d = new DeviceDisconnectedThreadSafeDelegate(DeviceDisconnected);
            this.Invoke(d);
        }


        void DeviceConnected()
        {
            if (Device != null)
            {
                TextStatus.Text = "Connected";
                TextStatus.ForeColor = Color.Green;
                if (Device.DeviceId != null) TextID.Text = Device.DeviceId.ToString();
                if (Device.HostAddress != null) TextHostAddress.Text = Device.HostAddress.ToString();
                if (Device.IPAddress != null) TextIPAddress.Text = Device.IPAddress.ToString();
                if (Device.Name != null) TextName.Text = Device.Name.ToString();
                if (Device.Platform != null) TextPlatform.Text = Device.Platform.ToString();
                if (Device.OSVersion != null) TextOSVersion.Text = Device.OSVersion.ToString();
                TextPhysicalMemory.Text = Device.MemoryStatus.TotalPhysical.ToString() + " bytes";
                TextPowerStatus.Text = Device.PowerStatus.BatteryLifePercent.ToString() + " %";
            }
        }

        void DeviceDisconnected()
        {
            TextStatus.Text = "Disconnected";
            TextStatus.ForeColor = Color.Red;
            TextID.Text = "";
            TextHostAddress.Text = "";
            TextIPAddress.Text = "";
            TextName.Text = "";
            TextPlatform.Text = "";
            TextOSVersion.Text = "";
            TextPhysicalMemory.Text = "";
            TextPowerStatus.Text = "";
        }
    }

}

Developer
Feb 8, 2012 at 8:13 PM
dietervd,

Your code looks fine. Only thing I can think to check is to make sure your application is referencing RAPI2 version 1.3.1.0. If you have used a previous version, this can be an easy mistake.

«Chris Courson, CET, Analyst II
Macy's Systems & Technology | National Repair Centers | 4130 W Gandy Blvd, Tampa, FL 33611-3402 | (813) 805-5466, Tie Line: 8-102-089-5466


From: "dietervd" <notifications@codeplex.com>
To: chris.courson@macys.com
Date: 02/08/2012 02:36 PM
Subject: Re: Subsequent Connections and null RemoteDevice Properties [rapi2:243560]





From: dietervd

chriscourson,

Thanks for your sample code.

I used your WinForms template and implemented the two remarks of your last post.
- On DeviceConnect I lock the RemoteDeviceManager before using it.
- On DeviceDisconnect I dispose of the RemoteDevice object explicitly.

However i'm still having the same problem with subsequent connections.
On the first connection the RemoteDevice properties are read correctly (OSVersion = 5.0.1400.0).
On the next connections the RemoteDevice properties are empty (OSVersion = 0.0.0.0).

What am i doing wrong ? Can anyone point me in the right direction ?

Specifications:
Windows 7 (32-bit)
.NET Framework 4.0
Windows Mobile Device Center version 6.1.6965.
RAPI2 version 1.3.1
Motorola MT2070 scanner

Below is my code:


using System.Windows.Forms;
using System.Drawing;
using System.Devices;

namespace RAPI2Form
{
public partial class FormMain : Form
{
private RemoteDeviceManager DeviceManager = new RemoteDeviceManager();
private RemoteDevice Device = null;

public FormMain()
{
// Initialize component
InitializeComponent();

// Initialize event handlers
DeviceManager.DeviceConnected += new System.EventHandler(DeviceManager_DeviceConnected);
DeviceManager.DeviceDisconnected += new System.EventHandler(DeviceManager_DeviceDisconnected);
}

private delegate void DeviceConnectedThreadSafeDelegate();
void DeviceManager_DeviceConnected(object sender, System.EventArgs e)
{
lock (DeviceManager)
{
// Set device
Device = DeviceManager.Devices.FirstConnectedDevice;

// Invoke connection
DeviceConnectedThreadSafeDelegate d = new DeviceConnectedThreadSafeDelegate(DeviceConnected);
this.Invoke(d);
}
}

private delegate void DeviceDisconnectedThreadSafeDelegate();
void DeviceManager_DeviceDisconnected(object sender, System.EventArgs e)
{
// Set device
Device = DeviceManager.Devices.FirstConnectedDevice;
if (Device != null) Device.Dispose();

// Invoke disconnection
DeviceDisconnectedThreadSafeDelegate d = new DeviceDisconnectedThreadSafeDelegate(DeviceDisconnected);
this.Invoke(d);
}


void DeviceConnected()
{
if (Device != null)
{
TextStatus.Text = "Connected";
TextStatus.ForeColor = Color.Green;
if (Device.DeviceId != null) TextID.Text = Device.DeviceId.ToString();
if (Device.HostAddress != null) TextHostAddress.Text = Device.HostAddress.ToString();
if (Device.IPAddress != null) TextIPAddress.Text = Device.IPAddress.ToString();
if (Device.Name != null) TextName.Text = Device.Name.ToString();
if (Device.Platform != null) TextPlatform.Text = Device.Platform.ToString();
if (Device.OSVersion != null) TextOSVersion.Text = Device.OSVersion.ToString();
TextPhysicalMemory.Text = Device.MemoryStatus.TotalPhysical.ToString() + " bytes";
TextPowerStatus.Text = Device.PowerStatus.BatteryLifePercent.ToString() + " %";
}
}

void DeviceDisconnected()
{
TextStatus.Text = "Disconnected";
TextStatus.ForeColor = Color.Red;
TextID.Text = "";
TextHostAddress.Text = "";
TextIPAddress.Text = "";
TextName.Text = "";
TextPlatform.Text = "";
TextOSVersion.Text = "";
TextPhysicalMemory.Text = "";
TextPowerStatus.Text = "";
}
}

}

Read the full discussion online.

To add a post to this discussion, reply to this email (rapi2@discussions.codeplex.com)

To start a new discussion for this project, email rapi2@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com

Feb 9, 2012 at 7:27 AM

Thanks for your reply.

I checked the properties of the RAPI2.DLL reference in my project.
Version = 1.3.1.21182
Runtime version = v2.0.50727

I assume this is the latest version, so i don't think this could be the problem.
Do you have any other suggestions ?

Feb 9, 2012 at 9:07 AM

The strange thing is, when i run the exact same code in a console application everything works fine.
After every subsequent connection, the properties are read correctly.

I don't understand why it is working as a console application, but not as a WinForm application.

Console application code :


using System;
using System.Devices;
using System.Threading;

namespace RAPI2Console
{
    class Program
    {
        static RemoteDeviceManager DeviceManager = new RemoteDeviceManager();
        static RemoteDevice Device = null;

        static void Main(string[] args)
        {
            DeviceManager.DeviceConnected += new EventHandler(DeviceManager_DeviceConnected);
            DeviceManager.DeviceDisconnected += new EventHandler(DeviceManager_DeviceDisconnected);

            Console.WriteLine("Main's Thread Name: {0}", Thread.CurrentThread.ManagedThreadId);
            while (true) { }
        }

        static void DeviceManager_DeviceConnected(object sender, EventArgs e)
        {
            Console.WriteLine("DeviceManager_DeviceConnected's Thread Name: {0}", Thread.CurrentThread.ManagedThreadId);
            lock (DeviceManager)
            {
                Device = DeviceManager.Devices.FirstConnectedDevice;
                if (Device != null)
                {
                    Console.WriteLine("\nConnected.");
                    Console.WriteLine("  ID:       {0}", Device.DeviceId);
                    Console.WriteLine("  Name:     {0}", Device.Name);
                    Console.WriteLine("  Platform: {0}", Device.Platform);
                    Console.WriteLine("  OS:       {0}", Device.OSVersion.ToString());
                    Console.WriteLine("  Memory:   {0} bytes", Device.MemoryStatus.TotalPhysical.ToString());
                    Console.WriteLine("  Battery:  {0} %", Device.PowerStatus.BatteryLifePercent.ToString());
                }
            }
        }

        static void DeviceManager_DeviceDisconnected(object sender, EventArgs e)
        {
            Console.WriteLine("\nDisconnected.\n");
            if (Device != null) Device.Dispose();
            Console.WriteLine("\nAwaiting connection...");
        }

    }
}

 

Developer
Feb 9, 2012 at 2:37 PM
Dietervd,

Being that the problem occurs only in a Forms application then it is probably a threading issue. Make sure the program is using [STAThread]. Make sure you are using invokes to access the Form as static objects.

«Chris Courson, CET, Analyst II
Macy's Systems & Technology | National Repair Centers | 4130 W Gandy Blvd, Tampa, FL 33611-3402 | (813) 805-5466, Tie Line: 8-102-089-5466


From: "dietervd" <notifications@codeplex.com>
To: chris.courson@macys.com
Date: 02/09/2012 04:07 AM
Subject: Re: Subsequent Connections and null RemoteDevice Properties [rapi2:243560]





From: dietervd

The strange thing is, when i run the exact same code in a console application everything works fine.
After every subsequent connection, the properties are read correctly.

I don't understand why it is working as a console application, but not as a WinForm application.

Console application code :


using System;
using System.Devices;
using System.Threading;

namespace RAPI2Console
{
class Program
{
static RemoteDeviceManager DeviceManager = new RemoteDeviceManager();
static RemoteDevice Device = null;

static void Main(string[] args)
{
DeviceManager.DeviceConnected += new EventHandler(DeviceManager_DeviceConnected);
DeviceManager.DeviceDisconnected += new EventHandler(DeviceManager_DeviceDisconnected);

Console.WriteLine("Main's Thread Name: {0}", Thread.CurrentThread.ManagedThreadId);
while (true) { }
}

static void DeviceManager_DeviceConnected(object sender, EventArgs e)
{
Console.WriteLine("DeviceManager_DeviceConnected's Thread Name: {0}", Thread.CurrentThread.ManagedThreadId);
lock (DeviceManager)
{
Device = DeviceManager.Devices.FirstConnectedDevice;
if (Device != null)
{
Console.WriteLine("\nConnected.");
Console.WriteLine(" ID: {0}", Device.DeviceId);
Console.WriteLine(" Name: {0}", Device.Name);
Console.WriteLine(" Platform: {0}", Device.Platform);
Console.WriteLine(" OS: {0}", Device.OSVersion.ToString());
Console.WriteLine(" Memory: {0} bytes", Device.MemoryStatus.TotalPhysical.ToString());
Console.WriteLine(" Battery: {0} %", Device.PowerStatus.BatteryLifePercent.ToString());
}
}
}

static void DeviceManager_DeviceDisconnected(object sender, EventArgs e)
{
Console.WriteLine("\nDisconnected.\n");
if (Device != null) Device.Dispose();
Console.WriteLine("\nAwaiting connection...");
}

}
}

Read the full discussion online.

To add a post to this discussion, reply to this email (rapi2@discussions.codeplex.com)

To start a new discussion for this project, email rapi2@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com

Feb 10, 2012 at 8:24 AM
ChrisCourson wrote:
Dietervd,

Being that the problem occurs only in a Forms application then it is probably a threading issue. Make sure the program is using [STAThread]. Make sure you are using invokes to access the Form as static objects.

Chris, I really appreciate your replies, thank you for that !
But the fact is that I'm a newbie to threading... :(

The main entry point for the application was already using [STAThread].
Do i also need to add this attribute on other methods in my program ?

namespace RAPI2Form
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FormMain());
        }
    }
}

What do you mean exactly with accessing the form as static objects ?
I tried using invokes to FormMain.ActiveForm.Invoke() and invokes to this.Invoke().
In both cases the problem remains, and i keep getting empty property values for every subsequent connection.

DeviceConnectedThreadSafeDelegate d = new DeviceConnectedThreadSafeDelegate(DeviceConnected);
FormMain.ActiveForm.Invoke(d);

DeviceConnectedThreadSafeDelegate d = new DeviceConnectedThreadSafeDelegate(DeviceConnected);
this.Invoke(d);

In a previous post you mentioned you have a fully working WinForm application.
Would it be possible to post your code here, so i can compare with my code ?
Thank you in advance.

Dieter

Developer
Feb 10, 2012 at 3:33 PM
Dietervd,

No problem. Only the declaration in Program.cs needs to exist. That's the actual entry point into your application.

In a forms application, when an event occurs, a thread is created to handle the event. To avoid cross thread problems, check the thread status by using InvokeRequired as such:

private delagate void UpdateTextBox_delagate(string text);
private void UpdateTextBox(string text)
{
if (this.InvokeRequired)
{
UpdateTextBox_delagate method = new UpdateTextBox_delagate(UpdateTextBox);
this.Invoke(method, text);
return;
}
TextBox1.Text = text;
}

You can find many other examples online.

«Chris Courson, CET, Analyst II
Macy's Systems & Technology | National Repair Centers | 4130 W Gandy Blvd, Tampa, FL 33611-3402 | (813) 805-5466, Tie Line: 8-102-089-5466


From: "dietervd" <notifications@codeplex.com>
To: chris.courson@macys.com
Date: 02/10/2012 03:24 AM
Subject: Re: Subsequent Connections and null RemoteDevice Properties [rapi2:243560]





From: dietervd
ChrisCourson wrote:
Dietervd,


Being that the problem occurs only in a Forms application then it is probably a threading issue. Make sure the program is using [STAThread]. Make sure you are using invokes to access the Form as static objects.

Chris, I really appreciate your replies, thank you for that !
But the fact is that I'm a newbie to threading... :(

The main entry point for the application was already using [STAThread].
Do i also need to add this attribute on other methods in my program ?

namespace RAPI2Form
{
static class Program
{
[
STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormMain());
}
}
}

What do you mean exactly with accessing the form as static objects ?
I tried using invokes to FormMain.ActiveForm.Invoke() and invokes to this.Invoke().
In both cases the problem remains, and i keep getting empty property values for every subsequent connection.

DeviceConnectedThreadSafeDelegate d = new DeviceConnectedThreadSafeDelegate(DeviceConnected);
FormMain
.ActiveForm.Invoke(d);

DeviceConnectedThreadSafeDelegate d = new DeviceConnectedThreadSafeDelegate(DeviceConnected);
this
.Invoke(d);

In a previous post you mentioned you have a fully working WinForm application.
Would it be possible to post your code here, so i can compare with my code ?
Thank you in advance.

Dieter

Read the full discussion online.

To add a post to this discussion, reply to this email (rapi2@discussions.codeplex.com)

To start a new discussion for this project, email rapi2@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com

Feb 21, 2012 at 3:16 PM

I'm seeing the same issue as ChrisCourson.

Using the thread safe code on this post: First time launching the app, works no problem.  Disconnect and reconnect the device - fails after that.  The RAPI2 library seems to work fine when not using events, however.

It would be great if someone can post a working winform project!

-- Ron

Coordinator
Feb 25, 2012 at 7:21 AM
Edited Feb 25, 2012 at 7:22 AM

I just made some pretty major changes (some are not backwards compatible) and released a beta of the 2.0 version of the library. Please test. I think I have resolved the connect/disconnect problem this discussion thread has been dealing with.

And Ron, the source code area does include a WinForms project that shows how to use the library.

Developer
Mar 15, 2012 at 12:31 PM
I will be testing this today or tomorrow. Just wanted to drop you a line to let you know I'm still here.


«Chris Courson, CET, Analyst II
Macy's Systems & Technology | National Repair Centers | 4130 W Gandy Blvd, Tampa, FL 33611-3402 | (813) 805-5466, Tie Line: 8-102-089-5466


From: "dahall" <notifications@codeplex.com>
To: chris.courson@macys.com
Date: 02/25/2012 02:21 AM
Subject: Re: Subsequent Connections and null RemoteDevice Properties [rapi2:243560]





From: dahall

I just made some pretty major changes (some are not backwards compatible) and released a beta of the 2.0 version of the library. Please test. I think I have resolved the connect/disconnect problem this discussion thread has been dealing with.

Read the full discussion online.

To add a post to this discussion, reply to this email (rapi2@discussions.codeplex.com)

To start a new discussion for this project, email rapi2@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com