I am having 1 long running process which I am running on seperate thread and when this long running process is going on I want to show time like stop watch on my form control just to show that process is going on and user doesn't think that program is stuck or block.
So on my form control I want to show user a timer/stop watch
like below which will start when my long running method will be called
and I want to show timer on form in below format which will keep on running as soon as method is start or stopped
.
Hours : Minutes : Seconds
Code:
private void btnBrowse_Click(object sender, EventArgs e)
{
this.Invoke(new delegateFileProgress(Progressbarcount), 0);
OpenFileDialog openFileDialog = new OpenFileDialog();
DialogResult dialog = openFileDialog.ShowDialog();
if (dialog == DialogResult.OK)
{
Thread longRunningProcess = new Thread(() => LongRunningMethod(openFileDialog.FileName));
}
}
private void LongRunningMethod(string path)
{
stopwatch.Start();
TimeSpan ts = stopwatch.Elapsed;
string name = string.Format("{0}:{1}", Math.Floor(ts.TotalMinutes), ts.ToString("ss\\.ff"));
if (lblTimer.InvokeRequired)
{
lblTimer.BeginInvoke(new MethodInvoker(delegate { name = string.Format("{0}:{1}", Math.Floor(ts.TotalMinutes), ts.ToString("ss\\.ff")); }));
}
lblTimer.Text = name; Error:Cross-thread operation not valid: Control 'lblTimer' accessed from a thread other than the thread it was created on.
/*
* Long running codes which takes 30 - 40 minutes
*/
stopwatch.Stop();
}
But getting error on below line:
Cross-thread operation not valid: Control 'lblTimer' accessed from a thread other than the thread it was created on.
lblTimer.Text = name;
I am pretty new in winform.