catching RAPI.Connect() errors

Nov 7, 2009 at 10:28 PM

Hi, i'm writing a small program to copy files from a mobile device, the program works great if a device is connected, but when i tried to add some error handling capabilities i found that:

 

1. if there's no device connected the Connect() method does not raise any exception, (since the return type is void) it does not return any error and the worst part is that it "aborts" the execution of the actual function:

private void button1_Click(object sender, EventArgs e)
        {
            using (RAPI rapi = new RAPI())
            {

                    rapi.Connect();

               // this code never executes if no device is connected (docked)
                if (!rapi.Connected)
                {

...

 

2. if i try to catch an exception, the result is the same...  the catch is never reached and after the call to Connect() i'm "out" of the "button1_Click" method.

 

private void button1_Click(object sender, EventArgs e)
        {
            using (RAPI rapi = new RAPI())
            {

                try
                {
                    rapi.Connect();
                }
                catch (Exception ex)
                {
                    Text = "can't connect";
                    return;
                }

 

btw, i need it to connect synchronously

any suggestions?

Apr 20, 2010 at 2:13 PM

Hi,

A bit late picking this up, but maybe useful for someone else.

 

You could try using the following to see if the device is actually connected first:

                while (!myrapi.DevicePresent)
                {
                    Result = MessageBox.Show("Please connect your device to your PC using ActiveSync and before clicking the OK button.",
                      "No Device Present", MessageBoxButtons.OKCancel);

                    if (Result == DialogResult.Cancel)
                    {
                        MessageBox.Show("Please Click The Synchronise Button Again When You're Ready To Synchronise!");
                        return;
                    }

                }

 

You could then use the following to connect and check the connection

 

                // Connect to the Device, wait for connection for up to 3 seconds.
                myrapi.Connect(true, 3);

                if (!myrapi.Connected)
                {
                    MessageBox.Show("Unable To Connect To Your Device.\r\n" +
                                                "Please Ensure Your Device Is Docked Correctly Before Clicking The OK Button.\r\n");

                    DisconnectRAPI();
                    return;
                }

 

I'm not saying this is perfect, but it seems to work OK for me.