14 Replies - 1019 Views - Last Post: 22 May 2015 - 07:55 AM Rate Topic: -----

#1 dan789  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-May 15

C# Stops Executing When Opening A Connection To MySql

Posted 22 May 2015 - 05:22 AM

I am writing an application to transfer data from a simulated OPC server to a MySql Database. I am using some of the libraries from OPC Foundation - OPCNETAPI.DLL, OPCNETAPI.COM.DLL.

I have the program reading from the server happily, but it seems as soon as I open a connection inside the loop of reading data, it loops about 5 times and then stops executing.. There are no exceptions thrown and I have tried for hours to work it out and miserably failed.

Hopefully some one may know whats going on, but there is nothing obvious, hopefully the following code will spark ideas!

**Windows Forms Code**

 public void readplc()
            {
                Opc.URL url = new Opc.URL("opcda://localhost/Matrikon.OPC.Simulation.1");
                Opc.Da.Server server = null;
                OpcCom.Factory fact = new OpcCom.Factory();
                server = new Opc.Da.Server(fact, null);
                try
                {
    
                    server.Connect(url, new Opc.ConnectData(new System.Net.NetworkCredential()));
                }
                catch (Exception ecy)
                {
    
                }
                // Create a group
                Opc.Da.Subscription group;
                Opc.Da.SubscriptionState groupState = new Opc.Da.SubscriptionState();
                groupState.Name = "Group";
                groupState.Active = true;
                group = (Opc.Da.Subscription)server.CreateSubscription(groupState);
                // add items to the group.
                Opc.Da.Item[] items = new Opc.Da.Item[2];
                items[0] = new Opc.Da.Item();
                items[0].ItemName = "Random.Int1";
                items[1] = new Opc.Da.Item();
                items[1].ItemName = "Random.Time";
                items = group.AddItems(items);

                    group.DataChanged += new Opc.Da.DataChangedEventHandler(OnTransactionCompleted);

            }
    
            public void OnTransactionCompleted(object group, object hReq, Opc.Da.ItemValueResult[] items)
            {

                for (int i = 0; i < items.GetLength(0); i++)
                {
                 
                    try
                    {
    
                        string a = items[i].Value.ToString();
                        string b = items[i].ItemName;
                        string c = items[i].Key;

                        MySqlConnection conn = new MySqlConnection("server=localhost;user id=hp42mis;password=hp42mis;database=hp42mis;pooling=false"); 
                        conn.Open();
                        conn.Close();
                    }
                    catch (Exception ec)
                    {
                    }
                    
                }
    
            }



I have set break points throughout the code, stepped through everything possible and still nothing obvious that's causing the issue.

Is This A Good Question/Topic? 0
  • +

Replies To: C# Stops Executing When Opening A Connection To MySql

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5955
  • View blog
  • Posts: 20,418
  • Joined: 05-May 12

Re: C# Stops Executing When Opening A Connection To MySql

Posted 22 May 2015 - 05:41 AM

Is readplc() called in your Form Load event handler? Are you running on a 64-bit OS? If so, exceptions are eaten by the OS for your protection. Try testing your code in a Console first, or move the call outside of the form load event.
Was This Post Helpful? 0
  • +
  • -

#3 dan789  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-May 15

Re: C# Stops Executing When Opening A Connection To MySql

Posted 22 May 2015 - 05:45 AM

The crazy thing is, this code works fine in a console app.

The call to ReadPLC() was in the form1Load event, I have just tried putting it into a button_click event, but seems to make no change

This post has been edited by Skydiver: 22 May 2015 - 06:24 AM
Reason for edit:: Removed unnecessary quote. No need to quote the message above yours.

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5955
  • View blog
  • Posts: 20,418
  • Joined: 05-May 12

Re: C# Stops Executing When Opening A Connection To MySql

Posted 22 May 2015 - 05:51 AM

Now that you've move it to the click event, have you told Visual Studio to catch first chance exceptions (or reset your breakpoints)? With all of your own code that blindly eats exceptions, are you 100% sure that you aren't just eating an exception as well?
Was This Post Helpful? 0
  • +
  • -

#5 dan789  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-May 15

Re: C# Stops Executing When Opening A Connection To MySql

Posted 22 May 2015 - 05:59 AM

Sorry, forgot to mention I am using X64 OS,

I have just changed it to break when an exception is thrown, and reset break points, still no change

This post has been edited by Skydiver: 22 May 2015 - 06:24 AM
Reason for edit:: Removed unnecessary quote. No need to quote the message above yours.

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5955
  • View blog
  • Posts: 20,418
  • Joined: 05-May 12

Re: C# Stops Executing When Opening A Connection To MySql

Posted 22 May 2015 - 06:23 AM

