EEVblog Electronics Community Forum

Products => Computers => Programming => Topic started by: PerranOak on June 09, 2020, 04:27:12 pm

Title: C# concept confusion
Post by: PerranOak on June 09, 2020, 04:27:12 pm
I'm progressing nicely with C# but there are a couple of things I can't get my head around.

Below is an extract of a simple LED flash prog. where clicking the "Start" button begins the LED flashing then a "Stop" button sets stopflash to true:

    private void Start_Click(object sender, EventArgs e)
    {
      Start.Capture = false;
      if (!port.IsOpen) port.Open();

      stopflash = false;
      while (stopflash == false)
      {
        ledon();
        delay(300);
        ledoff();
        delay(300);       
     }
      if (port.IsOpen) port.Close();
      label1.Text = "End of flashing";
    }

...

    private void delay(int d)
    {
      System.Threading.Thread.Sleep(d);          //Wait d milliseconds.
      Application.DoEvents();                           //Action outstanding events.
    }

Two questions if I may:

1.
"Start.Capture = false" seems to be about the mouse click on the button "Start". I guess it says, "set the state of whether the mouse has been clicked to no". Why would you need this here? Surely, if the mouse in not clicked all is well and if it is then just carry on with the prog.?

2.
"Application.DoEvents" it says that this is needed as the delay will not happen until "the even is closed". What event?


These are not things I can really look-up so any help is very welcome, cheers.
Title: Re: C# concept confusion
Post by: Lindley on June 09, 2020, 08:09:16 pm
Cannot help directly as only just started with c# myself today, though think its more that it needs to be told its initial state, True or False, as you would with a Pic variable, you init it as eg 00 or  FF otherwise it just assumes any old power on value.

However have a look at this tutorial, if you open it up you will see its broken down into lessons /timeframe. Quiet a few other on Ytube if you do not like its presentation.
https://www.youtube.com/watch?v=GhQdlIFylQ8 (https://www.youtube.com/watch?v=GhQdlIFylQ8)

Just been using the Arduino with C# and this little comms tutorial and it worked ok, so it gives us something to follow and work from, expect the C# part could work with the Pic serial with a few changes.
https://www.youtube.com/watch?v=vHeG3Gt6STE (https://www.youtube.com/watch?v=vHeG3Gt6STE)
Title: Re: C# concept confusion
Post by: IanB on June 09, 2020, 08:24:14 pm
These are not things I can really look-up so any help is very welcome, cheers.

Absolutely you can look them up. There will be loads of answers online.

One thing to be aware of is that you are not really using C#, you are using .NET. In particular, you will want to look up how the C# and .NET event model works, also look up tutorials on Windows Forms programming and threads. Microsoft has volumes of documentation and tutorials online about C# and .NET. (By the way, I never look at YouTube videos for help, I always look for web pages. I find it much easier to read and digest written information with examples I can copy/paste.)

As to your particular question about Start.Capture = false, I would guess without looking it up that it sets the capture state of the Start button to off so that it no longer responds to mouse clicks. In other words, when Capture is set to false, clicking on the Start button will no longer have any effect. You can look this up to confirm: look up information about the Button control in Forms.

Edit: I'm not quite right about the Capture property, but here's a link to get you started in the documentation tree:

https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.capture?view=netcore-3.1
Title: Re: C# concept confusion
Post by: HwAoRrDk on June 09, 2020, 10:05:01 pm
One thing to be aware of is that you are not really using C#, you are using .NET.

Yes, this is an important thing to realise. Knowing the language of C# (i.e. syntax, concepts, etc.) does not get you very far. If you are developing desktop apps, 90% of it is dealing with the .Net Framework, so knowing what it provides for you is key.

As to your particular question about Start.Capture = false, I would guess without looking it up that it sets the capture state of the Start button to off so that it no longer responds to mouse clicks. In other words, when Capture is set to false, clicking on the Start button will no longer have any effect. You can look this up to confirm: look up information about the Button control in Forms.

If that's what they're trying to accomplish - preventing the button being clicked again until the flashing is stopped (presumably there is another counterpart 'Stop' button) - then using the Capture property is a funny way to do it. It would be best done by simply disabling the button by setting the Enabled property to false. This will make it so the button does not respond to user input, and also importantly, shows some visual feedback that the button is disabled by greying it out.

Something tells me this code example from whatever tutorial OP is following was written by someone not very familiar with the .Net Framework, or someone trying to apply paradigms from other platforms. I mean, a more sensible method of creating something that performs an action at a repeating interval would be to use a Timer (https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.timer?view=netcore-3.1) object. Then you'd put your ledon(), ledoff() in the Tick() event handler, and have the Start/Stop buttons just call the Start() and Stop() methods of the timer.
Title: Re: C# concept confusion
Post by: Lindley on June 10, 2020, 08:41:47 am
Just a footnote to your C# problems.

You have that kit to teach yourself about Pics and Assembly (or C  code ? )  which can be a task in itself.

