i would like to block a function and wait for an event then continue , in my case the pseudo-code that i want to have when i click a button:
- pop up messagebox
- wait for the event click in datagridview
- pop up messagebox2
- wait for the event click in datagridview
- pop up a yes/no messagebox
- execute another function
but the actual code do not wait for autoevent.Set() function so the functions called by the threads remains blocked while i want to block the main function.
i tried ManualResetEvent and AutoResetEvent, this is the code that i used for AutoResetEvent :
public partial class person : Form
{
AutoResetEvent auto = new AutoResetEvent(false);
private void button1_Click(object sender, EventArgs e)
{
int old_id, new_id;
//dataGridView1.ClearSelection();
Thread t1 = new Thread(new ThreadStart(th_remove));
Thread t2 = new Thread(new ThreadStart(th_replace));
t1.Start();
old_id = (int)dataGridView1.SelectedRows[0].Cells[1].Value;
t2.Start();
new_id = (int)dataGridView1.SelectedRows[0].Cells[1].Value;
DialogResult dialogResult = MessageBox.Show("Remplacer", "Vous êtes sûr de vouloir remplacer ?", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
db.replace("person", new_id, old_id);
}
}
private void th_replace()
{
auto.WaitOne();
MessageBox.Show("Seléctionnez la ligne remplaçante");
}
private void th_remove()
{
auto.WaitOne();
MessageBox.Show("Seléctionnez la ligne à supprimer");
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
auto.Set();
}
}
Thanks in advance
You could try to add a synchronization object and use Monitor’s methods
WaitandPulseon your sync object. That is you need to callPulsein your event handler andWaitin your synchronous method. But make sure your event does not occur before you enter theWaitstate… =)Good luck!