|
I have a process, i can start, and hide working fine, but i want to read from the console program, when i runs, not after, i tried to run a timer, anbd read at the tick, but my program just crashes and when it not do, i get nothing at all.
startInfo= new ProcessStartInfo("cmd.exe");
startInfo.Arguments ="/C uus.exe "+ arg.ToString();
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
this.timer1.Enabled=true;
this.listBox1.Items.Clear();
p= Process.Start(startInfo);
Application.DoEvents();
void Timer1Tick(object sender, EventArgs e)
{
string str="";
str=p.StandardOutput.ReadLine();
if(str != null)
{
this.Text=str.ToString();
this.listBox1.Items.Add(str);
}
Application.DoEvents();
}
So what do i do to solve this?
Update: I tried a solution i found now My program don't crash anymore, but it also don't recevie any data at all
proc.StartInfo.UseShellExecute=false;
proc.StartInfo.CreateNoWindow=true;
proc.StartInfo.RedirectStandardOutput=true;
proc.StartInfo.RedirectStandardError=true;
proc.StartInfo.FileName="uus.exe";
proc.StartInfo.Arguments=arg;
proc.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(SortOutputHandler);
proc.Start();
proc.BeginOutputReadLine();
void SortOutputHandler(object o,System.Diagnostics.DataReceivedEventArgs e)
{
string str="";
string str2="";
str=e.Data.ToString();
if(str!=null && str!="")
{
this.listBox1.Items.Add(str.ToString());
this.Text=str.ToString();
}
str2=proc.StandardOutput.ReadLine();
if(str2!=null && str2!="")
{
this.lsw1.Items.Add(str2.ToString());
}
}
The program is a process whichs take some arguments, and if no error happends run and close is self, what i'am trying to do is run the process, read how long the process is done with the task, while it does the task not after, and write it in my program
some body have a working solution?
|
|
|
|
|
Hi,
there are several correct ways to read data from another process' output (and/or error) stream:
1.
read all at once from a stream as in string output = p.StandardOutput.ReadToEnd(); . The main drawback is this only produces anything when the process p has finished.
2.
use a separate thread which synchronously reads the output/error stream (that is two threads if both streams are required)
3.
use the DataReceived event; you should not have Read calls in the handler!
MSDN says: The event is enabled during asynchronous read operations on StandardOutput. To start asynchronous read operations, you must redirect the StandardOutput stream of a Process, add your event handler to the OutputDataReceived event, and call BeginOutputReadLine. Thereafter, the OutputDataReceived event signals each time the process writes a line to the redirected StandardOutput stream, until the process exits or calls CancelOutputRead.
BTW: you have way too many ToString() calls in your code; it does not make sense at all to call ToString() on a string object; e.g. DataReceivedEventArgs.Data is a string all by itself.
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
|
|
|
|
|
1. If you have read my post, you will know that solution wont work, that it reads after, not while.
2. I dont know here what you mean.
3. Ise'nt it what i have tried in the lower code? if, I get no data at all.
|
|
|
|
|
thomaxz.tc wrote: Ise'nt it what i have tried in the lower code?
No
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
|
|
|
|
|
Then i not sure what you mean?
|
|
|
|
|
Hi,
1.
Why do you ignore the documentation, it says:
The event is enabled during asynchronous read operations on StandardOutput. To start asynchronous read operations, you must redirect the StandardOutput stream of a Process, add your event handler to the OutputDataReceived event, and call BeginOutputReadLine. Thereafter, the OutputDataReceived event signals each time the process writes a line to the redirected StandardOutput stream, until the process exits or calls CancelOutputRead.
2.
The DataReceived event executes on a ThreadPool thread, not on the main thread; so you are not allowed to touch any Control, however you could use Console.WriteLine() for debugging purposes. To touch Controls, you need the Control.InvokeRequired/Control.Invoke pattern.
BTW: Setting Control.CheckForIllegalCrossThreadCalls false is NOT the solution.
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
|
|
|
|
|
>1.
>Why do you ignore the documentation, it says:
>The event is enabled during asynchronous read operations on StandardOutput.
>To start asynchronous read operations, you must redirect the StandardOutput stream of a Process,
As in my lower code
proc.StartInfo.RedirectStandardOutput=true;
Ise'nt that an redirect?
>add your event handler to the OutputDataReceived event
proc.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(SortOutputHandler);
Ise'nt that an event handler for the OuptuDataRecevide?
>And call BeginOutputReadLine.
proc.Start();
proc.BeginOutputReadLine();
Ise'nt that a Call to BeginOutputReadLine?
>Thereafter, the OutputDataReceived event signals each time the process writes a line to the >redirected StandardOutput stream, until the process exits or calls CancelOutputRead.
I still get no data.
2.
>The DataReceived event executes on a ThreadPool thread, not on the main thread; so you are not >allowed to touch any Control, however you could use Console.WriteLine() for debugging purposes. To >touch Controls, you need the Control.InvokeRequired/Control.Invoke pattern.
>BTW: Setting Control.CheckForIllegalCrossThreadCalls false is NOT the solution.
OK? all the other time i have code treate and i wasen allowed i was getting a cross thread error, with i don't do here.
futher more have i change my code af bit, do reflect invoek
private delegate void TextAdderDelegate(string str);
void TextAdder(string str)
{
if(this.lsw1.InvokeRequired==true)
{
Invoke(new TextAdderDelegate(TextAdder),new object[] {str});
}
else
{
this.lsw1.Items.Add(str);
}
}
void SortOutputHandler(object o,System.Diagnostics.DataReceivedEventArgs e)
{
string str="";
if(e!=null)
{
if(e.Data!=null)
{
str=e.Data.ToString();
}
}
TextAdder(str);
}
But I still not get any data.
And when i make a breakpoint, it only runs when the process finished.
|
|
|
|
|
hi
i want to load a lot of images to the memory.
of course every bitmap in the memory takes about 10 times more then in the file;
so (if there is no other way) i'm loading the image as a byte array:
private byte[] FileToByteArray (string path)
{
byte[] bytes = null;
using (System.IO.Stream stream = File.OpenRead(path))
{
using (System.IO.BinaryReader reader = new BinaryReader(stream))
{
bytes = reader.ReadBytes((int)reader.BaseStream.Length);
}
}
return bytes;
}
and store it into a byte array variable.
when i need the image i use:
private Image ByteArrayToImage(byte[] byteArrayIn)
{
if (byteArrayIn == null)
return null;
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
so far so good, the problem is if the image need to be change i send it to a function and get new bitmap, now i want to save it back to byte array, so if i use:
private byte[] ImageToByteArray(System.Drawing.Image imageIn)
{
if (imageIn == null)
return null;
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Png);
imageIn.Dispose();
return ms.ToArray();
}
i get a byte array that is about 10 times bigger. so how can i get a smaller byte array?
|
|
|
|
|
Hi,
several remarks:
1.
Image size (the file or memory footprint) depends on the image format; e.g. ImageFormat.Jpeg would be more economical than ImageFormat.Png due to the compression used.
2.
When a class offers a public Dispose() method, you should call it as soon as you are done with the object. MemoryStream is one of those.
3.
you must be aware that, the way you do things, causes each image to exist several times in memory, and being copied from one format to another: byte[], MemoryStream, Image
4.
are you sure you need byte arrays at all? maybe you could perform image operations on the MemoryStream directly. OTOH you might consider using a Bitmap constructor that builds a Bitmap from an existing array, without copying (see the ones with an IntPtr parameter)
5.
assuming your images/streams/arrays each take more than some 80KB, they may all end up in the "huge-object-heap" which does not have compaction; as a result after several operations you may run out of memory although your memory-in-use count may well be below your working set.
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
|
|
|
|
|
Luc Pattyn wrote: ImageFormat.Jpeg would be more economical than ImageFormat.Png due to the compression used.
Actually you don't know that for sure, it depends on the images.. what if they're diagrams or pictures of text? The OP didn't say..
|
|
|
|
|
The OP stated "every bitmap in the memory takes about 10 times more then in the file" so I assume compression has been doing a fine job on his original images and stands a good chance of doing the same on the final images.
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
|
|
|
|
|
Fair enough, but, who knows right? PNGOUT-ed PNGs can be a lot smaller than PNGs compressed by the .NET PNG compressor..
|
|
|
|
|
given time the OP will tell.
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
|
|
|
|
|
i need the png format because i'm useing the alpha channel. how does other programs do that ?
for example powerPoint ? in powerPoint i can put tens of images and the memory grows only with a few mb ?
|
|
|
|
|
Once you create the Bitmap object, in memory, the image is no longer compressed, but is a full 32-bit render of what the file says it should look like.
|
|
|
|
|
nryk wrote: private byte[] FileToByteArray (string path)
Just use the File.ReadAllBytes method.
nryk wrote: of course every bitmap in the memory takes about 10 times more then in the file
nryk wrote: i get a byte array that is about 10 times bigger
From that I can deduct that you either managed to change the image into an image that is ten times larger (e.g. three times larger in both x and y axis), or you have changed it into something that the Save method could not compress at all. Could you post example image files somewhere for examination?
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
i managed to see the problem: i'm loading jpg files, but in my app i need it as png file ( because of the alpha channel )so when i save it to the memoryStreem as ImageFormat.Png the png is less compressed,is it possible to compress the png file to something like 0.5 mb - 1 mb like jpg ?
|
|
|
|
|
That's hard when the source is a jpg. The PNG will preserve all the JPG compression artifacts resulting in a much bigger file (compared to what it would have been if the original source had directly been PNG-compressed) which essentially doesn't contain any extra useful information - except possibly for the Alpha channel (if present).
|
|
|
|
|
No, there is no lossy compression for PNG. The high compression rate of JPEG is simply done by throwing away the data that has least visual impact on the result.
It would of course be possible to write your own compression algorithm that does that, but it would be very hard to get the same quality - compression ratio as JPEG compression, as the compression used in a PNG is not at all designed for lossy compression.
Another alternative that is a bit more promising would be to extract the color data and alpha channel separately, and compress the color data as a regular JPEG image and the alpha data as a grayscale JPEG image.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Guffa wrote: Another alternative that is a bit more promising would be to extract the color data and alpha channel separately, and compress the color data as a regular JPEG image and the alpha data as a grayscale JPEG image.
and how do i do that ? and how do i combine them together later ? is it with LockBits? and run through every "Pixel" ? is there any other file type like png that have the alpha channel and still
remain small size ?
|
|
|
|
|
Yes, you would use LockBits to get access to the pixel data.
No, there isn't any image format with alpha channel and the compression ratio of JPEG, at least not supported by the .NET framework.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Is this still viable in VS2008 or is there a newer way to accomplish docking panels?
tia
rafone
Statistics are like bikini's...
What they reveal is astonishing ...
But what they hide is vital ...
|
|
|
|
|
Others may offer alternatives, but as far as I know DockPanel Suite is still OK. On the other hand I have not looked around for some while.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Thanks Henry;
I have not really seen any better alternatives but I am open to suggestions. I was not sure if MS had included this with 2008 or 2010.
thanks for the info
rafone
Statistics are like bikini's...
What they reveal is astonishing ...
But what they hide is vital ...
|
|
|
|
|
Please help me on this code.
Am trying to load data from sql server 2005 express into textbox in visual c# 2008 express. I want the data to be displayed in the textbox from the first record in the database but am having this error message
"Ths best overload method match for 'system.data.sqlclient.sqlcommand.sqlcommand(string,system.data.sqlclient.sqlcommand)' has some invalid arguments.
Please help cos I dont know what to do about this.
Thanks
private void COMPANY_INFO_Load(object sender, EventArgs e)
{
btnAdd.Enabled = true;
btnCancel.Enabled = false;
btnUpdate.Enabled = true;
btnSave.Enabled = false;
SqlConnectionStringBuilder myBuilder = new SqlConnectionStringBuilder();
myBuilder.UserID = "sa";
myBuilder.Password = "admin123";
myBuilder.InitialCatalog = "Kay_Nylon_Db";
myBuilder.DataSource = "ADEMOLAPC";
myBuilder.ConnectTimeout = (30);
SqlConnection conn = new SqlConnection(myBuilder.ConnectionString);
SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand("select * from Company_Info", myBuilder);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
Console.WriteLine(myReader["Company_Id"].ToString());
Console.WriteLine(myReader["Company_Name"].ToString());
Console.WriteLine(myReader["Company_Address_Line1"].ToString());
Console.WriteLine(myReader["Company_Address_Line2"].ToString());
Console.WriteLine(myReader["Fax"].ToString());
Console.WriteLine(myReader["Email"].ToString());
}
}
|
|
|
|