单击取消按钮后,如何清除数据网格视图。它将从启动开始该过程,但在启动新线程之前,我需要每次清除数据网格视图。
th.Suspend(),th.Resume(),th.Abort() // this is obsolete.
这已被弃用。我收到了这些警告。但我使用的代码对我来说仍然可以正常工作。我以前从未在线程上工作过。
当我使用Reset(),set()
方法时它不起作用。可能我不知道以正确的方式使用它们。有什么建议可以正确使用吗?
ManualResetEvent run=new ManualResetEvent(true);
ManualResetEvent shut=new ManualResetEvent(false);
public delegate void GridTestResults(int Sno,string Name,double MinValue,double MaxValue,double Value,string Result,string Time);
public void ValuesToGrid(int Sno,string Name,double MinValue,double MaxValue,double Value,string Result,string Time)
{
DataRow row=table.NewRow();
if(this.InvokeRequired)
{
GridTestResults grid = new GridTestResults(this.ValuesToGrid);
this.Invoke(grid,new object[]{Sno,Name,MinValue,MaxValue,Value,Result,Time});
}
else
{
row["Sno"] = Sno;
row["Name"] = Name;
row["MinValue"] = MinValue;
row["MaxValue"] = MaxValue;
row["Value"] = Value;
row["Result"] = Result;
row["Time"] = Time;
table.Rows.Add(row);
dataGridView1.Refresh();
//How can i clear all the rows of gridview once the
// its clicks the start button after cancel button
}
}
private void Startbutton_Click(object sender, EventArgs e)
{
stopwatch.Start();
timer1.Start();
ts=new ThreadStart(ProcessStarted);
th=new Thread(ts);
th.Start();
}
private void Pausebutton_Click(object sender, EventArgs e)
{
if (th.ThreadState == System.Threading.ThreadState.Suspended)
{
timer1.Enabled = true;
label5.Enabled = true;
Pausebutton.Enabled = true;
th.Resume(); //run.Set(); // this is obsolete.This has been deprecated.
label3.Text = "The Current thread has resumed again";
}
else
{
label5.Enabled = false;
timer1.Enabled = false;
th.Suspend();
//th.Suspend(); // this is obsolete.This has been deprecated.
label3.Text = "The current thread suspended";
}
}
private void Cancelbutton_Click(object sender, EventArgs e)
{
timer1.Stop();
stopwatch.Stop();
if (th.ThreadState==System.Threading.ThreadState.Suspended)
{
try
{
th.Abort();
}
catch (Exception)
{
th.Resume();
}
}
try
{
th.Abort();
//th.Join();
}
catch (Exception)
{
th.Resume();
}
label3.Text = "The Testprocess stopped completely";
//dataGridView1.Rows.Clear();
//dataGridView1.DataSource = null;
//i cannot use any of these two way to clear the gridview....
//gives error like cannot clear the list
stopwatch.Reset();
}
private void ProcessStarted()
{
//all the methods to be shown in datagridview
void oneMethod()
{
Application.Run(new OneForm());
//here the form appears as popup window.once
// we click ok button it will closes automatically.
//now the problem is when ever i clcik pause button the OneForm will
// hangs and when i clcik resume it will work normally.
// am not sure is it the correct way of doing r not while
// working with the threads?
}
//dataGridView1.Rows.Clear();
//Here also it does not work..gives same exception like
//cannot clear the list.
}