Delay between two method

Beginner

Member
Joined
May 31, 2018
Messages
5
Programming Experience
Beginner
I'm working with the following code:




----> This is the main class:

C#:
class Program    {




        static void Main()
        {


            public class  a : CalledClassWrite.secondClass 
            {
                 public void ThreadOne()
                 {
                       for (int i = 0; i < 1000; i++)
                       {  
                     
                           
                       writeWhiteInk();
                       System.Threading.Thread.Sleep(2000);
                       writeBlackInk();
                       }
                 }
           }


       }
   }

---> This is the classed to be invoked
C#:
using System.Text;using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using System.Timers;




namespace CalledClassWrite
{
    class secondClass
    {
       
        DateTime utc = DateTime.Now;
        
        public void writeWhiteInk()
        {
            //Create a new file to write
            using (StreamWriter writer = new StreamWriter("C:\\Users\\xxxxx\\Desktop\\a.txt", true ))
            {


               writer.WriteLine("White");
               writer.WriteLine("{0}",utc);
               //Il valore della batteria sarà di input
               writer.WriteLine("% Battery" + Environment.NewLine );
               
            }
        }


        public void writeBlackInk()
        {
        //Create a new file to write
            TextWriter nf = new StreamWriter(@"C:\\Users\\xxxxx\\Desktop\\a.txt", true);
            nf.WriteLine("Black");
            nf.WriteLine("{0}",utc);
            nf.WriteLine("% Battery\r" + Environment.NewLine);
            
            nf.Close();
        }


        }
    }

Here the output

White
30/05/2018 17:23:20
% Battery


Black
30/05/2018 17:23:20
% Battery




White
30/05/2018 17:23:20
% Battery


Black
30/05/2018 17:23:20
% Battery

But the time is stop ever at 17:23:20, How can I create a delay between the method writeWhite and writeBlack, in c sharp 4.0(I cant' use the sleep, or await).Thanks.
 
"DateTime utc = DateTime.Now;" - this variable is set only once when class is created. Use the current time instead of this previously stored value.
 
DateTime.Now is the current time, 'uct' is a variable that hold a previous time value.
 
DateTime.Now is the current time, 'uct' is a variable that hold a previous time value.
I used task delay in my code,but no delay happens between the two method, why according to you?

--->Main code
C#:
namespace CalledClassWrite{
    class Program
    {
        static void Main()
        {
            secondClass WriteOnLabel = new secondClass();
            int x = 0;
            while (x < 100)
            {
                
                WriteOnLabel.writeWhiteInk();
                
                WriteOnLabel.Delay(1000);
                WriteOnLabel.writeBlackInk();
                x++;
            }


            Console.ReadLine();
        }


-->Class to be invoked

C#:
using System;using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using System.Timers;
using System.Threading;


namespace CalledClassWrite
{
    class secondClass
    {
        DateTime utc = DateTime.Now;




        public void writeWhiteInk()
        {


            //Create a new file to write


            using (StreamWriter writer = new StreamWriter(@"C:\\Users\\xxxx\Desktop\\a.txt", true))
            {
                
                writer.WriteLine("White");
               
                String time = string.Format("{0:HH:mm:ss tt}", DateTime.Now);
                writer.WriteLine(time);
               
                Console.WriteLine("Tempo etichetta Bianca :");
                Console.WriteLine(time);
                //Il valore della batteria sarà di input
                writer.WriteLine("% Battery" + Environment.NewLine);
            }
        }


        public void writeBlackInk()
        {


            //Create a new file to write


            TextWriter nf = new StreamWriter(@"C:\\Users\\xxxxx\Desktop\\a.txt", true);
            nf.WriteLine("Black");
            String  time = string.Format("{0:HH:mm:ss tt}", DateTime.Now);
            nf.WriteLine(time);
            
            Console.WriteLine("Tempo etichetta Nera :");
            Console.WriteLine(time);
            // nf.WriteLine("{0}", utc);
            nf.WriteLine("% Battery\r" + Environment.NewLine);
            // System.Threading.Thread.Sleep(4000);
            nf.Close();


        }


        public Task Delay(double milliseconds)
        {
            var tcs = new TaskCompletionSource<bool>();
            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Elapsed += (obj, args) =>
            {
                tcs.TrySetResult(true);
            };
            timer.Interval = milliseconds;
            timer.AutoReset = false;
            timer.Start();
            return tcs.Task;
        }
        




    }

The output is:

empo etichetta Bianca :
11:36:05
Tempo etichetta Nera :
11:36:05
Tempo etichetta Bianca :
11:36:05
Tempo etichetta Nera :
11:36:05
Tempo etichetta Bianca :
11:36:05
Tempo etichetta Nera :
11:36:05
Tempo etichetta Bianca :
11:36:05
Tempo etichetta Nera :
11:36:05
Tempo etichetta Bianca :
11:36:05
Tempo etichetta Nera :
11:36:05
Tempo etichetta Bianca :
11:36:05
Tempo etichetta Nera :
11:36:05
Tempo etichetta Bianca :
11:36:05
Tempo etichetta Nera :
11:36:05
Tempo etichetta Bianca :
11:36:05
Tempo etichetta Nera :
11:36:05
Tempo etichetta Bianca :
11:36:05
Tempo etichetta Nera :
11:36:05
Tempo etichetta Bianca :
11:36:05
Tempo etichetta Nera :
11:36:05
Tempo etichetta Bianca :
11:36:05
Tempo etichetta Nera :
11:36:05
Tempo etichetta Bianca :
11:36:05
Tempo etichetta Nera :
11:36:05
Tempo etichetta Bianca :
11:36:05
Tempo etichetta Nera :
11:36:05
Tempo etichetta Bianca :
11:36:05
Tempo etichetta Nera :
11:36:05
Tempo etichetta Bianca :
11:36:05
Tempo etichetta Nera :
11:36:05
Tempo etichetta Bianca :
11:36:06
Tempo etichetta Nera :
11:36:06
Tempo etichetta Bianca :
11:36:06
Tempo etichetta Nera :
11:36:06
Tempo etichetta Bianca :
11:36:06
Tempo etichetta Nera :
11:36:06
Tempo etichetta Bianca :
11:36:06
Tempo etichetta Nera :
11:36:06
Tempo etichetta Bianca :
11:36:06
Tempo etichetta Nera :
11:36:06
Tempo etichetta Bianca :
11:36:06
Tempo etichetta Nera :
11:36:06
Tempo etichetta Bianca :
11:36:06
Tempo etichetta Nera :
11:36:06
Tempo etichetta Bianca :
11:36:06
Tempo etichetta Nera :
11:36:06
Tempo etichetta Bianca :
11:36:06

I would like to print :
Tempo etichetta Bianca
11:36:05
Tempo etichetta Nera
11:36:06..

Each second I want print my variable Etichetta for each colour, White and Black.Any hint?
 
I see no reason for you to not use Thread.Sleep as in first code.
 
Back
Top Bottom