Now to give you connection to a pc you have to start with a different complier / language /ide  like C# which I would have thought would be very confusing for any such beginner, unless that course provides all the ready made and working  code for you to just load and run it, without further learning ?

Some of the earlier Pickit programmers had a Usart tool to allow you to simulate a serial connection, do not know what chips or programmer you have.

Its also possible to use a connection adapter between your Pic and the PC and then use some free software like Putty or Terraterm to handle the data transfer.

However still think you have such a lot of other things you can do and learn with the Pics before confusing yourself with a PC connection.



Title: Re: C# concept confusion
Post by: PerranOak on June 10, 2020, 02:31:56 pm
Cheers, I'll look into learning/searching a bit more.

So, the language in my post is not C# but .NET? I didn't realise .NET was a language. I thought it was just some background thing that I didn't have to worry about and so would never have dreamed searching for info on it!

My manual does provide all the progs needed but I want to be able to do my own things too and to understand properly.
I've done the "assembly book" and the "C book" now it's the "comms book". However, I am now quite interested in C# (or .NET!) and so wish to understand that properly too.

I do find that general books on programming (assembly, C) are ok but they never deal with the things that PICs need like registers, ADCs and so on. How on earth do people find out the syntax for these things!?  :-//
Title: Re: C# concept confusion
Post by: IanB on June 10, 2020, 03:26:13 pm
So, the language in my post is not C# but .NET?

Well, the language is C#, but the programming environment is .NET.  The .NET framework is a kind of "virtual machine" that the C# code runs inside (called the CLR or Common Language Runtime). The .NET environment not only provides the managed runtime environment for the code, but it also provides a large set of support libraries that the code can use.

.NET also supports other languages like Visual Basic and F#, although C# is the "native" language.
Title: Re: C# concept confusion
Post by: bobcat2000 on June 12, 2020, 10:32:17 pm
Two questions if I may:

1.
"Start.Capture = false" seems to be about the mouse click on the button "Start". I guess it says, "set the state of whether the mouse has been clicked to no". Why would you need this here? Surely, if the mouse in not clicked all is well and if it is then just carry on with the prog.?

2.
"Application.DoEvents" it says that this is needed as the delay will not happen until "the even is closed". What event?


These are not things I can really look-up so any help is very welcome, cheers.

Let me try.  If I am wrong, please don't get mad.

#1 Windows is multitasking.  If you keep clicking the button, it will keep calling the function.  You want to prevent people from clicking the same button while the function is doing something.

#2 Windows is multitasking but the C# EXE program is not (kinda not).  In this case, if this C# function is running, other hardware event function (like clicking a mouse and pressing the keyboard) within the C# EXE will not be processed.  DoEvents tells this C# EXE program to start process (or listen) to the hardware events.  So, after you issue DoEvents, you can click the mouse button while this function is running.
Title: Re: C# concept confusion
Post by: Tony_G on June 13, 2020, 01:53:30 am
#2 Windows is multitasking but the C# EXE program is not (kinda not).  In this case, if this C# function is running, other hardware event function (like clicking a mouse and pressing the keyboard) within the C# EXE will not be processed.  DoEvents tells this C# EXE program to start process (or listen) to the hardware events.  So, after you issue DoEvents, you can click the mouse button while this function is running.'

Just stumbled over this thread, funnily enough, I was the original product manager for C# (over beers I can tell the story of how it was named, an 8-month tour through the darkness of human nature when it comes to product naming) so I hope I can provide a bit of clarity, C# is just a programming language - A compiler reads it and then produces a binary in Microsoft Intermediate Language (MSIL). This is pretty much the equivalent to Java's bytecode or other languages p-code implementations. You could liken this to the C/C++ compiler (which spits out an obj file). The Framework provides the classes (implementation) that your calling (C/C++ libs for example) and the CLR part of the Framework does all the "fix-up" to produce a runnable/running program (C/C++ linker) - This isn't quite accurate as the OS/Fx/CLR does stuff but from a conceptual point of view I hope it helps.

Let me deal with #2 from above - Windows is a true multi-tasking environment - You app can spawn any number of threads that will be pre-emptively scheduled for execution. The bit that Bobcat was alluding to is that the user interface in windows is single-threaded and it works using a message loop. Basically, every window has its own message loop and everything is a window (again simplification but hopefully good enough for explanation/concepts). When an application processes a message (like WM_EXIT) the UI thread passes into that windows message loop and so the UI is frozen until the thread returns to the global message loop. This doesn't affect other threads as they'll be scheduled and executed but just the UI thread. As you progress with the Framework, and I hope you do, you'll see concepts such as handling an event in a 'background' thread and sending it to the 'foreground' thread.

What DoEvents() does is to enable the processing of other messages in that UI thread so it keeps the UI looking 'alive' while your app does work. This is an older model and as was mentioned earlier in the thread you should be using tasks & eventing instead.

I hope this helps, and I ask for forgiveness as it has been over a decade and a half since I did Windows message-loop programming directly so I might have misnamed things or forgotten about changes that have been made. However, I think from a conceptual point of view this should help your understanding.

TonyG