Multithreading or something else

This is the first time I encounter such a problem. Not being my profession, but only my hobby, I have no previous links. In my program, I added one at a time several functions to control the machine. After I added the last function (temperature measurement), I began to experience problems with other functions (about 8 of them work together. The problem I am experiencing is a diagram (engine RPM), which is not related to it but to it you see the difference between the two diagrams with and without temperature measurement.The real engine speed is the same in both diagrams, and in the second I lose pieces on the fly because the application slows down.

No temperature function. enter image description here With temperature function enter image description here

In particular, this function interferes with the above management, and I think this is because the workload becomes heavy for the application and or because I need a selection, so there is some time waiting for them to be received:

private void AddT(decimal valueTemp) { sumTemp += valueTemp; countTemp += 1; if (countTemp >= 20) //take 20 samples and make average { OnAvarerageChangedTemp(sumTemp / countTemp); sumTemp = 0; countTemp = 0; } } private void OnAvarerageChangedTemp(decimal avTemp) { float val3 = (float)avTemp; decimal alarm = avTemp; textBox2.Text = avTemp.ToString("F"); if (alarm > 230) { System.Media.SoundPlayer player = new System.Media.SoundPlayer(); player.Stream = Properties.Resources.alarma; player.Play(); timer4.Start(); } else { timer4.Stop(); panel2.BackColor = SystemColors.Control; } } 

I am wondering if this function allowed another thread to solve the problem and how can I do this? Or, if there is another way to solve the problem. Rate the code.

Update, add a method call.

So I call the AddT method

 if (b != "") { decimal convTemp; //corrente resistenza decimal.TryParse(b, out convTemp); AddT(convTemp);} 

This is how I get data from the serial number and pass it to a class that removes unnecessary characters and returns values ​​to other variables. This is a class that removes unnecessary characters and returns values. And this is how I manage sequential incoming data. Please do not laugh at me after seeing my coding. I do other work, and I study myself.

+6
source share
2 answers

It is very difficult to say if something is wrong and what it might be - it looks like a subtle problem.

However, if you reorganize your code, it may be easier to get information about these things. There are many things in the code that you showed that make it more difficult than necessary to reason about what is going on.

  • You use float , and decimal - float not exact, but small and fast; decimal (trying) to be accurate, but especially predictable, because it bypasses errors the way a person can be in base-10, but it is rather slow and usually designed for calculations where accurate reproducibility is needed (for example, financial information). You should probably use double everywhere.
  • You have useless else {} code in the Stripper class.
  • Your Stripper is an unstable class when it should be just a static class with a static method - Stripper is stateless.
  • You catch exceptions only to restore them.
  • You use TryParse and do not test success. Usually you only use TryParse if you (a) expect the parsing to fail, and (b) can handle this analysis failure. If you do not expect failure or cannot handle it, you will be better off with a crash, which you will learn about shortly, than slightly incorrect values.
  • In stripping, you duplicate variables such as _currentMot , currentMot and param4 , but they are identical - use only the parameter and give it a logical name.
  • You are using out parameters. It is almost always better to define a simple struct and return it instead - this also allows you to ensure that you cannot mix variable names easily, and it is much easier to encapsulate and reuse functionality, since you do not need to duplicate the definition of a long call and argument.
  • Your parsing logic is too fragile. You should probably completely avoid Replace and instead explicitly do Substring without the characters you marked, and you have some weird things named like test1 and test2 that refer to lastChar that don't refer to the last character - this might be OK but the best names can help keep things right in your head too.
  • You have incorrect code comments ( decimal convTemp; //corrente resistenza ). I usually avoid all purely technical code comments; it is better to use descriptive variable names, which are another form of self-documenting code, but one in which the compiler can at least check to see if you use them sequentially.
  • Rather, return 4 possibly empty values, your Stripper should probably accept the "sink" parameter, to which it can directly call AddT AddD and AddA .

I don't think any of the above questions will fix your problem, but I believe that they help keep your code a little cleaner and (ultimately) make it easier to find problems.

+1
source

Your problem is parsing the values ​​you have

 decimal.TryParse(a, out convRes); AddA(convRes); 

and do not check for failed values, you only accept the value if it returns true

 if(decimal.TryParse(a, out convRes)) { AddA(convRes); } 

you may have more errors, but this makes you handle the value 0 every time TryParse fails.

+1
source

Source: https://habr.com/ru/post/948730/


All Articles