-
Jan 7th, 2023, 01:23 AM
#1
Thread Starter
Hyperactive Member
Showing Console Output On Label Is Not Working
Greetings,
i am just trying to show the console output to label text. but it's just not working..
i have tired hours and hours of this and that but none is working.. and now came here for support
here is the code i have tried so far:
Code:
private static void ExecuteCLI(string[] args, string workingDir, Form form, Label label)
{
List<string> lArgs = new List<string>(args);
lArgs.RemoveRange(0, 3);
string cliFile = args[2];
string cliArgs = string.Join(" ", lArgs.ConvertAll(item => item.Contains(" ") == true ? '"' + item + '"' : item));
//create working directory if not exits
if(Directory.Exists(workingDir) == true && isIde == true)
{
label.Text = "Deleting Working Dir...";
foreach(string filePath in Directory.GetFiles(workingDir, "*.*", SearchOption.AllDirectories))
{
new FileInfo(filePath).Attributes = FileAttributes.Normal;
File.Delete(filePath);
}
Directory.Delete(workingDir, true);
label.Text = "Status Goes Here...";
}
if(Directory.Exists(workingDir) == false)
{
Directory.CreateDirectory(workingDir);
}
Process process = new Process()
{
StartInfo = new ProcessStartInfo()
{
FileName = cliFile,
Arguments = cliArgs,
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
RedirectStandardOutput = true,
WorkingDirectory = workingDir,
},
};
process.OutputDataReceived += delegate (object sender, DataReceivedEventArgs e)
{
if(e.Data == null)
{
return;
}
string cliMSG = e.Data.Trim();
if(cliMSG.StartsWith("Extracting") == true)
{
System.Diagnostics.Debug.WriteLine(cliMSG); //if no method is used, it's shows the output on debug/output window just fine..
//sUpdateAddressBar(form, label, cliMSG); (3rd Method, crash/keeps loading forever without updating)
//setLabelText(label, cliMSG); (2nd Method, crash/keeps loading forever without updating)
/* // (1st method though don't crash the app, just don't updte the
form.BeginInvoke((Action)(() =>
{
label.Text = cliMSG;
}));
*/
}
};
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
process.Dispose();
form.DialogResult = DialogResult.OK;
}
private static void setLabelText(Label label, string text)
{
if(label.InvokeRequired)
{
label.Invoke((System.Action)(() => setLabelText(label, text)));
}
else
{
label.Text = text;
}
}
private delegate void sCBUpdateAddresBar(Form form, Label label, string text);
private static void sUpdateAddressBar(Form form, Label label, string text)
{
if(form.InvokeRequired)
{
form.Invoke(new sCBUpdateAddresBar(sUpdateAddressBar), form, label, text);
}
else
{
label.Text = text;
}
}
as i have noted in comments no method i have tried does works
here is the screen shot of crashed/loading forever: (i red marked the area to identify the cursor)
can any one suggest me what i am doing wrong? here?
best regards
-
Jan 7th, 2023, 05:30 AM
#2
Re: Showing Console Output On Label Is Not Working
There's a whole lot of noise in that code. In situations like this, you should ALWAYS strip it back to the bare essentials and then build it up bit by bit until it breaks. That way, you know exactly what part is the problem. That's all part of software development. It's not just write code, re4ad code fix code.
Also, have you actually debugged the code? You need to set breakpoints and step through the code to see whether it does what you expect and, if not, where and how it differs. That's more complex in multithreaded scenarios but that's why you need to simplify the code.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|