Hello everyone ,
Have a small problem which i dont know how to deal with . at the same time not sure if i need delegate or event handlers . here it goes .

I have two classes with two forms : Form A and Progress Form .
Form A contains a button where when the user clicks on it , a sql connection is connected and a query is executed ( which contains lets say a million data and take quite some time to execute it ) .

private void button1_Click(object sender, EventArgs e)
        {    
            SqlConnection conn = new SqlConnection("Server=.//SQLEXPRESS ;Initial Catalog=GRCEXPLORER;Integrated Security = true");
            try
            {
                // connect to the server
                string CrLF = Environment.NewLine;

                ProgressForm progressForm = new ProgressForm(conn);
                progressForm.Show();
                progressForm.Refresh();
                
               
                conn.Open();
                progressForm.Visible = true;
                progressForm.abbrechen_Click( sender, e);
               
                /*SqlCommand cmd = new SqlCommand("UPLOAD" + comboBox2.Text, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "UPLOAD" + comboBox2.Text;
                cmd.Parameters.Add(new SqlParameter ("@auditconnector", SqlDbType.NVarChar, 50));
                cmd.Parameters["@auditconnector"].Value = comboBox10.Text;
                comboBox10.Items.AddRange(new[] { "20100808" }); comboBox10.SelectedItem = ("20100802");

                cmd.CommandTimeout = 0;
                SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) Console.WriteLine(dr[""]); dr.Close(); conn.Close(); Console.ReadLine();
                */

                progressForm.Close();
                conn.Close();

            }
            catch(Exception Ex)
            {
                
                if (conn != null)
                    conn.Dispose();
 
                string ErrorMessage = "A error occurred while trying to connect to the server.";
          
                ErrorMessage += Ex.Message;
 
             
                MessageBox.Show(this, ErrorMessage, "Connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
 
              
                return;
            }
            
        }

For this i have created another class ProgressFor which contains a Cancel button. i.e if the user clicks on the Cancel button the connection to the sql server is closed

public  void abbrechen_Click(object sender, EventArgs e)
        {
           // testdelegate del = new testdelegate(this.abbrechen_Click) ;
            
            if (sqlConn.State  != 0 )
                sqlConn.Close();

        }

My Problem : Progress Form opens but the control is sent back to Form A( where the query is executing ) becaue of which cannot click on the Form B Cancel button.

would appreciate any help

I would recommend you look into the BackGroundWorker class. Its a useful wrapper for thread classes that provides Reporting, Cancelling and Result methods.
You attach a method to the class which is then run in a separate thread. Within that method you can call ReportProgress method which can be used to update your main UI thread and when the method ends it can return a result. This kind of lengthy operation is best handled in a separate thread to avoid locking up the rest of your program.

Yes you will need a delegate and an event. I wouldn't recommend passing the conn object. Instead do it this way.

put these two lines in your ProgressForm (I put mine at the top because its easy to find that way

public delegate void delNotifyOfCancel();
public event delNotifyOfCancel NotifyOfCancel;

then put this line in the Click event of your cancel button

public  void abbrechen_Click(object sender, EventArgs e)        
{           
      NotifyOfCancel();     
}

in FormA, where you create and show the progress form, tie this event to a local method and use Show()

ProgressForm progressForm = new ProgressForm();
progressForm.NotifyOfCancel += new progressForm.delNotifyOfCancel(frm_NotifyOfCancel);
progressForm.Show();

finally, somewhere in the form that creates and launches progressForm you want to add this method.

private void frm_NotifyOfCancel()
{
               if (sqlConn.State  != 0 )
                sqlConn.Close();    

// anything else you need to do to cancel
}

keep in mind that using Show() allows the user to alternate between the forms and you will need to be responsible for making sure progressForm is closed. So if you want, you could use the same delegate and event concept to actually start the SQL process and use ShowDialog() instead

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.