I am trying to show a message box on top of my form and get the result. I am using backgroundworker and in my dowork function, I am calling below function. I used delegates based on my past experience for show the message box but I am not sure what is wrong it is giving me below error.
e.Message = "Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on."
Would you mind check why I am getting above error even after using delegates?
public delegate DialogResult Del(Form1 fr,string text, string title, MessageBoxButtons mbb, MessageBoxIcon mbi);
public static DialogResult showMessageBox(Form1 fr, string text, string title, MessageBoxButtons mbb, MessageBoxIcon mbi)
{
return MessageBox.Show(fr, text, title, mbb, mbi);
}
public static OutputEventArgs execSync(string exe, string arguments, string standardInput,Form1 fr)
{
OutputEventArgs oea = new OutputEventArgs();
try
{
using (Process myProcess = new Process())
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.FileName = exe;
startInfo.Arguments = arguments;
myProcess.StartInfo = startInfo;
myProcess.Start();
string line = string.Empty;
while (!myProcess.StandardOutput.EndOfStream)
{
line = myProcess.StandardOutput.ReadLine();
if (line.Contains("is already defined in archive"))
{
Del handler = new Del(showMessageBox);
DialogResult dialogResult = handler(fr, "Do you want overwrite the version label?", "OverWrite",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
if(dialogResult == DialogResult.Yes)
myProcess.StandardInput.WriteLine("y");
else
myProcess.StandardInput.WriteLine("n");
}
}
oea.Data = line;
myProcess.WaitForExit();
oea.exitCode = myProcess.ExitCode;
}
}
catch (Exception e)
{
oea.Data = e.Message;
oea.ExceptionHappened();
}
return oea;
}