-
May 9th, 2011, 03:29 AM
#1
visual studio c++ windows form application
Hello.
I have a problem with windows form application. I try to write a code where i read continuous values from a serial, and that values i put in a text box. The program should do that until I press another button. I thought I should use theards... when I run this code, the program read my values corect but I can't press another button because all are blocked. Anybody has any ideea how shoud I do this program work?or if I should use something else,not threads? I put the code that I wrote below.
Thanks a lot.
public ref class Form1 : public System::Windows::Forms::Form
{
public:
delegate void TextboxCallback(System::Object ^obj);
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
newThread1 = gcnew Thread(gcnew ParameterizedThreadStart(&ThreadProc1 ));
th1 = gcnew Thread(gcnew ThreadStart(this, &Form1::th1Method));
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private: System::Windows::Forms::TextBox^ textBox1;
private: System::IO::Ports::SerialPort^ serialPort1;
private: System:iagnostics::Process^ process1;
private: System::ComponentModel::IContainer^ components;
Thread ^th1;
private: System::Windows::Forms::Button^ button2;
Thread ^newThread1;
//private: System::Threading::Thread^ serialread;
private:
/// <summary>
/// Required designer variable.
/// </summary>
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
this->button1 = (gcnew System::Windows::Forms::Button());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components));
this->button2 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System:rawing::Point(197, 12);
this->button1->Name = L"button1";
this->button1->Size = System:rawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// textBox1
//
this->textBox1->Location = System:rawing::Point(68, 14);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System:rawing::Size(100, 20);
this->textBox1->TabIndex = 1;
//
// button2
//
this->button2->Location = System:rawing::Point(116, 109);
this->button2->Name = L"button2";
this->button2->Size = System:rawing::Size(75, 23);
this->button2->TabIndex = 2;
this->button2->Text = L"button2";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// Form1
//
this->AutoScaleDimensions = System:rawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System:rawing::Size(735, 293);
this->Controls->Add(this->button2);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->FormClosing += gcnew System::Windows::Forms::FormClosingEventHandler(this, &Form1::Form1_FormClosing);
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void th1Method()
{
Byte data;
}
static void SafeThread1(System::Object ^obj)
{
Form1 ^ob = (Form1^) obj;
if(ob->textBox1->InvokeRequired)
{
TextboxCallback ^d = gcnew TextboxCallback(SafeThread1);
ob->Invoke(d,gcnew array<System::Object^>{ob});
}
else
{
do
{
ob->textBox1->Text=ob->serialPort1->ReadLine();
Thread::Sleep( 80 );
}while(ob->serialPort1->IsOpen);
}
}
static void ThreadProc1(System::Object ^obj)
{
SafeThread1(obj);
}
//
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
this->serialPort1->BaudRate = 9600;
this->serialPort1->DataBits = 8;
this->serialPort1->PortName = "COM1";
this->serialPort1->StopBits = System::IO::Ports::StopBits::One;
//this->serialPort1->ReadTimeout = 10;
this->serialPort1->Open();
// System::Threading::ThreadStart^ myThreadDelegate = gcnew System::Threading::ThreadStart(this, &Licenta02::Form1::readSerial);
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
String^ a;
double b=3.4;
// if(this->serialread->IsAlive)
// this->serialread->Abort();
// else
// this->serialread->Start();
// this->demoThread = gcnew Thread(gcnew ThreadStart(this,&Form1::ThreadProcSafe));
// this->textBox1->Text=this->serialPort1->ReadLine();
// this->demoThread->Start();
// this->th1->Start();
// this->newThread1->IsBackground=true;
// newThread1->Start(this);
a=""+b;
this->textBox1->Text=a;
}
private: System::Void Form1_FormClosing(System::Object^ sender, System::Windows::Forms::FormClosingEventArgs^ e) {
this->serialPort1->Close();
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
this->serialPort1->Close();
}
};
}
-
May 9th, 2011, 03:35 AM
#2
Re: visual studio c++ windows form application
The Windows Form problems are discussed in the Managed C++/CLI forum.
This one is for native VIsual C++
Besides, you should use Code tags while posting code snippets. See Announcement: Before you post....
Victor Nijegorodov
-
May 11th, 2011, 06:06 AM
#3
Re: visual studio c++ windows form application
Dear meenaa,
please stop spamming!
Otherwise you'll be banned!
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|