Sorry, I don't know what else to suggest that I have a lot of confidence in. Your code is almost exactly like the sample code that they provide.

For grins, I would try making the variables fact, server, and group into class variables instead of local variables in case they are getting garbage collected early.
Was This Post Helpful? 0
  • +
  • -

#7 dan789  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-May 15

Re: C# Stops Executing When Opening A Connection To MySql

Posted 22 May 2015 - 06:24 AM

Will give it a go, thanks alot for the advice though, appreciate it.

This post has been edited by Skydiver: 22 May 2015 - 06:49 AM
Reason for edit:: Removed unnecessary quote. No need to quote the message above yours.

Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13666
  • View blog
  • Posts: 54,545
  • Joined: 12-June 08

Re: C# Stops Executing When Opening A Connection To MySql

Posted 22 May 2015 - 06:26 AM

51	                   catch (Exception ec)

What's the exception's message here?
Was This Post Helpful? 0
  • +
  • -

#9 dan789  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-May 15

Re: C# Stops Executing When Opening A Connection To MySql

Posted 22 May 2015 - 06:43 AM

Tried putting it in an external class, still didn't make a difference :(
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5955
  • View blog
  • Posts: 20,418
  • Joined: 05-May 12

Re: C# Stops Executing When Opening A Connection To MySql

Posted 22 May 2015 - 06:51 AM

But are keep the instance of that external class alive? This is why I suggested that you make the variable members of the current form which you know is alive.

View Postmodi123_1, on 22 May 2015 - 09:26 AM, said:

51	                   catch (Exception ec)

What's the exception's message here?


The OP claims that he's enabled catching first chance exceptions and none are thrown. So therefore, there is no message for him to look at.
Was This Post Helpful? 0
  • +
  • -

#11 dan789  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-May 15

Re: C# Stops Executing When Opening A Connection To MySql

Posted 22 May 2015 - 06:54 AM

View PostSkydiver, on 22 May 2015 - 06:51 AM, said:

But are keep the instance of that external class alive? This is why I suggested that you make the variable members of the current form which you know is alive.

View Postmodi123_1, on 22 May 2015 - 09:26 AM, said:

51	                   catch (Exception ec)

What's the exception's message here?


The OP claims that he's enabled catching first chance exceptions and none are thrown. So therefore, there is no message for him to look at.


What do you mean by Alive?

When the program stops executing, I can't see no locals at all, even though it's still in debug mode.
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13666
  • View blog
  • Posts: 54,545
  • Joined: 12-June 08

Re: C# Stops Executing When Opening A Connection To MySql

Posted 22 May 2015 - 06:55 AM

Hmm.. okay. It didn't seem overly clear on what the problem was.
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5955
  • View blog
  • Posts: 20,418
  • Joined: 05-May 12

Re: C# Stops Executing When Opening A Connection To MySql

Posted 22 May 2015 - 06:59 AM

View Postdan789, on 22 May 2015 - 09:54 AM, said:

View PostSkydiver, on 22 May 2015 - 06:51 AM, said:

But are keep the instance of that external class alive? This is why I suggested that you make the variable members of the current form which you know is alive.


What do you mean by Alive?


Keep a reference to that class within a class that you know active. Read about garbage collection here.
Was This Post Helpful? 0
  • +
  • -

#14 dan789  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-May 15

Re: C# Stops Executing When Opening A Connection To MySql

Posted 22 May 2015 - 07:05 AM

View PostSkydiver, on 22 May 2015 - 06:59 AM, said:

View Postdan789, on 22 May 2015 - 09:54 AM, said:

View PostSkydiver, on 22 May 2015 - 06:51 AM, said:

But are keep the instance of that external class alive? This is why I suggested that you make the variable members of the current form which you know is alive.


What do you mean by Alive?


Keep a reference to that class within a class that you know active. Read about garbage collection here.


I don't understand why this could be GarbageCollection, for one, it works fine in a console application and for two, if I remove the 3 lines of code refencing to MySql, the program works fine.
Was This Post Helpful? 0
  • +
  • -

#15 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5955
  • View blog
  • Posts: 20,418
  • Joined: 05-May 12

Re: C# Stops Executing When Opening A Connection To MySql

Posted 22 May 2015 - 07:55 AM

If it's the calls to MySQL that is failing, then exceptions should have been thrown. Add a MessageBox call within your exception handler just to ensure that you really are not missing any exceptions.

I don't know if MySQL needs to be running on a particular flavor of thread (eg. STA, UI, etc.) and if the DataChange events fired by OPC satisfies the requirements of MySQL. It maybe something to look into if no exceptions are being thrown.

Additionally, in Visual Studio, make sure to uncheck the "Just My Code" under the debugging options. It's possible that VS is trying to help you by hiding the MySQL or OPC code when the exception is thrown.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1