https://www.google.com/contributor/welcome/?utm_source=publisher&utm_medium=banner&utm_campaign=2376261057261409

Search This Blog

Search with our Site

Custom Search

Friday, January 20, 2012

CS410 Assignment No 5 Solution & Discussion Due Date: 19-01-2012


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 withAllocConsole(), 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