private async void Button1_Click(object sender, EventArgs e) { //Cria uma thread new Task(() => { //Sleep de 1s System.Threading.Thread.Sleep(1000); //Manda executar um metodo na thread em que a frm2 está rodando (principal) frm2.Invoke((MethodInvoker) delegate { frm2.Text = "Teste"; }); }).Start(); //Inicia a execução da thread criada la em cima }
public Class1(Form2 f) { a = f; SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(); builder.DataSource = "ANTONINA"; builder.InitialCatalog = "testowy"; builder.IntegratedSecurity = true; con = new SqlConnection(builder.ConnectionString); DataSet ds = new DataSet(); con.InfoMessage += new SqlInfoMessageEventHandler(Connection_InfoMessage); Task task = Task.Factory.StartNew(() => { using (SqlCommand cmd = new SqlCommand()) { cmd.Connection = con; cmd.CommandText = "dbo.test"; cmd.CommandTimeout = 120; cmd.Connection.Open(); using (SqlDataAdapter sa = new SqlDataAdapter(cmd)) { Task.Delay(300).Wait(); sa.Fill(ds); cmd.Connection.Close(); a.Invoke((MethodInvoker) delegate() { a.Close(); }); } } } ); }