Close a form from another thread?

Error: Cross-Thread exeption.

I found many exaples how to modify a control from another Thread but i cant close the Form (a simple StatusForm) from the The MainForm. What im trying to do is to close the form inside a void "AsyncCallback" wich is called by a Thread.

I would appreciate your Help

 

Reggards.

Jose L. Yanez R.




Answer this question

Close a form from another thread?

  • PhilipHaugaard

    Ok... I dont understand what you are telling me, maybe you dont understand me either.

    The code speak for itself. ^_^ better then I

    Form2 FormStatus = new Form2 ();

    private void DoSomething()

    {

    FormStatus.Show("Please Wait a LOT!!");

    ThreadStart ThrStart = new ThreadStart(Cuadra);

    Thread DoThread = new Thread(ThrStart);

    DoThread.Name = "Thread1";

    DoThread.Start();

    }

    private void Cuadra()

    {

    SqlConnection Conn = new SqlConnection(Form2.RutaConn + ";Asynchronous Processing=true");

    SqlCommand command = new SqlCommand("cuadra_accthist", Conn);

    command.CommandType = CommandType.StoredProcedure;

    SqlParameter Periodo = command.Parameters.Add("@Periodo", SqlDbType.VarChar, 6);

    command.CommandTimeout = 60;

    Periodo.Value = textBox1.Text.Trim();

    try

    {

    Conn.Open();

    AsyncCallback callback = new AsyncCallback(HandleCallback);

    command.BeginExecuteNonQuery(callback, command);

    isExecuting = true;

    }

    catch (SqlException SqlEx)

    {

    MessageBox.Show("Ha Ocurrido el siguiente Error :" + Environment.NewLine + SqlEx.Message, "Error al ejecutar el SP", MessageBoxButtons.OK, MessageBoxIcon.Error);

    }

    }

    private void HandleCallback(IAsyncResult result)

    {

    try

    {

    SqlCommand command = (SqlCommand)result.AsyncState;

    int rowCount = command.EndExecuteNonQuery(result);

    string rowText = " Filas Afectadas.";

    if (rowCount == 1)

    {

    rowText = " Fila Afectada.";

    }

    rowText = rowCount + rowText;

    isExecuting = false;

    //Here i want close the FormStatus (Form2) and i get the Exception Cross-Thread.

    MessageBox.Show("Proceso Terminado Correctamente" + Environment.NewLine + rowText, "Terminado", MessageBoxButtons.OK, MessageBoxIcon.Information);

    }

    catch (Exception Ex)

    {

    MessageBox.Show("Proceso Erroneo" + Ex.Message, "Fallo", MessageBoxButtons.OK, MessageBoxIcon.Information);

    }

    }



  • Reva

    public delegate void CloseDelagate();

    Add this line before Form2 class. To close form, you need form reference. If it is called "FormStatus", instead of:

    FormStatus.Close();

    write:

    FormStatus.Invoke(new CloseDelegate(Form2.Close));


  • Damir Dobric

    delegate void CloseDelagate();

    Having Form reference in another thread, you can close it by the following way:

    form.Invoke(new CloseDelegate(form.Close));


  • mveeravagu

    I really need help! so please Help.

  • Jesper L. Nielsen

    Alex Farber & ahmedilyas thank you! i start to read again and now i understand very well.



  • Kiwi_Ant

    take a look, particular to my answer, on how to Invoke the UI from another thread:

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=564275&SiteID=1



  • Close a form from another thread?