Problems using constructor and distructor to save current...

This is where you talk about Macros, show examples of your macro scripting and SHARE handy segments of script code as examples.

Re: Problems using constructor and distructor to save curren

Postby dezsoe » Wed Apr 25, 2018 7:08 am

Yes, it's true that a missing value causes exception. But why is it missing?

I've got a strange issue that UCCNC sometimes cancels the value of the key in the .pro file

UCCNC alone never makes a missing value. Check the code where you write your key for that Writekey which writes no value. Maybe a conversion problem or a missing data to write. The only way you can make missing value is to write no value:

Code: Select all
exec.Writekey("MyCNC", "CurrentTool", "");

(I have a lot of data to read on startup and write back on exit. I had some similar problems when the startup macro could not run correctly and on exit there was no valid data to write. Now I turn on a LED in the last line of M99998 to show that it finished, and in M99999 I check this LED and if it's OFF then I do not save anything. Since I made this trick there was no data error.)
dezsoe
 
Posts: 595
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Problems using constructor and distructor to save curren

Postby kig23 » Wed Apr 25, 2018 7:23 am

Hi Dezsoe, thanks. Here is my code in destructor macro:
Code: Select all
exec.Writekey ("MyCNC", "CurrentTool", Convert.ToString(exec.Getcurrenttool ()));


I don't think the problem is in the code. I'm trying to reproduce the issue, but without success. Maybe I made some mistakes before that caused the missing value of the key in .pro file. Thanks again Dezsoe.
kig23
 
Posts: 121
Joined: Sat Mar 31, 2018 6:58 am
Location: Italy

Re: Problems using constructor and distructor to save curren

Postby cncdrive » Wed Apr 25, 2018 9:35 am

the missing CurrentTool value in the Default.pro file raises an exception (System.FormatException), rather than get the default value.


What causes the exception is that the key is missing and so the Readkey function will give the default value (3rd parameter of the function) to the key,
but your default value is a boolean type and then you trying to convert the boolean "False" value into integer which raises the exception, because False value is not Int32 type.
Ofcourse you could handle this as you do now with exception handling, but we've made the default value parameter of the Readkey function to handle this case, so it is more elegant to give a proper default value to the key.

The UCCNC will write the value into your profile. It will not leave the key in the profile empty except if the value you writting is an empty string.

I've noticed one thing is that you starting a Thread in the constructor macro which is OK, but I thought to mention to not start a Thread like that in the Destructor (I don't know if you doing or did that just thought to mention as a possible problem).
The issue if you starting a Thread in the Destructor is that when the main Thread code of the Destructor macro will finish then the UCCNC will start closing and if your Thread is a backgroundworker then that will be stopped when the UCCNC closes, so the Thread may not able to finish it's work and if it started writting, but meanwhile the UCCNC Closed that will be an issue, it could possibly leave an empty value key.
And if your Thread is not a backgroundworker then strange things may happen, likely the UCCNC will not close until your Thread finishes, but this is not guaranteed to always happen like that.
So, do not start Threads in the destructor macro is the key.
cncdrive
Site Admin
 
Posts: 2366
Joined: Tue Aug 12, 2014 11:17 pm

Re: Problems using constructor and distructor to save curren

Postby kig23 » Wed Apr 25, 2018 9:57 am

Thanks. I have a Thread only in constructor macro. Like i told before, i know that converting bool to int will raise un exception. If you look at the posts above you will see the discussion between me and Dezsoe. You'll see that i posted un example above and in this case Dezsoe agreed with me that will raise un exeption. I'm talking about missing value not missing key, like this:

[MyCNC]
CurrentTool=

If in the Default.pro file you have the code above, at UCCNC starup, when try to read the value of this key you'll get un exception and not the defaul value that is to 0.
kig23
 
Posts: 121
Joined: Sat Mar 31, 2018 6:58 am
Location: Italy

Re: Problems using constructor and distructor to save curren

Postby cncdrive » Wed Apr 25, 2018 10:16 am

If in the Default.pro file you have the code above, at UCCNC starup, when try to read the value of this key you'll get un exception.


Well, in that case still not the Readkey function will throw the exception, but your Int32 conversion.
If the key is defined but it's value is empty "", then the software will read the empty string value, the Readkey will not throw an exception,
but when you trying to convert the empty string to Int32 that will throw an exception.
For this scenario you can really use an exception handling or a TryParse, like:

Code: Select all
int variable = 0;
int.TryParse(stringValue, out variable);


If the string value can't be parsed to integer type then the value will be the value before the TryParse which is 0 in the above example.
cncdrive
Site Admin
 
Posts: 2366
Joined: Tue Aug 12, 2014 11:17 pm

Re: Problems using constructor and distructor to save curren

Postby kig23 » Wed Apr 25, 2018 10:47 am

Thanks for your replay.
kig23
 
Posts: 121
Joined: Sat Mar 31, 2018 6:58 am
Location: Italy

Previous

Return to Macros

Who is online

Users browsing this forum: No registered users and 2 guests

cron