Threading problem

This is a discussion on Threading problem within the Windows Programming forums, part of the Platform Specific Boards category; hi im trying to create a server program to recieve and send text msgs to and from clients. im trying ...

  1. #1
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    362

    Threading problem

    hi im trying to create a server program to recieve and send text msgs to and from clients. im trying to create a seperate thread for the function that listens to clients, so that the server can also send data while it listen. i create the thread like this:

    Code:
    void CServerDlg::OnBnClickedBstart()
    {
        HANDLE hT1 =  CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ListenToClient,NULL,0,NULL); 
        WaitForSingleObject(hT1, INFINITE);
    }
    but when thread starts, the program hangs (because of the infinite loop inside the ListenToClient() function). (ListenToClient() is not a class function.) so creating a different thread like this dosen't seems to work with MFC? so how can i run ListenToClient() seperately so it dosen't hangs programs' UI?

    thanks for any help.
    Some useless programs i've wrote : slprogrammersheaven
    Youtube: http://www.youtube.com/user/geekyzine
    My amature IT Blog: http://everything-geeky.blogspot.com/
    My Flash games: http://flashweed.blogspot.com/

    (and, sorry for my amature english)

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    Is this a blocking socket?

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    the program hangs (because of the infinite loop inside the ListenToClient() function).
    Programs hans up because you do not exit the OnClicked callack till the created thread is finiished. Remove you waitforsingleObject - It just kills the idea of creating thread
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    362
    ok thanx.
    Some useless programs i've wrote : slprogrammersheaven
    Youtube: http://www.youtube.com/user/geekyzine
    My amature IT Blog: http://everything-geeky.blogspot.com/
    My Flash games: http://flashweed.blogspot.com/

    (and, sorry for my amature english)

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    1) Do NOT cast the address you pass to CreateThread. If it doesn't compile without it, then your prototype function is incorrect.
    2) For MFC, you should use AfxBeginThread and not CreateThread (I'm assuming you are?).
    3) And yes, you're basically blocking until the thread exits.
    Last edited by Elysia; 12-14-2007 at 05:25 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    3) To actual way to get the address of a function is to do &Function and not Function; this is non-standard and supported in versions prior to Visual Studio 2005.
    The above is incorrect.

    Code:
    D:\temp>gcc -Wall -pedantic -ansi t.c
    
    D:\temp>cat t.c
    #include <stdio.h>
    void foo(void) {
       printf("In foo\n");
    }
    void (*f)(void) = foo;
    
    int main()
    {
       f();
      return 0;
    }
    
    D:\temp>a
    In foo
    No ampersand needed to take an address of a function. [This is why it also works to write "foo;" in a line of code, and at worst(best?), this gives a warning for "statement with no effect"].

    I tried with g++ too, just in case.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    From what I understand, Visual C++ 2005 was made to be more standards compliant.
    Maybe it just applies to classes? I'll have to try.
    From what I see, it only applies to class members. I'd have preferred FunctionName wouldn't work, but apparently it does...
    Last edited by Elysia; 12-14-2007 at 05:25 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21