C ++构建器CreateThread()中的错误

时间:2010-09-30 20:21:34

标签: c++ multithreading c++builder createthread

嘿,我正在尝试用C ++编写一个生成三角形,正方形和正弦波的程序。我输入频率,幅度等,它计算波的平均值。我可以通过选择一个单选按钮选择波形。

这是一个实时系统,因此,如果正在绘制波浪,并且如果我选择对应于另一种波浪的无线电按钮,它将实时变化。

错误是这样的:

[C ++错误] FormularioPrincipal.cpp(171):E2297'this'只能在成员函数中使用

它发生在我正在创建线程的行上。有人能帮助我吗?

谢谢!

//---------------------------------------------------------------------------

#include <vcl.h>
#include <io.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <limits>
#include <complex>
#include <math.h>
#pragma hdrstop

#include "FormularioPrincipal.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
HANDLE thread1;
HANDLE thread2;
HANDLE thread3;
HANDLE thread4;
HANDLE thread5;
HANDLE Mutex;

int pipe[2];
double freq;
double per;
double freqAngular;
double pi;
double taxaAmostragem;
double tempofinal;
double deslocamento;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
DWORD WINAPI geraSinalSenoide(void *parametro){

    TForm1 *FormularioPrincipal = (TForm1*) parametro;
    int i;
    double valorGerado;
    per = StrToFloat(FormularioPrincipal->Edit2->Text);
    freq = 1/per;
    FormularioPrincipal->Edit3->Text = freq;
    pi = 3.141592654;
    taxaAmostragem = 20;
    tempofinal = 1000;
    freqAngular = (2 * pi)/per;
    double amp = StrToFloat(FormularioPrincipal->Edit1->Text);

            while(true){
                    for (double time=0; time<=freq; time = time + (1 / taxaAmostragem)){

                            WaitForSingleObject(Mutex, INFINITE);
                            ReleaseMutex(Mutex);
                        deslocamento = ( sin(freqAngular * time)  * amp);
                            write(pipe[1], &deslocamento, sizeof(int));
                        FormularioPrincipal->Series1->AddXY(per, deslocamento, "", clRed);
                            per = per + StrToFloat(FormularioPrincipal->Edit2->Text);
                            //FormularioPrincipal->Label7->Caption = time;

                    }
                     Sleep(1000);
                     return 0;
            }


};

DWORD WINAPI geraSinalQuadrado(void *parametro){

    TForm1 *FormularioPrincipal = (TForm1*) parametro;
    int i;
    int x=0;
    float valorGerado;
    double per = StrToFloat(FormularioPrincipal->Edit6->Text);
    freq = 1/per;
    FormularioPrincipal->Edit2->Text = freq;
    while(true){
            for(i=0; i<freq;i++){
            WaitForSingleObject(Mutex, INFINITE);
            ReleaseMutex(Mutex);

                        valorGerado = rand() % (FormularioPrincipal->Edit1->Text);
                        write(pipe[1], &valorGerado, sizeof(int));
                        FormularioPrincipal->Series1->AddXY(per, valorGerado, "", clRed);
                        per = per + StrToFloat(FormularioPrincipal->Edit6->Text);
                        FormularioPrincipal->Series1->AddXY(per, valorGerado, "", clRed);
            }    Sleep(1000);
    }
}

DWORD WINAPI geraSinalTriangular(void *parametro){

     TForm1 *FormularioPrincipal = (TForm1*) parametro;
    int i;
    int x=0;
    float valorGerado;
    double per = StrToFloat(FormularioPrincipal->Edit6->Text);
    freq = 1/per;
    FormularioPrincipal->Edit2->Text = freq;
    while(true){
            for(i=0; i<freq;i++){
            WaitForSingleObject(Mutex, INFINITE);
            ReleaseMutex(Mutex);

                        valorGerado = rand() % (FormularioPrincipal->Edit1->Text);
                        write(pipe[1], &valorGerado, sizeof(int));
                        FormularioPrincipal->Series1->AddXY(per, valorGerado, "", clRed);
                        per = per + StrToFloat(FormularioPrincipal->Edit6->Text);

            }    Sleep(1000);
    }

};

