Click here to Skip to main content
15,916,280 members
Home / Discussions / C#
   

C#

 
AnswerRe: Invert Bitmap Pin
Judah Gabriel Himango20-Apr-06 12:47
sponsorJudah Gabriel Himango20-Apr-06 12:47 
Questionregistering a dll at runtime Pin
shezh20-Apr-06 6:44
shezh20-Apr-06 6:44 
AnswerRe: registering a dll at runtime Pin
Le centriste20-Apr-06 7:05
Le centriste20-Apr-06 7:05 
Questionlate binding to VB6 dlls Pin
shezh20-Apr-06 6:11
shezh20-Apr-06 6:11 
Questioncreating a txt file Pin
john3420-Apr-06 5:42
john3420-Apr-06 5:42 
AnswerRe: creating a txt file Pin
J4amieC20-Apr-06 5:51
J4amieC20-Apr-06 5:51 
AnswerRe: creating a txt file Pin
AETaylor21-Apr-06 1:54
AETaylor21-Apr-06 1:54 
QuestionI/O Threading program Pin
eric_tran20-Apr-06 4:55
eric_tran20-Apr-06 4:55 
Please help me

I wouldn't know how to modify my coding

I'd like to use 2 threads running concurrently. Thread 1 reads input 1 character from keyboard at a time. Thread 2 outputs 1 character at a time to the console.



using System;
using System.Text;
using System.Threading;

class Program
{
    static String m2 = "Press any combination of CTL, ALT, and SHIFT modifier keys, " +
                    "and a console key.\nPress the Escape (Esc) key to quit: ";
    static String m3 = "You pressed ";
    static String m4 = " (character '{0}').";

    static StringBuilder sb = new StringBuilder();
      
                                                
    public static void Main()
    {
        bool write = false;

        Thread t1 = new Thread(new ParameterizedThreadStart(Program.Input));
        Thread t2 = new Thread(new ParameterizedThreadStart(Program.Output));

        Console.WriteLine("Thread 1 starting...");
        t1.Start(write);

        Console.WriteLine("Thread 2 starting...");
        t2.Start(write);
    }


    public static void Input(object param)
    {
        bool isWrite = (bool)param;

        ConsoleKeyInfo cki;

        //StringBuilder sb = new StringBuilder();

        Console.TreatControlCAsInput = true;

        do
        {
            Console.WriteLine(m2);
            sb.Length = 0;
            cki = Console.ReadKey(true);
            sb.Append(m3);
            if (cki.Modifiers != 0)
            {
                if ((cki.Modifiers & ConsoleModifiers.Alt) != 0)
                    sb.Append("ALT+");
                if ((cki.Modifiers & ConsoleModifiers.Shift) != 0)
                    sb.Append("SHIFT+");
                if ((cki.Modifiers & ConsoleModifiers.Control) != 0)
                    sb.Append("CTL+");
            }

            sb.Append(cki.Key.ToString());
            sb.AppendFormat(m4, cki.KeyChar);
            sb.AppendLine().AppendLine();
            Console.WriteLine(sb.ToString());
            isWrite = true;
        } while (cki.Key != ConsoleKey.Escape);

    }

    public static void Output(object param)
    {
        bool isWrite = (bool)param;
        while (isWrite)
        Console.WriteLine(sb.ToString());
    }

}


eric
AnswerRe: I/O Threading program Pin
LongRange.Shooter20-Apr-06 7:10
LongRange.Shooter20-Apr-06 7:10 
Questioninsert in database Pin
guest_098720-Apr-06 4:34
guest_098720-Apr-06 4:34 
QuestionHTTP Sniffer Pin
ksor20-Apr-06 4:34
ksor20-Apr-06 4:34 
QuestionHow to update data from an access database? Pin
nulkoud20-Apr-06 4:30
nulkoud20-Apr-06 4:30 
QuestionHow to make Web Application more Secure in Asp.Net???? Pin
pal_vaidya20-Apr-06 4:14
pal_vaidya20-Apr-06 4:14 
AnswerRe: How to make Web Application more Secure in Asp.Net???? Pin
LongRange.Shooter20-Apr-06 7:26
LongRange.Shooter20-Apr-06 7:26 
QuestionCreating playlists Pin
mickeyfitzray20-Apr-06 3:21
mickeyfitzray20-Apr-06 3:21 
AnswerRe: Creating playlists Pin
LongRange.Shooter20-Apr-06 7:19
LongRange.Shooter20-Apr-06 7:19 
AnswerRe: Creating playlists Pin
malharone20-Apr-06 9:56
malharone20-Apr-06 9:56 
QuestionASP.NET is not working Pin
Mamphekgo20-Apr-06 3:09
Mamphekgo20-Apr-06 3:09 
AnswerRe: ASP.NET is not working Pin
Robert Rohde20-Apr-06 3:12
Robert Rohde20-Apr-06 3:12 
GeneralRe: ASP.NET is not working Pin
Mamphekgo20-Apr-06 3:35
Mamphekgo20-Apr-06 3:35 
GeneralRe: ASP.NET is not working Pin
LongRange.Shooter20-Apr-06 7:22
LongRange.Shooter20-Apr-06 7:22 
GeneralRe: ASP.NET is not working Pin
Mamphekgo20-Apr-06 22:28
Mamphekgo20-Apr-06 22:28 
Questionopen IE from win applicatin Pin
mahmoud_sama20-Apr-06 2:42
mahmoud_sama20-Apr-06 2:42 
AnswerRe: open IE from win applicatin Pin
Robert Rohde20-Apr-06 2:58
Robert Rohde20-Apr-06 2:58 
AnswerRe: open IE from win applicatin Pin
Eric Dahlvang20-Apr-06 4:15
Eric Dahlvang20-Apr-06 4:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.