Usuário com melhor resposta
abrir form dentro de uma thread

Pergunta
-
Bom dia a todos,
Estou tendo um problema ao tentar abrir um form, chamando ele dentro de uma função que roda dentro de uma thread.
Eu só consigo abri-lo usando o ShowDialog(), porém com este método eu só consigo continuar o programa fechando o form e não é isso que eu quero, pois preciso que ele abra, fique aberto e o programa continue rodando, porem usando apenas o método Show(), o form não abre, ou melhor ele tenta abrir, mas é como se não terminasse o processo. Tentei também o método Invoke(), mas também não obtive sucesso.
Segue o codigo abaixo, esta função roda dentro de uma thread:
void mensagemGrafica(char Form, Byte msg, int tempo, const char *constmsg)
{
array<Byte>^ dado = gcnew array<Byte>(80);
for(int i = 0; i < strlen(constmsg); i++) dado[i] = (const unsigned char)constmsg[i];
if(Form == 1)
{
Msg1^ MSG = gcnew Msg1();
MSG->imagem = msg;
MSG->tempo = tempo;
MSG->mensagem = System::Text::Encoding::ASCII->GetString(dado);
MSG->Invoke(MSG->myDelegate);
}
}Aqui é o codigo do Form:
#pragma once
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;public ref class Msg1 : public System::Windows::Forms::Form
{
public:
Msg1(void)
{
InitializeComponent();myDelegate = gcnew Chamar(this, &Msg1::Mostra);
#pragma region Windows Form Designer generated code
if(!this->IsHandleCreated)
{
this->CreateHandle();
}
}
protected:
~Msg1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::PictureBox^ pictureBox1;
private: System::Windows::Forms::Label^ label1;
private: System::ComponentModel::IContainer^ components;
public:
static unsigned int tempo;
static Byte imagem;
public:
static String^ mensagem;
public:
public:
public:
delegate void Chamar();
Chamar^ myDelegate;
public:
void Mostra()
{
Msg1^ MSG = gcnew Msg1();
MSG->Show();
}
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
this->label1 = (gcnew System::Windows::Forms::Label());
this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->BeginInit();
this->SuspendLayout();
//
// pictureBox1
//
this->pictureBox1->ImageLocation = L"...\\V_autoimagens\\apresente.png";
this->pictureBox1->Location = System::Drawing::Point(181, 0);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size = System::Drawing::Size(430, 430);
this->pictureBox1->TabIndex = 0;
this->pictureBox1->TabStop = false;
//
// label1
//
this->label1->AutoSize = true;
this->label1->Font = (gcnew System::Drawing::Font(L"Cooper Black", 27.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label1->ForeColor = System::Drawing::Color::Black;
this->label1->Location = System::Drawing::Point(-3, 453);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(130, 42);
this->label1->TabIndex = 1;
this->label1->Text = L"MSG1";
//
// Msg1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->BackColor = System::Drawing::Color::White;
this->ClientSize = System::Drawing::Size(792, 573);
this->Controls->Add(this->label1);
this->Controls->Add(this->pictureBox1);
this->Name = L"Msg1";
this->StartPosition = System::Windows::Forms::FormStart
this->Text = L"Msg1";
this->Load += gcnew System::EventHandler(this, &Msg1::Msg1_Load);
this->Shown += gcnew System::EventHandler(this, &Msg1::Msg1_Show);
this->VisibleChanged += gcnew System::EventHandler(this, &Msg1::Msg1_Load);
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->EndInit();
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
Alguém poderia me ajudar?
Obrigado.
Respostas
-
Bom dia Edubass.
Essa linguagem é C++? Desculpe, conheço mais o C# mas vou tentar responder, espero que seja útil!
Duas sugestões de alteração no seu FOR é alterar o Invoke por BeginInvoke e incluir um DoEvents no final:
for(int i = 0; i < strlen(constmsg); i++) dado[i] = (const unsigned char)constmsg[i]; if(Form == 1) { Msg1^ MSG = gcnew Msg1(); MSG->imagem = msg; MSG->tempo = tempo; MSG->mensagem = System::Text::Encoding::ASCII->GetString(dado); //MSG->Invoke(MSG->myDelegate); MSG->BeginInvoke(MSG->myDelegate); } // Favor verificar se a sintaxe C++ está correta! Conheço apenas C# Application->DoEvents(); }
Veja se funciona.
- Marcado como Resposta Edubass quinta-feira, 10 de julho de 2014 15:26
Todas as Respostas
-
Bom dia Edubass.
Essa linguagem é C++? Desculpe, conheço mais o C# mas vou tentar responder, espero que seja útil!
Duas sugestões de alteração no seu FOR é alterar o Invoke por BeginInvoke e incluir um DoEvents no final:
for(int i = 0; i < strlen(constmsg); i++) dado[i] = (const unsigned char)constmsg[i]; if(Form == 1) { Msg1^ MSG = gcnew Msg1(); MSG->imagem = msg; MSG->tempo = tempo; MSG->mensagem = System::Text::Encoding::ASCII->GetString(dado); //MSG->Invoke(MSG->myDelegate); MSG->BeginInvoke(MSG->myDelegate); } // Favor verificar se a sintaxe C++ está correta! Conheço apenas C# Application->DoEvents(); }
Veja se funciona.
- Marcado como Resposta Edubass quinta-feira, 10 de julho de 2014 15:26
-
-
Ola dimascsoares, tudo bom?
A sintaxe na ultima linha na verdade é :Application::DoEvents();
Ainda assim o form não abriu, ou melhor ele abriu, mas sem carregar a imagem no pictureBox1->ImageLocation = L"...\\V_autoimagens\\apresente.png"; e a mensagem no label1->Text = L"MSG1";
É meu primeiro contato com o c++/cli e estou tendo dificuldades nessa questão.