threads + delegate em c#.net

PHP, Java, JavaScript, XML, XHTML, HTML, CSS, ASP, Delphi, Assembly, LaTeX, C, UML, Flash, Perl, SQL, Python, Zope, Pascal, WML. Se conhece mais de 3 siglas referidas, este é o forum para si.

Moderadores: Administradores, Moderadores

threads + delegate em c#.net

Mensagempor arrakisdunes » Terça Mai 01, 2007 2:02

boas,
eu ando a tentar meter-me no mundo das threads em c#.net (para depois implementar alg coisa com socks) e deparei-me com um problema. tentei seguir varias alternativas inclusive o uso d delegates (na minha opiniao um bocado difíceis) e n consigo obter um resultado positivo. Então o problema e este, tenho uma tabela(em ambiente gráfico claro) e quero continuamente meter la um simples "ola" sem ke o ambiente gráfico crash. So começa a meter "ola" kd pressiono o botão, só que ele da-me o seguinte erro:

"An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll

Additional information: Cross-thread operation not valid: Control 'dataGridView1' accessed from a thread other than the thread it was created on."

ja tentei por o codigo de varias posicoes e nao caiu nd, ja ando a mto tempo a tentar. alguem que me possa ajudar??
o codigo em questao e este:

Código: Seleccionar todos
private void begin_simulacao(object sender, EventArgs e)
        {
            Thread oThread = new Thread(socks_t);
            oThread.Start();

            simulacao.Enabled = false;
        }




delegate void tabela(string s, out int iExecThread);




public void faz_tabela(string s, out int iExecThread)
        { tabela fazer = new tabela(faz_tabela);
          iExecThread = AppDomain.GetCurrentThreadId();
         
     if (dataGridView1.InvokeRequired == true)
             dataGridView1.Rows.Add(s);
         else
         {
            IAsyncResult ar = fazer.BeginInvoke("ola", out iExecThread, null, null);
            ar.AsyncWaitHandle.WaitOne();
            fazer.EndInvoke(out iExecThread, ar);
         }
        }

     
       
  public void socks_t()
        {
           
    int iExecThread;
    tabela fazer = new tabela(faz_tabela);
   

                while(true){

                    IAsyncResult ar = fazer.BeginInvoke("ola", out iExecThread, null,null);
                    ar.AsyncWaitHandle.WaitOne();

                fazer.EndInvoke(out iExecThread, ar);
                }

         
               
        }
    }
}


Obrigado
arrakisdunes
Novato
Novato
 
Mensagens: 3
Registado: Terça Mai 01, 2007 1:59

Voltar para Programação

Quem está ligado:

Utilizador a ver este Fórum: Nenhum utilizador registado e 1 visitante

cron