Avatar billede stringbuffer Nybegynder
29. januar 2001 - 05:07 Der er 2 kommentarer og
2 løsninger

Skab en thread i ATL (Visual C++)

Jeg har et ATL-komponent, hvor jeg gerne vil køre nogle processer i en thread for sig.

Jeg har ingen erfaring med hvordan man gør sådan noget her, ved at man i Java skal lave en klasse, der skal arve fra Thread og have en public metode, der hedder run, hvori processerne kan afvikles.

Gælder noget lignende for ATL? Er der nogle Thread-klasser, man kan bruge? eller skal man lave sådan noget helt fra bunden?

Jeg søger et eksempel, der virker, om det så gør noget så trivielt som at kalde MessageBeep hver 5 sek eller sådan...

Kravet er bare at det ikke bruger Borland-klasser/API´er, men kun bruger ATL og windows API
Avatar billede wisen Nybegynder
29. januar 2001 - 07:53 #1
Det her var det eneste jeg lige kunne finde, måske du kan bruge det :

Creating Threads
The CreateThread function creates a new thread for a process. The creating thread must specify the starting address of the code that the new thread is to execute. Typically, the starting address is the name of a function defined in the program code. This function takes a single parameter and returns a DWORD value. A process can have multiple threads simultaneously executing the same function.

The following example demonstrates how to create a new thread that executes the locally defined function, ThreadFunc.

DWORD WINAPI ThreadFunc( LPVOID lpParam )
{
    char szMsg[80];

    wsprintf( szMsg, \"ThreadFunc: Parameter = %d\\n\", *lpParam );
    MessageBox( NULL, szMsg, \"Thread created.\", MB_OK );

    return 0;
}

VOID main( VOID )
{
    DWORD dwThreadId, dwThrdParam = 1;
    HANDLE hThread;
    hThread = CreateThread(
        NULL,                        // no security attributes
        0,                          // use default stack size 
        ThreadFunc,                  // thread function
        &dwThrdParam,                // argument to thread function
        0,                          // use default creation flags
        &dwThreadId);                // returns the thread identifier

  // Check the return value for success.

  if (hThread == NULL)
      ErrorExit( \"CreateThread failed.\" );

  CloseHandle( hThread );
}
For simplicity, this example passes a pointer to a DWORD value as an argument to the thread function. This could be a pointer to any type of data or structure, or it could be omitted altogether by passing a NULL pointer and deleting the references to the parameter in ThreadFunc.

It is risky to pass the address of a local variable if the creating thread exits before the new thread, because the pointer becomes invalid. Instead, either pass a pointer to dynamically allocated memory or make the creating thread wait for the new thread to terminate. Data can also be passed from the creating thread to the new thread using global variables. With global variables, it is usually necessary to synchronize access by multiple threads. For more information about synchronization, see Synchronizing Execution of Multiple Threads.

In processes where a thread might create multiple threads to execute the same code, it is inconvenient to use global variables. For example, a process that enables the user to open several files at the same time can create a new thread for each file, with each of the threads executing the same thread function. The creating thread can pass the unique information (such as the file name) required by each instance of the thread function as an argument. You cannot use a single global variable for this purpose, but you could use a dynamically allocated string buffer.

The creating thread can use the arguments to CreateThread to specify the following:

The security attributes for the handle to the new thread. These security attributes include an inheritance flag that determines whether the handle can be inherited by child processes. The security attributes also include a security descriptor, which the system uses to perform access checks on all subsequent uses of the thread\'s handle before access is granted.
The initial stack size of the new thread. The thread\'s stack is allocated automatically in the memory space of the process; the system increases the stack as needed and frees it when the thread terminates.
A creation flag that enables you to create the thread in a suspended state. When suspended, the thread does not run until the ResumeThread function is called.
You can also create a thread by calling the CreateRemoteThread function. This function is used by debugger processes to create a thread that runs in the address space of the process being debugged.

Avatar billede stringbuffer Nybegynder
29. januar 2001 - 16:12 #2
Jep, jeg fik oz idéen og brugte CreateThread efter at have fundet flg. artikel:

http://support.microsoft.com/support/kb/articles/Q102/3/52.asp

Jeg lavede et medlemsvariabel threadID og en static metode

static run(CMitObjektsKlasse * mk);

og brugte så CreateThread på flg. måde i konstruktøren:

CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)(run), this, 0, &threadID);

i run bliver mk givet som this-pointer, så jeg kan kalde metoder på mit nuværende objekt.

Wisen <- Jeg fandt godt nok ud af det selv, men fordi du svarede så hurtigt og fordi dit svar nok ville lede mig frem til en løsning får du et par point oz.
Avatar billede soepro Nybegynder
30. januar 2001 - 10:42 #3
Det var vel sådan set det jeg beskrev.
Avatar billede wisen Nybegynder
02. februar 2001 - 12:20 #4
Tak for det...
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester