2021-04-17

War and Peace - Programming in C#

A plug for my Kindle-published book,
Amazon Link:
"War and Peace C# Programming in Visual Studio"


After more years than can be admitted, keyliner has published a six-volume set of beginning programming tutorials.  2,100 pages, 1,300 illustrations.  

Search for "War and Peace Programming". 

Released on Kindle as a multi-volume set to keep printing costs low and print sizes manageable.  The electronic copy is color, the printed copy is black-and-white.  Fabulous in either medium, but I find the print version faster and easier to use. 


Because it is published on Kindle, it is inexpensive. 

Electronic versions are about $18 each
(roughly 300 to 500 pages per volume)

Printed editions are about $22 to $24.00 (8"x10" pages). 

Kindle takes a big cut, leaving a few dollars for each sale; this is not a money-making adventure.  Plus, they recently raised their fixed prices. Since these are textbooks with illustrations, a large tablet or a Kindle PC viewer is recommended. 


* From Kindle, choose a volume and download a sample, it will show in your Kindle reader.


Series Intro from Volume 1:

These books are different than most publications.

You will not find a "hello world" program. Nor will you find a Console application (at least not until Chapter 24).

Instead, after Chapter 1, you will immediately begin work on loops, if-statements and string-manipulation using honest-to-god Windows programs.  Little time is spent on theories and technical side-trips are rare. 

This means some topics, such as data-type conversions, numeric types, and other such concepts, are glossed over until they are more germane to the concepts being taught.

The goal is to have as much time on the keyboard, working with common business problems.  

After studying this book, and working through the examples, you will be a proficient programmer – able to write real programs that do real work.  Each topic has step-by-step instructions with lots-of code examples, with literally a thousand illustrations.

Illustrations are tightly cropped and annotated.
No full-screen, space-wasting pictures.

Details

Where other books might spend a page or two on a topic, this book dives into the most common and most useful ways to solve a problem.  For example, over 80 pages are dedicated to creating and opening multiple forms, and how to pass data between them.  Both "good ways" and "bad ways" are shown, because you will see these in the real world.


100 pages are spent on parsing, covering delimiters, CSV, Tab, Excel, and other techniques.  This is not over-kill.  This addresses real-world needs and goes beyond a brief description of Microsoft's ".split" command.  Parsing is hard.  I cover the tips and tricks you need to know.

Different techniques for the same problem are shown, and the benefits and drawbacks of each are explained.  If there is a chance of making a mistake in punctuation, style, or logic, you are shown how to identify and solve the problem.  Compiler errors are scattered throughout the book and there is a comprehensive alphabetic error reference in the appendix, with examples, likely causes, and recommended actions.

(Electronic publishing allows the books to be verbose, with details.) 
 

 Why "War and Peace"?  This thing is epic!

My books are different.  My books are better.

The Array chapter is 140 pages long with 80 illustrations (gasp!).  Learn the ins-and-outs about arrays, showing how to apply them to every-day business problems.  This goes beyond the basics of "this is an array and here is how you load it." 

I cover how to sort 2-dimensional arrays! -- something few books attempt, and yet this is a need that many programs have.  As you will learn, sorting 2D arrays is *not* native to the language, but with this chapter you will have the skills to do this. 
 
Most importantly, the Array chapter shows how to solve that perennial array problem, "How big to make the array when the size is not known."  This is solved with a beautiful technique that "over-allocates" the array and auto-grows in a manner that is smart and efficient.  This is much more than a simple "re-dimension" command and works with multi-dimensioned arrays.  This chapter 'rocks.'  

Chapter 26 (SQL data-entry screens) builds a complicated SQL data-entry panel that works exactly as your users want.  This panel properly exposes buttons and menus when needed, prompts users, does field-auditing, and myriad of other things that a well-written program must do. 

Step-by-step procedures are shown.  The skills taught here work in all kinds of programs, not just in database programs.  
 
Are these detailed chapters overkill?  No. 
 
There are a *hundred* other similar discussions. The text is clear, and the illustrations are actually helpful.

By the end of this series, you will be able to write complicated programs that are clean, organized, and can do real work. But all this detail takes time, and takes study.  This is not 'Learn C# in a week.'


Do you just want to try Visual Studio for a test drive?

Order Volume 6 - Instructor Student workbook
$10 Electronic, $13 Print - Cheap and fun



A small, light-weight book, with 5 projects.  Get your feet wet and have fun.  This is a stand-alone book and you do not need the other volumes to do this.