DWORD WINAPI processaNumeros(void *parametros){

   TForm1* FormularioPrincipal = (TForm1*) parametros;
             int dados[10];
             int qtdDadosLidosBuffer=0;
             float media = 0;
             float soma =0;
             float dif= 0;
             while(true){
                       soma  = 0;
                       dif = 0;
                       int i;
                       for(i=0; i<10; i++)//ele vai pegar de 10 em 10 numeros e calcular a media
                       {
                          int qtdBytesLidos;
                          //ler os dados do pipe
                          qtdBytesLidos = read(pipe[0], &dados[i], sizeof(int));
                          if   (qtdBytesLidos == 0)
                             break;
                       }
                       qtdDadosLidosBuffer = i-1;
                         ReleaseMutex(Mutex);
                         for(int i=0; i<qtdDadosLidosBuffer;i++){
                                soma += dados[i];
                                }
                         if(qtdDadosLidosBuffer != 0)
                            soma/=qtdDadosLidosBuffer;//calcula a media


                         for(int i=0; i<qtdDadosLidosBuffer;i++){
                             dif = dados[i] - soma;
                             dif+=dif;
                         }
                         FormularioPrincipal->Edit4->Text = soma;
                         FormularioPrincipal->Edit5->Text = dif;
                         FormularioPrincipal->Edit6->Text = sqrt(dif);
                         Sleep(1000);//tbm espera 1 segundo
             }
};

DWORD WINAPI main(void *parametro){

    Mutex = CreateMutex(NULL, false, NULL);
    TForm1 *FormularioPrincipal = (TForm1*) parametro;
    DWORD prioridade;
    prioridade = THREAD_PRIORITY_NORMAL;

    if((FormularioPrincipal->RadioButton1->Checked == true) && (FormularioPrincipal->RadioButton2->Checked             == false) && (FormularioPrincipal->RadioButton3->Checked == false)){
            DWORD thread1ID;
            thread1 = CreateThread(NULL, 0, geraSinalSenoide, this, CREATE_SUSPENDED, &thread1ID);
            DWORD thread4ID;
            thread4 = CreateThread(NULL, 0, processaNumeros, this, CREATE_SUSPENDED, &thread4ID);

            GetExitCodeThread(thread2, &exitCode);
            TerminateThread(thread2, exitCode);
            GetExitCodeThread(thread3, &exitCode);
            TerminateThread(thread3, exitCode);

            SetThreadPriority(&thread1ID, prioridade);
    }
    else if((FormularioPrincipal->RadioButton1->Checked == false) && (FormularioPrincipal->RadioButton2->Checked == true) && (FormularioPrincipal->RadioButton3->Checked == false)){
            DWORD thread2ID;
            thread2 = CreateThread(NULL, 0, geraSinalTriangular, this, CREATE_SUSPENDED, &thread2ID);
            DWORD thread4ID;
            thread4 = CreateThread(NULL, 0, processaNumeros, this, CREATE_SUSPENDED, &thread4ID);

            GetExitCodeThread(thread1, &exitCode);
            TerminateThread(thread1, exitCode);
            GetExitCodeThread(thread3, &exitCode);
            TerminateThread(thread3, exitCode);

            SetThreadPriority(&thread2ID, prioridade);
    }
    else{
            DWORD thread3ID;
            thread3 = CreateThread(NULL, 0, geraSinalQuadrado, this, CREATE_SUSPENDED, &thread3ID);
            DWORD thread4ID;
            thread4 = CreateThread(NULL, 0, processaNumeros, this, CREATE_SUSPENDED, &thread4ID);

            GetExitCodeThread(thread1, &exitCode);
            TerminateThread(thread1, exitCode);
            GetExitCodeThread(thread2, &exitCode);
            TerminateThread(thread2, exitCode);

            SetThreadPriority(&thread3ID, prioridade);

    }

    if(_pipe(pipe, sizeof(int)*500, O_BINARY) == -1){//cria o pipe
            MessageBox(NULL, "Erro ao criar pipe", "Aviso", 0);
            return;
   }



}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
DWORD prioridade;
prioridade = THREAD_PRIORITY_NORMAL;

DWORD thread5ID;
thread5 = CreateThread(NULL, 0, main, this, CREATE_SUSPENDED, &thread5ID);
SetThreadPriority(&thread5ID, prioridade);
ResumeThread(thread1);
ResumeThread(thread2);
ResumeThread(thread3);
ResumeThread(thread4);
ResumeThread(thread5);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    unsigned long exitCode;
    GetExitCodeThread(thread1, &exitCode);
    TerminateThread(thread1, exitCode);
    GetExitCodeThread(thread2, &exitCode);
    TerminateThread(thread2, exitCode);
    GetExitCodeThread(thread3, &exitCode);
    TerminateThread(thread3, exitCode);
    GetExitCodeThread(thread4, &exitCode);
    TerminateThread(thread4, exitCode);
    GetExitCodeThread(thread5, &exitCode);
    TerminateThread(thread5, exitCode);
}
//---------------------------------------------------------------------------

1 个答案:

答案 0 :(得分:2)

DWORD WINAPI main(void *parametro)传递parametro而不是thisCreateThread,因为(就像错误一样)你处于自由功能而不是成员函数。