C# 如果fun1做了fun2,那么就不要做fun2
我正在从事C#项目,所以我有两个函数,我只需要如果函数1已经完成,那么我希望函数2执行,否则函数2必须被阻止执行(没有执行!!) 我的函数1称为C# 如果fun1做了fun2,那么就不要做fun2,c#,function,C#,Function,我正在从事C#项目,所以我有两个函数,我只需要如果函数1已经完成,那么我希望函数2执行,否则函数2必须被阻止执行(没有执行!!) 我的函数1称为saveDuplicateCourse(),函数2称为Save(),彼此的代码如下: public void SaveDuplicatCourse() { if(con.State !=ConnectionState.Open) con.Open(); List<int> IDs = new List<int&g
saveDuplicateCourse()
,函数2称为Save()
,彼此的代码如下:
public void SaveDuplicatCourse()
{
if(con.State !=ConnectionState.Open)
con.Open();
List<int> IDs = new List<int>();
foreach (DataGridViewRow r in dataGridViewStudents.Rows)
{
if (r.Cells[0].Value != null && bool.Parse(r.Cells[0].Value.ToString()))
{
IDs.Add(int.Parse(r.Cells[1].Value.ToString()));
}
}
foreach (int i in IDs)
{
try
{
SqlCommand com = new SqlCommand(@"Insert into DuplicateCourses
values(" + i + "," + CCID + ")", con);
com.ExecuteNonQuery();
}
catch (Exception)
{
MessageBox.Show("They are exist");
}
}
con.Close();
}
public void Save()
{
SqlCommand com = new SqlCommand(@"Delete from students
where
Course_ID = " + ID, con);
con.Open();
com.ExecuteNonQuery();
List<int> IDs = new List<int>();
foreach (DataGridViewRow r in dataGridViewStudents.Rows)
{
if (r.Cells[0].Value!=null && bool.Parse(r.Cells[0].Value.ToString()))
{
IDs.Add(int.Parse(r.Cells[1].Value.ToString()));
}
}
foreach (int i in IDs)
{
com.CommandText = "Insert into students values(" + i + "," + ID + ")";
com.ExecuteNonQuery();
}
con.Close();
}
请告诉我这将如何完成。在您的示例中,我没有看到任何异步发生的情况,因此您的代码应该以您正在寻找的方式执行。现在,如果您询问执行是否成功(与任何不成功的情况相反,即没有删除行),则情况就不同了,并且可以以各种不同的方式进行处理。但在我们走这条路之前,你似乎已经得到了想要的结果。设置断点并逐行查看每行代码的执行顺序。为function1添加一个
布尔值。如果返回false,则不要运行function2。如果返回true,则运行function2
boolean saveDuplicateCourseCompleted = false;
private void buttonSaveChanges_Click(object sender, EventArgs e)
{
if (saveDuplicateCourseCompleted == true)
Save();
else
// or do something else.
}
请注意以下示例:
public boolean SaveDuplicatCourse()
{
if(con.State !=ConnectionState.Open)
con.Open();
List<int> IDs = new List<int>();
foreach (DataGridViewRow r in dataGridViewStudents.Rows)
{
if (r.Cells[0].Value != null && bool.Parse(r.Cells[0].Value.ToString()))
{
IDs.Add(int.Parse(r.Cells[1].Value.ToString()));
}
}
foreach (int i in IDs)
{
try
{
SqlCommand com = new SqlCommand(@"Insert into DuplicateCourses
values(" + i + "," + CCID + ")", con);
com.ExecuteNonQuery();
}
catch (Exception)
{
MessageBox.Show("They are exist");
saveDuplicateCourseCompleted=false// Now this is right way
}
}
con.Close();
}
public boolean savecourse()
{
if(con.State!=ConnectionState.Open)
con.Open();
列表ID=新列表();
foreach(dataGridViewStudents.Rows中的DataGridViewRow r)
{
if(r.Cells[0].Value!=null&&bool.Parse(r.Cells[0].Value.ToString())
{
Add(int.Parse(r.Cells[1].Value.ToString());
}
}
foreach(id中的int i)
{
尝试
{
SqlCommand com=新的SqlCommand(@“插入到副本中”
数值(“+i+”、“+CCID+”),con);
com.ExecuteNonQuery();
}
捕获(例外)
{
MessageBox.Show(“它们存在”);
saveDuplicateCourseCompleted=false//现在这是正确的方法
}
}
con.Close();
}
我不知道你在说什么<代码>未执行的
??什么…?使这些函数返回布尔而不是void。在第一个函数上返回bool
<代码>如果(Func1())Func2()代码>我只需要保存()
仅在saveDuplicateCourse()
完成后执行,这就是您现在正在做的事情-它们不是并行执行的。
public boolean SaveDuplicatCourse()
{
if(con.State !=ConnectionState.Open)
con.Open();
List<int> IDs = new List<int>();
foreach (DataGridViewRow r in dataGridViewStudents.Rows)
{
if (r.Cells[0].Value != null && bool.Parse(r.Cells[0].Value.ToString()))
{
IDs.Add(int.Parse(r.Cells[1].Value.ToString()));
}
}
foreach (int i in IDs)
{
try
{
SqlCommand com = new SqlCommand(@"Insert into DuplicateCourses
values(" + i + "," + CCID + ")", con);
com.ExecuteNonQuery();
}
catch (Exception)
{
MessageBox.Show("They are exist");
saveDuplicateCourseCompleted=false// Now this is right way
}
}
con.Close();
}