Thread: catch all printf of c process in c# application

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by C progammer View Post
    I dont understand you, do you say that it relate to sleep?

    Even ehen I edit my c code to , drop out the sleep and put some loops that take time
    Paste ofCode
    (those loops take about 18 seconds) , I get the "start" only when the c program finish
    Well, I think so, but I wouldn't say I've fully got the hang of Windows processes and threads -- I think your process starts in the same thread as your C# program, and so when your C program calls Sleep your C# program is also put to sleep, but I'm not able to find chapter and verse about it.

    Certainly when I used your original code, I didn't get any response from the C# program until after everything was finished. I was able to get an immediate response by modifying a program that did work, as mentioned above. I don't have anything C# here at home, but I can try your code when I get back to the office on Monday and see if those spin-waits give me the same trouble they're giving you.

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by C progammer View Post
    I dont understand you, do you say that it relate to sleep?

    Even ehen I edit my c code to , drop out the sleep and put some loops that take time
    Paste ofCode
    (those loops take about 18 seconds) , I get the "start" only when the c program finish
    Curiouser and curiouser. I see the same result, but if I add "fflush(stdout);" directly after the printf I get the start printout after about 3 seconds and then finish when it's done. Forcing stdout to be line buffered made no difference; forcing it to be unbuffered gave me the three-second delay again (using
    Code:
    setvbuf(stdout, NULL, _IONBF, 0);
    to do the forcing).

    This code
    Code:
    #include <iostream>
    #include <windows.h>
    
    main()
    {
        std::cout << "start" << std::endl;
        for (long i = 0; i < 573123264; i++) {}
    	for (long i = 0; i < 573123264; i++) {}
    	for (long i = 0; i < 573123264; i++) {}
    	for (long i = 0; i < 573123264; i++) {}
    	for (long i = 0; i < 573123264; i++) {}
    	for (long i = 0; i < 573123264; i++) {}
    	for (long i = 0; i < 573123264; i++) {}
    	for (long i = 0; i < 573123264; i++) {}
    	for (long i = 0; i < 573123264; i++) {}
    	for (long i = 0; i < 573123264; i++) {}
    	for (long i = 0; i < 573123264; i++) {}
    	for (long i = 0; i < 573123264; i++) {}
    	for (long i = 0; i < 573123264; i++) {}
    	
    	std::cout << "finish" << std::endl;
    }
    however does print "start" immediately for me (compiling with g++ since this classroom doesn't have visual studio in it). Perhaps there's some weird interaction between the old MSVCRT and the new C# stuff? How tied down are you to using C?

    EDIT TO ADD: Looking back, that numeric solver I was using above that worked was a C++ program; changing the std::cout to printf's gave me the same wait-until-everything's-done scenario. I'm not sure, though, why RedirectOutput is reading std::cout but not printf.
    Last edited by tabstop; 11-12-2018 at 07:18 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. best process for cross compiling an application
    By nanodog in forum C Programming
    Replies: 4
    Last Post: 10-26-2013, 04:04 PM
  2. Replies: 5
    Last Post: 09-04-2011, 01:22 AM
  3. ?writing bug in a multi-process Linux server application?
    By Arrummzen in forum C Programming
    Replies: 7
    Last Post: 12-14-2002, 11:53 AM
  4. Process still active when application exits
    By xds4lx in forum Windows Programming
    Replies: 13
    Last Post: 08-15-2002, 02:17 AM
  5. Application running as a process in Win2k?????
    By J_Bravo in forum Windows Programming
    Replies: 8
    Last Post: 05-07-2002, 04:02 AM

Tags for this Thread