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 kig23 » Tue Apr 24, 2018 9:59 am

I'm sorry I lost some posts. The code I proposed requires a change. Here is the code.

Code: Select all
new Thread(delegate () {
    ChangeCurrTool ();
   }).Start();

#Events

private void ChangeCurrTool ()
{
   while (AS3.GetLED (25));
   Thread.Sleep (500);
   
   try
   {
      exec.Setcurrenttool (Convert.ToInt32 (exec.Readkey ("MyCNC", "CurrentTool", "False")));
   }
   catch ()
   {
      // do something to handle the exception
   }
}



dezsoe wrote:Nice! But, instead of "False" use "0", because "False" cannot be converted to Int32.


You're right Dezsoe, that "False" will raise un exception, but in this case is better to get exception and handle it with try-catch block. You will know that the constructor macro doesn't load the current tool value, rather than having the default value 0.
kig23
 
Posts: 114
Joined: Sat Mar 31, 2018 6:58 am
Location: Italy

Re: Problems using constructor and distructor to save curren

Postby dezsoe » Tue Apr 24, 2018 11:06 am

Yes, that's another point of view. If I want to hanle the problem, I set default to -1 and check it. But, both work! :)
dezsoe
 
Posts: 533
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Problems using constructor and distructor to save curren

Postby kig23 » Tue Apr 24, 2018 11:46 am

Thanks Dezsoe for sharing your opinion.
kig23
 
Posts: 114
Joined: Sat Mar 31, 2018 6:58 am
Location: Italy

Re: Problems using constructor and distructor to save curren

Postby kig23 » Tue Apr 24, 2018 2:08 pm

I migrated from Mach3 to UCCNC one month and a half ago. Maybe you already know it, but i found that if the value of a key in the .pro file is missing that will raise an exception. Only if in the .pro file is missing the key the Readkey () will get the default value. This was the reason to decide the use of exception handling. Normally I also prefer to avoid the use of exception handling.
kig23
 
Posts: 114
Joined: Sat Mar 31, 2018 6:58 am
Location: Italy

Re: Problems using constructor and distructor to save curren

Postby cncdrive » Tue Apr 24, 2018 9:10 pm

The exec.Readkey function does not raise an exception if the key to be read is missing.
Why you getting an exception with your code is because you reading a key and giving it a default value "False".
If the key is missing then the Readkey function will return this default "False" value, but you also converting the return value of the Readkey function to an Int32 type,
but the False value is boolean and is not a valid value for Int32 type. So, the Convert.ToInt32 function will raise the exception when it can't convert the returned False value to an Integer number.
You should change the default value to a number, e.g. to "0" instead of "False", if you don't want to receive an exception.
cncdrive
Site Admin
 
Posts: 2253
Joined: Tue Aug 12, 2014 11:17 pm

Re: Problems using constructor and distructor to save curren

Postby kig23 » Wed Apr 25, 2018 6:25 am

Thanks for your replay.
I know that if you try to convert bool to int this will raise an exception. But the point is other. Maybe i didn't express myself well. I wrote:
kig23 wrote: if the value of a key in the .pro file is missing that will raise an exception


i'm referring to the following situation. I've got a strange issue that UCCNC sometimes cancels the value of the key in the .pro file, not that the key is missing. When i opened the Default.pro file i found that the value of the key (CurrentTool ) is missing, like the following code.
Code: Select all
[MyCNC]
CurrentTool=


When i try to start UCCNC and read the key with the following code in constructor macro:
Code: Select all
new Thread(delegate () {
    LoadCurrToolValue ();
   }).Start();

#Events

private void LoadCurrToolValue ()
{
   while (AS3.GetLED (25));
   Thread.Sleep (500);
   
   exec.Setcurrenttool (Convert.ToInt32 (exec.Readkey ("MyCNC", "CurrentTool", "0")));
}

the missing CurrentTool value in the Default.pro file raises an exception (System.FormatException), rather than get the default value.
kig23
 
Posts: 114
Joined: Sat Mar 31, 2018 6:58 am
Location: Italy

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: 533
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: 114
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: 2253
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: 114
Joined: Sat Mar 31, 2018 6:58 am
Location: Italy

PreviousNext

Return to Macros

Who is online

Users browsing this forum: No registered users and 2 guests