Visual Programming (CS410)
Assignment # 5
Total marks = 20
Deadline Date = 19-01-2012
Q1 [Marks: 10]
I've allocated a console window in my GUI program, but if the user closes the console, my program closes too. What to do?
SOLUTION
One method that works well is to disable the close menu option. After the console has been allocated with
AllocConsole()
, you can do this if you can find the console window's handle.void DisableClose()
{
char buf[100];
wsprintf ( buf,
_T("some crazy but unique string that will ID ")
_T("our window - maybe a GUID and process ID") );
SetConsoleTitle ( (LPCTSTR) buf );
// Give this a chance - it may fail the first time through.
HWND hwnd = NULL;
while ( NULL == hwnd )
{
hwnd = ::FindWindowEx ( NULL, NULL, NULL, (LPCTSTR) buf );
}
// Reset old title - we'd normally save it with GetConsoleTitle.
SetConsoleTitle ( _T("whatever") );
// Remove the Close menu item. This will also disable the [X] button
// in the title bar.
HMENU hmenu = GetSystemMenu ( hwnd, FALSE );
DeleteMenu ( hmenu, SC_CLOSE, MF_BYCOMMAND );
}
Q2 [marks: 10]
I am trying to write an application (console based), in which one thread reads information from console and another thread prints the same information back to console. Write the code by applying synchronization logic.
SOLUTION
string content = "";
Thread thRead = new Thread(() =>
{
while (true)
{
content = Console.ReadLine();
}
});
thRead.Start();
Thread thWrite = new Thread(() =>
{
while (true)
{
if (!string.IsNullOrEmpty(content))
{
Console.WriteLine(content);
content = "";
}
}
});
thWrite.Start();
No comments:
Post a Comment