Project 1 is a Star Trek Bridge Computer Simulation.  Frivolous, yes, but loads of fun! 

It will take 2 to 3 hours and it will be mesmerizing. We are talking blinky lights! You can do this.  See it in action here: https://youtu.be/zRtoqZEPVLk



The other projects are more serious -- but most are small enough to complete in a short time.  These include a credit-card program, disk inventory, and an asset-tag manager for computers.

Learn real-world programming

Nothing fancy.  Little theory.  Just practical day-to-day skills that all programmers need.  Skills you need to write real-program -- Programs that parse files, generate reports, and extract data are the goals.

Illustrations are tightly-cropped, never showing a big-wasted full-screen. 
Every illustration has pointed annotations guiding you towards the goal.

Sample Illustration from the ASCII File Read Chapter:


 
Here is a more technical illustration from the CSV Parsing Chapters, showing arrays and array offsets:



A Phenomenal Utilities Library:


One of the benefits of this book is a library of utility modules that you will write, and in the process, learn all kinds of programming techniques. 

These utilities include commands that automate mundane tasks, such as parsing delimited files, punctuating phone numbers, street-addresses, and capitalizing proper names.  This book shows how to package these into re-usable libraries and how to link them into your projects.  These libraries save boat-loads of work and will be useful in all of your programs.


Volumes:

Volume 1:  650 pages
    1    Introduction to the Editor
    2    Introduction to Loops
    3    Conditional Branching
    4    Strings
    5    Numbers and Dates
    6    Utility Functions   *required by all other chapters
    7    Advanced Utility Functions *recommended for all other chapters
    A    Appendix A - Compiler Error Messages

Volume 2: 600 pages
    8    Class Libraries
    9    Variable Scope
    10    Form Controls and Events
    11    Calling Multiple Forms
    12    ASCII Files   
    13    Parsing Tab and CSV Files

Volume 3:  430 pages
    14    INI Files
    15    XML and App.config Files
    16    Windows Registry
    17    Reading Excel, ComboBoxes and SQL
    18    External Programs (Shell)
    19    Wait, Delays, Pauses
    20    Printing
    21    Formatting   
    B    Appendix B - Compiling and distributing code
    E    Appendix E - Building A860_BasicINIRead

Volume 4:  410 pages
    22    Arrays
    23    File Manipulation
    24    Console Applications
    D    Appendix D - Routines of Interest
           Obfuscating and Encrypting

Volume 5:  410 pages
    25    SQL Databases
    26    SQL Record Edits
    27    SQL Data Grids
    28    SQL Data Grid Cell Editing
    29    SQL Data Entry Forms
    C    Appendix C - Installing SQL Server Express

Volume 6:  310 pages  (standalone book)
    Project 1 - Write a Star Trek TOS Bridge Computer simulation
    Project 2 - Convert a photo into Red, Green, Blue and gray-scale
    Project 3 - Write a Visa Card Luhn Check Digit algorithm
    Project 4 - Write a Disk Directory Inventory Program with interesting counts|
    Project 5 - MD5 Checksum viewer for comparing files
    Project 6 - Computer Asset Tag and Network Status

Projects 4, 5, and 6 are programs that are actually useful and I run them on my own computers.  These are downloaded, as compiled programs, from "keyliner.com"


Software Needed for this series

Visual Studio Express
SQL Server Express

These tools are free from Microsoft. Kudos to them for doing this.  Instructions in Chapter 1.

All of the keyliner-developed programs available on this blog (such as the ASCII table program, the raffle PrizeSelect program), Midy5, and EarlKeeper, have been developed using the techniques described in these books.  



Editions:

3rd Edition, republished 2022-01 with new illustrations and text for Visual Studio 2022.  Substantially the same text as the 2020 version, but many small improvements, along with an expanded SQL section.  This should have been version 3.1 but Kindle does not allow decimals and it was not enough of a change to warrant a 4th edition.
 
3rd Edition, published 2020-04; consolidated Version 2's small chapters into Volumes, considerable editing changes.  Republished 2020-10 with a new Volume 6.

2nd Edition: published as individual chapters, 1-26, roughly $3 each.  This was a failed experiment and was too difficult for readers to navigate.  Kindle does not allow these to be deleted once published.



Publishing Humor:
 
When I first submitted these to normal publishers, they were polite, but laughed and said, "We can't print anything this big.  Plus the market is saturated.  We ain't gonna do it."   They were probably right, but I had a great time writing this.  I suspect you'll have a good time reading it.

