-2

I want to show how many times I open and close the valve in the text box in the application I control, but I can only see the result when the process is finished, I cannot see it in real time.

        private void button6_Click(object sender, EventArgs e) 
    {
        sayacValue = 0;
        int LoopCount = Convert.ToInt32(textBox_send.Text);
        
        for (int s = 0; s < LoopCount; s++)
        {
        OpenValf();
        IncreaseValfValue();
        System.Threading.Thread.Sleep(400);
        CloseValf();
        System.Threading.Thread.Sleep(400);
        }

    }

    

 public int IncreaseValfValue() //Counter Control Function
    {
        sayacValue++;
        sayac.Text = sayacValue.ToString();
        return sayacValue;
    }

How can I do it using Thread or any other method?

  • 1
    Why you think that when you block main(UI) thread there would be some changes on UI ? ... feel free to use `await Task.Delay` in `async` method... – Selvin Aug 23 '21 at 08:53
  • 1
    In addition to what Selvin said: 1. Do not trust user input. If `textbox_send` does not contain a valid integer, this will crash. 2. From my experience, it is crutial when dealing with hardware (which you seem to be dealing with) that you don't assume your requests have been carried out. I'd _always_ have a system of "1. Check value, 2. Send new value, 3. Check if new value has been propagated to device (=read again)". Also mind that WinForms is inherently _not_ "real time" (in the computer science definition of that). – Fildor Aug 23 '21 at 08:59
  • As a minimal change, why not try calling `sayac.Refresh()` after `sayac.Text = sayacValue.ToString()` ;? – kunif Aug 23 '21 at 09:52
  • @kunif It works but after 1 it says 3-5-7 – Muhammed Emin Kılınç Aug 23 '21 at 10:33
  • Maybe you're doing `sayacValue++;` elsewhere, or `IncreaseValfValue();` has been called twice. – kunif Aug 23 '21 at 10:48
  • Does this answer your question? [Wait before changing a Control's text after previous change](https://stackoverflow.com/questions/68557580/wait-before-changing-a-controls-text-after-previous-change) – GuidoG Aug 23 '21 at 12:24

1 Answers1

0

You should use Microsoft's Reactive Framework (aka Rx) - NuGet System.Reactive.Windows.Forms - then you can do this:

using System;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reactive.Linq;
using System.Reactive.Disposables;

namespace WindowsFormsApp
{
    public partial class ExampleForm : Form
    {
        public ExampleForm()
        {
            InitializeComponent();
        }

        private SerialDisposable _subscription = new SerialDisposable();

        private void button6_Click(object sender, EventArgs e)
        {
            if (int.TryParse(textBox_send.Text, out int LoopCount))
            {
                if (LoopCount > 0)
                {
                    _subscription.Disposable =
                        Observable
                            .Interval(TimeSpan.FromMilliseconds(400.0))
                            .Take(LoopCount + 1)
                            .ObserveOn(this)
                            .Subscribe(
                                x => sayac.Text = $"{x}",
                                () => sayac.Text = "Done.");
                }

            }
        }
    }
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172