Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1071
  • Last Modified:

Console.In.ReadLine() ...my program hangs "sometimes"

I'm messing around with making a utility and i'm trying to test some code to accept input from the console.

Here's my problem...

Console.Read()   HANGS... but only sometimes, when I PIPE text into it.
For example.....

ECHO test1,test2 | splitcmd -d "," -v 1

Returns ...   test2

However, sometimes the program hangs waiting for more input.
I can't figure out how to "break" out of the loop or stop reading if nothing is there to read.  I'm at a loss as to why it would exit normally as expected only *sometimes...running the same exact test command as shown above.  Now if I ..re-pipe it to another program it exits normally ever single time.   {straaaaange}

WHAT is the "proper" method to read from the console input when PIPE'ed information is to be read????  How can I break out of Console.Read() ??

I was reading about a "beginread" method, but I have no idea how to implement it and cannot find any documentation for using it.
input = Console.ReadLine()
                Do Until input Is Nothing


                    output = input.Split(delimiter).GetValue(valueindex).ToString
                    Console.Write(output)
                    Try
                        input = Console.ReadLine()
                    Catch ex As Exception

                    End Try


                Loop

Open in new window

0
Ron Malmstead
Asked:
Ron Malmstead
  • 7
3 Solutions
 
Ron MalmsteadInformation Services ManagerAuthor Commented:
More details...

The reason I don't just readline() once then exit...is because I want to process every line until there are none...

So if the console is getting input:

test1,test2
test3,test4
test5,test6

....it returns one split calc for each line.
test2
test4
test6

Split is not the actual code i'm going to implement, but i'm just using this to test the method in general until I get the In/Out working as expected.
0
 
Ron MalmsteadInformation Services ManagerAuthor Commented:
help...
0
 
jhshuklaCommented:
try setting [input = Nothing] in Catch block.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
käµfm³d 👽Commented:
However, sometimes the program hangs waiting for more input.
Is this always using the test case:

ECHO test1,test2 | splitcmd -d "," -v 1

Open in new window

0
 
Ron MalmsteadInformation Services ManagerAuthor Commented:
Kaugmed,

Actually no...
I was wrong...

I actually have two of these progs, and when I use them in tandem is when this happens...*sometimes*.

This is the actual test, and "replacecmd", is basically the same exact code as above, but using replace instead of split functions.

C:\>echo TEST,test2,test3,testtest,test4 | c:\replacecmd.exe -o "es" -n "TESTINGS"
| c:\splitcmd.exe -d "," -v 3

Either one of them works fine when used ONLY with echo test |  ... it's when I chain them together that i'm having this problem...*sometimes*.  The cursor just hangs after returning the value, and I have to CTRL+C to end it.  Also as I said before, if I "re-pipe" this into something else it exits normally everytime without any blocking.

I'm trying to create a utility..that really has nothing to do with the above, but I want it to accept piped information and exit as it's supposed to.

Kind of like...  dsquery dsget  utilities ...where you can chain them and pipe information from one to the other.
0
 
Ron MalmsteadInformation Services ManagerAuthor Commented:
jhshukla,

Tried it, didn't work.  Same result.
0
 
Ron MalmsteadInformation Services ManagerAuthor Commented:
...ok... I think I figured it out.

I was doing Console.Write ...instead of Console.WriteLine

I think that was holding up Console.ReadLine(), because of the lack of carriage return in Write.

I'm going to hold open this question though in case this leads to other issues.
0
 
Ron MalmsteadInformation Services ManagerAuthor Commented:
Apparently, from everything i've read.... console.in.read ,  console.in.readline , and console.in.readtoend

....all block indefinitely!!...that is, unless data is actually being piped in.
This causes the console to hang indefinitely.

After spending hours of frustrating attempts at hacks and workarounds...(due to a personality defect I have with not being able to make things do what I want them to do)... i've come to accept that this is by design.


0
 
Ron MalmsteadInformation Services ManagerAuthor Commented:
The solution is... there is no solution apparently.

Behaviour is by design.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now