how avoids deadlock condition

2019-07-31 10:42发布

I try to write a program like a compiler. In this case I must simulate these codes of SQLPL (this one can be just for example). For example in command prompt I want to do these:

c:\> sqlplus
 ....
Enter User-Name:(here we must enter username) xxxx
Enter password:(same up)yyyyy
...
sql>(now I want to send my sql command to shell)prompt "rima";
"rima"  [<-output]
sql> prompt "xxxx"
sql> exit

very simple. I try to use this code:

ProcessStartInfo psi = new ProcessStartInfo(
@"sqlplus");
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
//psi.RedirectStandardError = true;
Process process = new Process();
process.StartInfo = psi;
bool started = process.Start();
if (started)
{
process.StandardInput.WriteLine("user/password");
process.StandardInput.Flush();
string ret = process.StandardOutput.ReadLine(); // <-- stalls here
System.Console.WriteLine("sqlplus says :" + ret + "\".");
}

I find out it form here but as if you read this code has problem! DEADLOCK Condition problem! This is my second time I ask my question, every time my knowledge growing, but I can't get how I can solve my problem at last!!!

So I kindly kiss your hand, please help me, this process is not clear for me. Any one can give me a code that handle my problem? Already I look at all codes, also I try to use ProcessRunner that in my last post some one offer me, but I can't use it also, I receive an error.

I hope by first example you find out what I want, and solve the second code problem...
I use C# for implementation, also my DB is Oracle
I don't want close sqlplus for each time connecting because it take time to log in again.

1条回答
倾城 Initia
2楼-- · 2019-07-31 11:10

You approach is not working for this example in my opinion. You wrote:

Enter User-Name:(here we must enter username) xxxx
Enter password:(same up)yyyyy

So it looks as if there is no newline involved. Since you are connecting to StandardOutput, which is buffered it will get only flushed when a newline character appears or a flush() is called directly. This is probably not the case (should be happen in sqlplus).

So as long as you can not change sqlplus and insert a flush or newline there, a synchronous read will not help you at all. Try to communicate asynchronous, like:

process.OutputDataReceived += OnOutputDataReceived;
process.Start();
process.BeginOutputReadLine();
// ... more missing code here ...

But this is a far more complex approach ...

查看更多
登录 后发表回答