I am a newbie and trying to learn the right way.
Is it acceptable to use a Thread within a constructor to avoid gui(Form) to freeze when an object is created? I will reuse this class often.
class Cmd
{
protected static string parameters;
protected HashSet<string> list_result;
public Cmd( string parameters)
{
Thread Thread1 = new Thread(new ThreadStart(Process1));
Thread1.Start();
Thread1.Join();
}
private void Process1()
{
ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd", "/c " + parameters);
processStartInfo.RedirectStandardOutput = true;
processStartInfo.RedirectStandardError = true;
processStartInfo.CreateNoWindow = true;
processStartInfo.UseShellExecute = false;
Process process = Process.Start(processStartInfo);
list_result = new HashSet<string>();
while (!process.StandardOutput.EndOfStream)
{
string line = process.StandardOutput.ReadLine();
list_result.Add(line);
}
}
If you would like to avoid freeze in your UI when doing a time-consuming task, you should add
BackgroundWorker
to your form and run your task in its event handlerYou can find the example here: https://msdn.microsoft.com/en-us/library/cc221403%28v=vs.95%29.aspx
You should also consider using newer async/await logic which is usually better than BackgroundWorker, as Panagiotis Kanavos mentioned in his answer