Disassociating write to screen - command prompt from execution flow

  • Thread starter Thread starter ananda vardhana
  • Start date Start date
A

ananda vardhana

Guest
Hello,

I am writing to the screen using wprintf() and wcout() at various points in my app in a big while(). Example:

while(1) {
wprintf(L"Hello\n");
wcout << L"Hello again\n";
WriteToAfile(L"Abcdefgh\n");
}

When this is happening if I click on the screen the display freezes that is fine that is what I want but I want the while loop to continue and the write to happen. One solution is creating a thread but then I might end up with creating millions of threads so that is not a viable solution. So is there a way to Disassociate the write to the screen from program execution? In other words if the user clicks and freezes the screen WriteToAFile() should continue unfazed. If creating a write thread is the only solution is there a easy way to prevent millions of thread being created? I would say detect the mouse click and not create any more threads until user hits an ESC to unclick it. I have enclosed a sample program I wrote to demo it. If you can kindly suggest the way to disassociate that would be really very helpful.

#include<Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <cstdlib>
#include<conio.h >
#include <string>
#include <iostream>
#include <istream>
#include <sstream>
#include <fstream>

using namespace std;
wchar_t GCurrentPath[MAX_PATH];
wofstream GPerfDataLog;
bool Handler(int CtrlType);

DWORD WINAPI WritePerfCounter(LPVOID lpParameter)
{
while (1)
{
wprintf(L"sdlfkjssdlfjslfnsldfknslgfnlsdjslfjsldfjsldfjsldfjsldfjsldfjsdlfjsdlfjs;dfj23048723048972384dlfjsdlfjsdlfjsdfjsdfjslfjg\n");
Sleep(1000);
}
}

void main()
{
ZeroMemory(GCurrentPath, sizeof(GCurrentPath));
GetCurrentDirectoryW(MAX_PATH, GCurrentPath);
SetConsoleCtrlHandler((PHANDLER_ROUTINE)Handler, TRUE);

CreateThread(NULL, 0, WritePerfCounter, (void*)NULL, 0, NULL);
wstring GPerfDataLogPath(GCurrentPath);
GPerfDataLogPath += L"\\GPerfDataLog.log";
GPerfDataLog.open(GPerfDataLogPath, std::fstream::out);
while(1)
{
wprintf(L"Thread createdXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n");
GPerfDataLog << L"I am live\n";
Sleep(1000);
}
}

bool
Handler(int CtrlType) {
UNREFERENCED_PARAMETER(CtrlType);
GPerfDataLog.close();
ExitProcess(1);
}

Continue reading...
 
Back
Top