My thanks to my good friend Steve C. for suggesting the title:  "War and Peace programming"  - much more memorable than "A Beginners guide: Introduction to programming in Visual Studio C-Sharp" -- gasp - what a lame title.


Related Links:Amazon Link:
"War and Peace C# Programming in Visual Studio"

Print Edition Errata

Source Code Downloads
(especially for Chapter 6 and 7 Utility Functions)
https://drive.google.com/drive/folders/1nubCFPLM4c4xUEGdQOimKxqDn8dsQXU1?usp=sharing


 

2021-04-12

Laptop battery drains unexpectedly while shutdown

Laptop battery drains unexpectedly while shutdown.
Article:  Windows 10 2021.04.16

Problem:
When a Windows 10 / Windows 11 laptop is shut down (not Sleep, not Hibernate), the battery meter shows excessive draining, visible even after a short over-night downtime.

This is a vexing problem, affecting many brands of computers.  I suspect multiple, simultaneous issues are at play.  Solved using a combination of Solution 1, Solution 2, and Solution 3


After much researching and snooping, I was surprised few of the sites mentioned a rather obvious Control Panel setting in Windows 10.  This was the real solution (#3) to the problem.  I joked with a friend, "I've never read this screen before..."

By design, if the laptop is in "Sleep" mode, the battery drains.  This article is only concerned about draining in "Shutdown" mode and assumes the laptop is not connected to AC power.


Solution 1:  "Disable Charge (USB) in Battery Mode"

If this is a newer laptop, it likely has the ability to charge a USB port (charge external devices) during low-power states (standby, hibernate, or power-off).  I believe this uses power to monitor if the port is active or not.  This is known by various names, such as USB Passthrough, USB External Charging, PowerShare, etc. 

A.  Boot into the laptop's BIOS Setup screens.  Steps vary by manufacturer or model.
      Often press F1, or F2 at the hardware boot banner (if displayed).
      Some vendors expose this setting in a settings screen. 
           With Lenovo, see the Vantage program.

B.  Note the BIOS version. 
      Needed by Step 2.
   
C.  Regardless of the BIOS version, continue:

      In the BIOS menus, locate the USB menu 
      For Lenovo:  BIOS, Setup, Config, USB

     Turn off:  "USB:  Always On USB"  or similarly-named feature.

D. Save BIOS changes with (F10)  Steps vary by mfg.

Drawback:  You won't be able to charge your cellphone, while connected to a powered-off laptop.


Solution 2:  BIOS Update

Research the laptop's vendor support page for the current BIOS version.  Even with a new laptop, the BIOS is likely older.   Early Intel motherboards had power management flaws.  

A.   BIOS update steps vary by manufacturer. 

With Lenovo, the BIOS can be updated through the desktop "Vantage" program.    

See "Recommended Updates" or download the BIOS from the support site.  BIOS updates are specific to your machine.   If downloading by hand, be sure to locate the correct machine.  The Vantage program's update makes this easy. 

With Dell, use your Service Tag and their website to download the latest BIOS.  Other vendors use similar techniques.


Solution 3: Power Button Settings and Turn Off Fast Startup

This is the key to this problem.  By default, many laptops (defaults) to "When I press the power button", the laptop goes to sleep!  Sleep is not the same as shutdown -- and sleep draws power.

A.  Open Power Options
      (this setting is not easily found in the Windows 10, 11 Gear icon; Use the Control Panel)
      Click Start, type "Control Panel"
      Select "Power Options"

B.  On left-nav, select link "Choose what the power buttons do"

C.  (If you are running in a non-administrative mode)
      Click center link, "Change settings that are currently unavailable"

D.  Continue with this IMPORTANT step
      Change the power button's behavior from "Sleep" to "Shutdown", as appropriate.
      Particularly with the "When I press the power button", both battery and plugged-in

      This was the key to the problem.

Incorrectly Set as "Sleep"

By default, your setting may be set to
"When I press the Power Button, go to Sleep" - where Sleep draws power. 

I recommend these changes:

* When I press the Power Button   On Battery:  "Shut down"
   When I press the Power Button   Plugged in:  "Shut down"

* When I close the lid:                    On Battery:  "Shut down"
   When I close the lid                     Plugged in:  "Shut down"

Why change in four places
Imagine you are plugged into power.  Then you shut the lid and stuff the laptop in a bag to catch your flight.  As illustrated above, the laptop goes into a Sleep-state, not a power-off state.  You'll forget, and the next day at your conference, half your juice is gone.  Set these to shutdown.

"Sleep" is still available on the Start Menu.  On some laptops, a long-press on the Power button is a shutdown, a short-press is Sleep.  This recommended change overrides this behavior.  How to tell which state you are in?  Sleep is a soft-boot and you will not see the hardware or BIOS screens.

 
E.  Finally, consider this change on the same screen:  "Fast Startup"

Numerous sites recommend unchecking "[ ] Turn on fast startup" (turn off, even though 'recommended').  Lenovo recommends turning this off for this problem.  I am not doing this myself.

Comment: There are reasons to use and not use "Fast Start".  See this article for a description of the feature:  The Pros and Cons of FastStart).  

