PDA

View Full Version : سوال: ثانیه شمار در کنسول



drsina
یک شنبه 24 شهریور 1392, 18:57 عصر
سلام !
راستش خیلی دنبال این مطلب تو اینترنت گشتم و به نظرم بهترینش این بود http://stackoverflow.com/questions/186084/how-do-you-add-a-timer-to-a-c-sharp-console-application

حالا میشه با یه حلقه For بی نهایت این محاسبه را انجام داد ، ولی برنامه تو حلقه For می مونه !!!
سوالم اینه که میشه بدون استفاده از نخ اینکارو کرد ؟!

یعنی می خوام یه متغیر int داشته باشم ، و در هر ثانیه ، یکی به اون اضافه بشه و تو کنسول مرتبا آپدیت و نمایش داده بشه ، مثلا متغیر TimeElasped !!؟

aliagamon
یک شنبه 24 شهریور 1392, 20:14 عصر
یه همچین چیزی میخواین؟:

static int Counter = 0;
static void Main(string[] args)
{
Timer tm=new Timer(tm_tick,null,TimeSpan.FromSeconds(1),TimeSpa n.FromSeconds(1));
Console.Write("");
Console.ReadKey();
}
static void tm_tick(object obj)
{
Console.Clear();
Console.WriteLine("Timer:{0}",Counter);
Counter++;
}

drsina
یک شنبه 24 شهریور 1392, 20:23 عصر
این تایمر تووو system.threading هست یا تووو system.timers ?

drsina
یک شنبه 24 شهریور 1392, 20:27 عصر
فکر نکنم این باشه ، این کاری رو که می خوام انجام میده ولی با System.Windows.Forms; مشکل پیدا میکنه !!!! تو پروژم از اشیای System.Windows.Forms; هم استفاده کردم !!!!:متفکر:

بعد کله پروژه از بین میره !!! و فقط تایمر میاد !!!
این کد منه :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Windows.Forms;
using System.Threading;

namespace BeginnerLevel
{
class Program
{
static int Counter = 0;
static void Main(string[] args)
{

Timer tm = new Timer(tm_tick, null, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));


int Numofchances;
int Marks = 0;
int TimeElapsed = 0;





Random tasadofi = new Random();
string[] words = new string[20];
words[0] = "Word1";
words[1] = "Word2";
words[2] = "Word3";
words[3] = "Word4";
words[4] = "Word5";
words[5] = "Word6";
words[6] = "Word7";
words[7] = "Word8";
words[8] = "Word9";
words[9] = "Word10";
words[10] ="Word11";
words[11] ="Word12";
words[12] ="Word13";
words[13] ="Word14";
words[14] ="Word15";
words[15] ="Word16";
words[16] ="Word17";
words[17] ="Word18";
words[18] ="Word19";
words[19] = "Word20";








Console.Beep();
Console.Write("Chances : \t \t");
Console.Write("Marks : \t \t");
Console.Write("");
Console.Write("Time Elasped : \t \t ");
// Console.Write(Timer());
Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine(" ");





int WordsMissing = tasadofi.Next(0, words.Length);



// Console.WriteLine(words[tasadofi.Next(1, 20)]);
// Console.WriteLine(SelectedWord.Substring(SelectedW ord.Length));






string SelectedWord = words[tasadofi.Next(0, 19)];
// Console.WriteLine(SelectedWord);


int WordsMissing1 = tasadofi.Next(0, SelectedWord.Length);
int WordsMissing2 = tasadofi.Next(0, SelectedWord.Length);
int WordsMissing3 = tasadofi.Next(0, SelectedWord.Length);
char underline = '-';
string WordMissingNum1 = SelectedWord.Substring(WordsMissing1, 1);
string WordMissingNum2 = SelectedWord.Substring(WordsMissing2, 1);
string WordMissingNum3 = SelectedWord.Substring(WordsMissing3, 1);

// Console.WriteLine(WordMissingNum1);


string Word = SelectedWord.Replace(WordMissingNum1, underline.ToString());
Word = Word.Replace(WordMissingNum2, underline.ToString());
Word = Word.Replace(WordMissingNum3, underline.ToString());
string FinalWord = Word;



Console.WriteLine("\t \t \t \t \t" + FinalWord);


Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine(" ");

Console.Write("Guess : ");
string WordGuested = Console.Read().ToString();


Console.WriteLine(" ");
Console.WriteLine(" ");
int TotalChar = SelectedWord.Length;
Console.Write("Total Characters : "+ TotalChar.ToString());





Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine(" ");










//*********************************************
Console.ReadLine();

}
static void tm_tick(object obj)
{
Console.Clear();
Console.WriteLine("Timer:{0}", Counter);
Counter++;
}
}
}

aliagamon
یک شنبه 24 شهریور 1392, 20:33 عصر
این تایمر تووو system.threading هست یا تووو system.timers ?
تو System.Threading
دومی هم سورس طولانیه نخوندم ... :لبخند:اما اگه مشکل اینه که نوشته ها پاک میشه دلیلش اینه که من از clear استفاده کردم که کنسولو پاک میکنه اون خطو پاک کنین ...
اما در کل تو کنسول هر کاری بکنی متن هارو نمیتونی به اون شکلی که میخوای اپدیت کنی ...

drsina
یک شنبه 24 شهریور 1392, 20:46 عصر
آره ×!!! :لبخند: کلیر رو ندیده بودم ...
هی پشت سر هم میاد !!!

با thread هم امتحان کردم ، تقریبا همینه !!! نمیشه اینجوری . .. :گریه:

راهی دیگه نی ؟!

drsina
یک شنبه 24 شهریور 1392, 21:39 عصر
سلام !
حل شــــــــــــــــــــــــ ـــــــــــــــــــــــــ ـــــــــــــــــــــــــ ــــد.
تو stackoverflow مطرح کردم ، یه بنده خدای جوابمو داد !!! :قلب:

به جا این خط
Console.WriteLine("Timer:{0}",Counter);


این خط رو گذاشتم :
Console.Write("\rTimer:{0}", Counter);


لینکش هم اینه : http://stackoverflow.com/questions/18815166/a-timer-with-c-sharp-console