I am trying to demonstrate file transfer in WPF so that it refreshes the screen in real time and shows the current data. My requirement is it should show the current file which is being transferred. This is my code I have written.
public MainWindow()
{
InitializeComponent();
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += tick_event;
timer.Start();
}
private void tick_event(object sender, EventArgs e)
{
for (int i = 0; i < 60; i++)
{
txtName.Text = "Transfering file no. "+i.ToString();
}
}
what I have tried is for instance there are 60 files and the screen show me which file is being transferred. But, when I run this code it shows me only the last file which is 59th file.
What changes I can make here to make this work? Thanks in advance.
When you say
it shows me only the last file which is 59th file
It is because when the event triggers it counts from 0 to 59 and it writes those numbers with no delay into txtName. The consecuence is that you only see the latest written number.
You should set a counter and in the timer event only write the counter variable.
DispatcherTimer timer = new DispatcherTimer();
private int filesCopied = 0;
public MainWindow()
{
InitializeComponent();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += tick_event;
timer.Start();
}
private void tick_event(object sender, EventArgs e)
{
filesCopied++;
txtName.Text = "Transfering file no. " + filesCopied.ToString();
if (filesCopied >= 60) timer.Stop();
}
Related
I have a WPF application that includes a countdown timer, I'm stuck with the formatting part of it, I have little to no experience with programming and this is my first time using c#. I want to countdown from 15 minutes using DispatchTimer, but as of now, my timer only counts down from 15 seconds, any ideas?
My countdown timer so far:
public partial class MainWindow : Window
{
private int time = 15;
private DispatcherTimer Timer;
public MainWindow()
{
InitializeComponent();
Timer = new DispatcherTimer();
Timer.Interval = new TimeSpan(0,0,1);
Timer.Tick += Timer_Tick;
Timer.Start();
}
void Timer_Tick(object sender, EventArgs e) {
if (time > 0)
{
time--;
TBCountDown.Text = string.Format("{0}:{1}", time / 60, time % 60);
}
else {
Timer.Stop();
}
}
The output looks like this:
A better approach would be to do it with a TimeSpan rather than an int with a number. Setting the TimeSpan value in the following application will do the countdown as I want.
TimeSpan.FromMinutes for minutes
TimSpan.FromSeconds for seconds
You can check here for more detailed information.
public partial class MainWindow : Window
{
DispatcherTimer dispatcherTimer;
TimeSpan time;
public MainWindow()
{
InitializeComponent();
time = TimeSpan.FromMinutes(15);
dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Interval = TimeSpan.FromSeconds(1);
dispatcherTimer.Tick += DispatcherTimer_Tick;
dispatcherTimer.Start();
}
private void DispatcherTimer_Tick(object sender, EventArgs e)
{
if (time == TimeSpan.Zero) dispatcherTimer.Stop();
else
{
time = time.Add(TimeSpan.FromSeconds(-1));
MyTime.Text = time.ToString("c");
}
}
}
Xaml Code
<Grid>
<TextBlock Name="MyTime" />
</Grid>
You initialise the DispatchTimer with an interval of 1 second: Timer.Interval = new TimeSpan(0,0,1);
And every TimerTick you decrement your timefield.
So, timeshould start of with the total number of seconds you want to count down. If you start with 15, your countdown timer will count down from 15 seconds to zero.
If you want it to count down for 15minutes, you have to initialise time to 900 (15 x 60'').
I am writing a code in WPF & C# to display next sampling time in Date and time format.
For example, if sampling time is one minute and current time is 08:00 - the next sampling time should show 08:01, Next sampling time is displayed once 08:01 has passed.
I have tried using dispatcherTimer and sleep thread.
But when I use the whole WPF form freezes until next update.
Could you please help me?
code ->
public float samplingTime = 1;
public MainWindow()
{
InitializeComponent();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += timer_Tick;
timer.Start();
tboxSampling.Text = DateTime.Now.AddSeconds(samplingTime).ToString();
void timer_Tick(object sender, EventArgs e)
{
System.Threading.Thread.Sleep((int)samplingTime*1000);
tboxSampling.Text = DateTime.Now.AddSeconds(samplingTime).ToString();
}
}
I'd use the DispatcherTimer for such a problem:
public float samplingTime = 1;
public MainWindow()
{
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0,0,1);
dispatcherTimer.Start();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
tboxSampling.Text = DateTime.Now.AddSeconds(samplingTime).ToString();
}
Timers are not guaranteed to execute exactly when the time interval occurs, but they are guaranteed to not execute before the time interval occurs.
I have a site that is extremely basic and will only ever consist of a single integer.
However the integer will actively change, I want to add onto my existing application to display what this integer is in real time.
-I've tried using a Timer and WebClient however if I put the code under InitializeComponent() the form will never load.
-Also if I put the code in Form1_Load the form will never load.
-I was successful in getting the number to display in real time by putting the code under a button_click event, but I want this code to begin as soon as the form load.
-Also when the button was first clicked the first timer sequence the label would display lat (unsure what this means)
-After the button was pressed and the timer loop began the app breaks, the number will update properly, but you cannot use any other functionality of the app, you can not move the window, you cannot close the app, etc..
private void timer_Tick(object sender, EventArgs e)
{
Timer timer = (Timer)sender;
this.Visible = false;
timer.Stop();
this.Visible = true;
}
private void button1_Click(object sender, EventArgs e)
{
int c = 5;
while (c == 5)
{
using (var client = new WebClient())
{
var s = client.DownloadString(#"myURL.html");
var htmldoc2 = (IHTMLDocument2)new HTMLDocument();
htmldoc2.write(s);
var plainText = htmldoc2.body.outerText;
label1.Text = plainText;
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
timer.Interval = 5000;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
}
}
Please help me no clue what I am doing wrong here
I managed to fix my issue by using the following code if anyone ever has a similar question:
private void test()
{
using (var client = new WebClient())
{
var s = client.DownloadString(#"myURL.html");
var htmldoc2 = (IHTMLDocument2)new HTMLDocument();
htmldoc2.write(s);
var plainText = htmldoc2.body.outerText;
label1.Text = plainText;
}
}
int i = 1;
private void timer1_Tick(object sender, EventArgs e)
{
i += 1;
if (i >= 199)
{
i = 1;
timer1.Stop();
timer1.Start();
}
test();
}
timer1 was added to the winform from the toolbox, and is set to enabled with an interval of 200
I am coding in C#, and I have 3 variables that are being updated via my timer every 1000ms.
I want to use this timer to have a FastLine chart that plots the new points every 1000ms obviously.
I have got this working to a certain extent.
It is plotting each tick the timer does, but it just keeps adding to it i only want it to show the previous 20 plots, not the past 2000 if the program has been running that long.
My code below for the chart within my timer1_Tick method:
try
{
chart1.Series[0].Points.AddXY(xaxis++, CPUTemperatureSensor.Value);
chart1.Series[1].Points.AddXY(xaxis, NvdGPUTemperatureSensor.Value);
chart1.Series[2].Points.AddXY(xaxis, ramusedpt);
}
catch
{
}
xaxis is declared previously as an int, no need to show all code as rest is irrelevant
Below code is what was used to solve the problem, posted by the user jstreet
Link to the hread and his comment:
How to move x-axis grids on chart whenever a data is added on the chart
public partial class Form1 : Form
{
Timer timer;
Random random;
int xaxis;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
random = new Random();
timer = new Timer();
timer.Interval = 1000;
timer.Tick += timer_Tick;
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
chart1.Series[0].Points.AddXY(xaxis++, random.Next(1, 7));
if (chart1.Series[0].Points.Count > 10)
{
chart1.Series[0].Points.Remove(chart1.Series[0].Points[0]);
chart1.ChartAreas[0].AxisX.Minimum = chart1.Series[0].Points[0].XValue;
chart1.ChartAreas[0].AxisX.Maximum = xaxis;
}
}
I am developing a C# desktop application. I want my all open windows to pop up (something which happens with Alt + Tab) every 5th minute. I looked at a few questions here. They suggest doing it by using timers, but how do I pop up the minimised windows?
Here is a really basic example for you to work on.
First create the timer.
Create a function that will run when the timer ticks.
Then add an event to run every time it ticks. And link it your function
Inside that function check if it has been 5 minutes. If so, maximize
the window
public partial class TimerForm : Form
{
Timer timer = new Timer();
Label label = new Label();
public TimerForm ()
{
InitializeComponent();
timer.Tick += new EventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
timer.Interval = (1000) * (1); // Timer will tick evert second
timer.Enabled = true; // Enable the timer
timer.Start(); // Start the timer
}
void timer_Tick(object sender, EventArgs e)
{
// HERE you check if five minutes have passed or whatever you like!
// Then you do this on your window.
this.WindowState = FormWindowState.Maximized;
}
}
Here is the complete solution
public partial class Form1 : Form
{
int formCount = 0;
int X = 10;
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
public Form1()
{
InitializeComponent();
timer.Tick += new EventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
timer.Interval = (1000) * X; // Timer will tick evert second
timer.Enabled = true; // Enable the timer
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
FormCollection fc = new FormCollection();
fc = Application.OpenForms;
foreach (Form Z in fc)
{
X = X + 5;
formCount++;
if (formCount == fc.Count)
X = 5;
Z.TopMost = true;
Z.WindowState = FormWindowState.Normal;
System.Threading.Thread.Sleep(5000);
}
}
}