But in relation to this article's problem (power-drain), this switch made no difference with the power-drain of a sleeping computer.  If checked, this consumes a small amount of extra processing during shutdown, but after shutdown, the PC is off and consumes no power except for a 1 or 2% drain per day that all rechargeable batteries have.  


Other Solutions - Battery Gauge Reset:

Not recommended:  Run "Battery Gauge Reset"

On an older computer (battery), the battery's daily-life-expectancy can change from what the battery-level meter expects.  By fully-charging, then fully-discharging the machine (with no sleep, hibernation,  power-savings, or shutting down), Windows re-calibrates the meter with the battery's actual abilities.  Lenovo provides a battery re-calibration program just for this purpose, but you can also easily do this by hand.  

Contrary to other web sites, this actually does re-calibrate the battery meter, but this will not fix an unexpected drain.  In other words, this is a waste of your time.  I ran a re-calibration on my new computer and it took 23 hours!  In the end, the battery "adjusted itself" from 44.72 watt-hours to 44.50 watt-hours, and the draining continued.  Older batteries might benefit from this, but this only fixes inaccurate power % readings, not premature draining.  The BIOS and Powerbutton-changes, steps 2 and 3 above, were the real solution.  

Not recommended for this problem:
A.  With the Lenovo, launch the Lenovo  desktop "Vantage" tool. 
      (Start, Run, "Vantage")
      Other vendors may have similar software.

B.  (Scroll past all the advertisements and premier enrollments),
      locating "Quick Settings".  Click "More Settings"

C.  Locate "Battery Gauge Reset"

This program walks you through a series of steps, where power is plugged in, then the laptop drains the battery (even with power), and then re-charges the battery.  This takes a horribly-long time. 


Disable Wake-on-LAN

In BIOS, disable Wake on LAN  (allows a network packet to boot/reboot) the PC.  This is only needed in a corporate environment and your laptop likely has this already disabled.

This is not likely a cure for this problem, but is worth looking at.


Related Problems:
Battery Meter fluctuates and is wildly inaccurate.
Battery meter unreliable. Battery meter erratic.   Battery meter unreliable.
Battery meter fluctuates.   Batter meter wildly inaccurate.  Battery meter wrong.

Many users, with many brands of computers (possibly all with Intel Motherboards) are reporting a problem where the Windows 10 System tray reports wildly-inaccurate "time to live" on the battery gauge.  

For example, my new Lenovo E14, on a fully-charged battery will report 6 hours of time left, then a few minutes later, it will say 2 hours, then a few minutes later will report 11 hours.  The battery is not malfunctioning, nor is the battery near discharge. 

I spoke to Lenovo about this:  "We have never seen this problem before."  They offered to repair the machine, but I declined, thinking this is really a software problem.

Clearly lots of people are seeing the problem and from what I can tell, there is not an obvious solution.  There is, however, a registry hack which is acceptable.  This hack hides the "3.5 hours remaining", leaving only the battery's percentage-left.  This does not fix the problem but it does hide the inaccurate appendage.  Changing from "89% (4 hours remaining)"  to "89%".

High-level details:
HKLM\System\CurrentControlSet\Control\Power
EnergyEstimationDisabled       = dword: 00000001
EnergyEstimationEnabled        = dword: 00000000
UserBatteryDischargeEstimator  = dword: 00000001

You will probably have to manually create the EnergyEstimationDisabled key by hand (a Dword).  Step-by-step on how to edit the registry are not in this article and I assume you have skills in this area.  If not, leave a comment and I consider expanding this article.

Close the Registry Editor.
Restart the computer

Hover mouse over battery meter.  Formerly, it would say:  "89% (2 hours remaining)".  Now it will report just the percentage "89%".   This is good enough.

2021: If I had a suspicion, this is a problem with Intel Motherboards and the "Intel Management Engine" drivers.  Updating to Lenovo's approved version, and then updating to Intel's latest version, which is newer than what Lenovo has tested, has not yet resolved the problem.