RAPI.Connect() block infinitely if device unplugged & replugged...

Nov 19, 2009 at 7:41 AM

Hi,

First, i want to thank developers for this great library..

I have a problem about RAPI.Connect(). I am trying to make a form that shows if device is available or not; and if available make RAPI connection via RAPI.Connect(). Here is what happens:

- Device is usb-plugged, active sync running and connected
- Run program, test if RAPI.DevicePresent; returns true; connect RAPI via RAPI.Connect(); Connection succeeds, everythings ok.
- While form is open and running, unplug the usb cable, Activesync disconnects, RAPI.ActiveSync.Disconnect triggers, show device is not connected.
- While still form is open and running, plug the usb cable, Activesync connects, RAPI.ActiveSync.Active triggers, test if RAPI.DevicePresent returns true; RAPI.Connect() blocks program infinitely, really infinitely. Also tryed RAPI.Connect(false) and RAPI.Connect(false, 30) still same...

Here is the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenNETCF.Desktop.Communication;

namespace TestConn
{
    public partial class frmTestConn : Form
    {
        RAPI rapi;

        public frmTestConn()
        {
            InitRapi();
            InitializeComponent();
        }
        private void frmTestConn_Load(object sender, EventArgs e)
        {
            if (rapi.DevicePresent)
            {
                ShowConnected();
            }
            else
            {
                ShowDisconnected();
            }
        }
        private void frmTestConn_FormClosed(object sender, FormClosedEventArgs e)
        {
            rapi.Disconnect();
        }
        private void InitRapi()
        {
            rapi = new RAPI();
            rapi.ActiveSync.ConnectAsGuestOnly = true;
            rapi.ActiveSync.Active += new ActiveHandler(ActiveSync_Connect);
            rapi.ActiveSync.Disconnect += new DisconnectHandler(ActiveSync_Disconnect);
        }
        private void ActiveSync_Connect()
        {
            this.Invoke(new MethodInvoker(ShowConnected));
        }
        private void ActiveSync_Disconnect()
        {
            rapi.Disconnect();
            this.Invoke(new MethodInvoker(ShowDisconnected));
        }
        private void ShowConnected()
        {
            if (rapi.DevicePresent)
            {
                rapi.Connect();
                lblConnectStatus.Text = "Connection OK";
                lblConnectStatus.ForeColor = Color.Green;
                Application.DoEvents();
            }
        }
        private void ShowDisconnected()
        {
            lblConnectStatus.Text = "No Connection";
            lblConnectStatus.ForeColor = Color.Red;
            Application.DoEvents();
            rapi.Disconnect();
        }
    }
}

 

Dec 1, 2009 at 3:18 AM

Hi nedium.  I'm getting this same issue with the version of downloaded from here and referenced in my project.  Did you end up finding a solution?

 

Dec 1, 2009 at 7:39 AM

Hi Eugen,
I am still stuck at that point and made no positive progress.

I've tried everything i can but couldn't find the cause of problem in source codes.
Still waiting for an reply with an answer.

For me, it was acceptable to make a form which can be opened only if device is activesync-connected. If not, i simply say "connect the device and try to open form again". But if this problem solved, it will be a cool feature for my application.

Stay in contact, maybe someone can solve the problem and reply here...

Dec 1, 2009 at 1:48 PM

Hi Nedium,  I think I've worked it out.  Try removing the rapi.Connect() call out of your ShowConnected() method invoker.

I got it working but calling connect() from a button on my form after ActiveSync_Connect detected the connection.  I think it related the Invoke call and the thread the RAPI is instanciated on.

So in

private void ShowConnected()
{
if (rapi.DevicePresent)
{
// rapi.Connect(); - comment out
lblConnectStatus.Text = "Connection OK";
lblConnectStatus.ForeColor = Color.Green;
// Application.DoEvents();
}
}

and add a button event

       private void buttonPublishDeviceFiles_Click(object sender, EventArgs e)
        {

            m_rapi.Connect();

            // add test to see if the connection was made and the source file exists
m_rapi.CopyFileToDevice("Product.sdf", "\\Application\\Product.sdf", true);
MessageBox.Show("The product file has been updated successfully.", "Update Successful", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1); }

I would like it to be automated so I'm going to play around with calling rapi.Connect() in a timer event that I
enable from the EventHandler on successful connnection.

Hope it helps.

 

Dec 10, 2009 at 6:06 PM

If you keep the call to rapi.Connect() outside of the invoke, it works.

i.e., :

private void ActiveSync_Connect()
{
     if (rapi.DevicePresent)
     {
          rapi.Connect();
          this.Invoke(new MethodInvoker(ShowConnected));
     }
 }

private void ShowConnected()
        {
            if (rapi.DevicePresent)
            {
                
                lblConnectStatus.Text = "Connection OK";
                lblConnectStatus.ForeColor = Color.Green;
                Application.DoEvents();
            }
        }

Also put the call to rapi.Disconnect() outside invoke. 

 

Eric