Home > Net >  Known string "adcdefg" how to use c # timer every 1 s output characters?
Known string "adcdefg" how to use c # timer every 1 s output characters?

Time:09-22

1 s output a, for example, output b 2 seconds, 3 seconds output c

CodePudding user response:

 
[TestMethod]Public void TimerTest ()
{
String woutStr="abcdefg";
int index=0;
The Timer Timer=new Timer (new TimerCallback (obj=& gt;
{
The Debug. WriteLine (woutStr [index % woutStr Length]);
Index++;
}), null, 0, 1000);
The timer. Change (0, 1000);
//wait for output
Thread.sleep (1000 * 10);
}

CodePudding user response:

Use nuget introduce rx.net

The
reference
Install - Package System. Reactive


The
reference
string x="abcdefghijk";
Var ob1=x.T oObservable ();
Observables. Interval (TimeSpan. FromSeconds (1))
.zip (ob1, (a, b)=& gt; B)
The Subscribe (p=& gt; {the Console. WriteLine (p); });

CodePudding user response:

refer to the second floor wanghui0380 response:
use nuget introduce rx.net

The
reference
Install - Package System. Reactive


The
Quote:
string x="abcdefghijk";
Var ob1=x.T oObservable ();
Observables. Interval (TimeSpan. FromSeconds (1))
.zip (ob1, (a, b)=& gt; B)
The Subscribe (p=& gt; {the Console. WriteLine (p); });


Just want to say Rx found that has already been answered

CodePudding user response:

This also line
 string STR="abcdefghijk"; 
Var observables=
Observables. Generate (x=0, & gt; X & lt; STR. Length, x=& gt; X + 1, x=& gt; X,
X=& gt; TimeSpan. FromSeconds (1)). Timestamp ();

Observables. Subscribe (p=& gt; Console. WriteLine (${" STR [p.V alue]}, {p.T imestamp} "));

CodePudding user response:

So still can
 observables. Generate (" abcdefghijk ". GetEnumerator (), 
Events=& gt; Events. MoveNext (),
Events=& gt; Events,
Events=& gt; Events. The Current,
X=& gt; TimeSpan. FromSeconds (1))
The Timestamp ()
The Subscribe (p=& gt;
{
Console. WriteLine (${" p.V alue}, {p.T imestamp} ");

});

CodePudding user response:

Is operating in the winform, first seconds display a, more than 2 seconds shows a b, the output is ab, more than 3 seconds according to c, output is ABC, until the final string all displayed

CodePudding user response:

refer to 6th floor weixin_40428416 response:
is operating in the winform, first seconds display a, more than 2 seconds shows a b, the output is ab, more than 3 seconds according to c, output is ABC, until finally the string displayed all


 observables. Generate (" abcdefghijk ". GetEnumerator (), 
Events=& gt; Events. MoveNext (),
Events=& gt; Events,
Events=& gt; Events. The Current,
X=& gt; TimeSpan. FromSeconds (1))
ObserveOn (SynchronizationContext. Current)
The Subscribe (p=& gt; {this. TextBox1. Text +=p; });

CodePudding user response:

String triple="abcdfg";
Int I=1;
Private void Form20_Load (object sender, EventArgs e)
{

}

Private void timer1_Tick (object sender, EventArgs e)
{
I=I + 1;
If (I & lt;=aaa. Length)
{
TextBox1. Text=aaa. The Substring (0, I);
}


}
}

CodePudding user response:


 char c='a'; 
New Timer ((x)=& gt;
{
Console. WriteLine (c + +);
}). Change (0, 1000);

CodePudding user response:

Can be achieved, thank you very much

CodePudding user response:

Cout< [a * 2], Sleep (444), was originally thought it was a c + + c #?

CodePudding user response:

Is not the most simple should use a timer?

CodePudding user response:

Use the timer,,,

CodePudding user response:

Is a dynamic effect, word by word to do animation timing without too accurate delay line

CodePudding user response:

After each output, sleep (1000)

CodePudding user response:

After all the.net asynchronous programming specification has been out for many years, many programming languages are basically uniform, this should be common sense to learn, as a result, the most relevant timer, events, and so on mechanism could be abandoned ,
 using System; 
Using System. The Threading. The Tasks;

The namespace ConsoleApp1
{
Class Program
{
The static void Main (string [] args)
{
The Test ();
Console. WriteLine ("... Press any key to end ");
The Console. ReadKey ();
}

Private static async void Test ()
{
Foreach (var c in string \ "known" adcdefg \ "how to use c # timer every 1 s output character?" )
{
The Console. Write (c);
Await Task. Delay (500);
}
}
}
}

For winform, just that one line of code to control the output characters. The BeginInvoke entrusted to operate controls, other asynchronous programming logic is the same,

CodePudding user response:

You can write in the winform
 using System; 
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related