GRChandrashekar
asked on
Closing Thread
In below code, the code continues to execute inspite of application.exit and gives error Invoke or BeginInvoke cannot be called on a control until the window handle has been created.. How do I make sure application exits where the code is written for exit ?
private void ClubCentricUpdater_Load(ob ject sender, EventArgs e)
{
flashLabel.Text = @"Downloading Updates. Please Wait";
lblsupport.Text = @"Problems, if any, please contact IT Support";
ThreadStart ts = delegate
{
try
{
bool version = FileCompare(Comparesourcef ile,
Comparedestinationfile);
if (version)
{
//Exit this application and open the desired application
var rundesiredprocess = new Process();
rundesiredprocess.StartInf o.FileName =
desiredapplication;
rundesiredprocess.Start();
CloseForm(this);
Application.Exit();
}
FindAndKillProcess("notepa d");
filePaths = Directory.GetFiles(sourcep ath, "*.*",
SearchOption.AllDirectorie s);
Invoke(new MethodInvoker(delegate
{
progressBar.Maximum =
filePaths.Length;
progressBar.Step = 1;
}));
const int bufferSize = 32*1024; //buffer
foreach (string filelist in filePaths)
{
var file = new FileInfo(filelist);
string fileDirectory = file.DirectoryName;
string filename = file.Name;
var stringSeparators = new[] {sourcepath};
string[] result = fileDirectory.Split(
stringSeparators, StringSplitOptions.None);
int filedirectorycount = result.Length;
string directoryname = "";
if (filedirectorycount == 2)
{
directoryname = result[1] + "\\";
}
CopyFile(filelist,
destinationpath + directoryname +
filename,
directoryname, bufferSize);
Invoke(
new MethodInvoker(
() => progressBar.PerformStep()) );
}
Invoke(
new MethodInvoker(
delegate
{
flashLabel.Text =
@" Completed";
}));
}
catch (Exception)
{
Invoke(new MethodInvoker(delegate
{
flashLabel.Text =
@"The network path was not found.";
return;
}));
}
};
var th = new Thread(ts);
th.Start();
}
private void ClubCentricUpdater_Load(ob
{
flashLabel.Text = @"Downloading Updates. Please Wait";
lblsupport.Text = @"Problems, if any, please contact IT Support";
ThreadStart ts = delegate
{
try
{
bool version = FileCompare(Comparesourcef
Comparedestinationfile);
if (version)
{
//Exit this application and open the desired application
var rundesiredprocess = new Process();
rundesiredprocess.StartInf
desiredapplication;
rundesiredprocess.Start();
CloseForm(this);
Application.Exit();
}
FindAndKillProcess("notepa
filePaths = Directory.GetFiles(sourcep
SearchOption.AllDirectorie
Invoke(new MethodInvoker(delegate
{
progressBar.Maximum =
filePaths.Length;
progressBar.Step = 1;
}));
const int bufferSize = 32*1024; //buffer
foreach (string filelist in filePaths)
{
var file = new FileInfo(filelist);
string fileDirectory = file.DirectoryName;
string filename = file.Name;
var stringSeparators = new[] {sourcepath};
string[] result = fileDirectory.Split(
stringSeparators, StringSplitOptions.None);
int filedirectorycount = result.Length;
string directoryname = "";
if (filedirectorycount == 2)
{
directoryname = result[1] + "\\";
}
CopyFile(filelist,
destinationpath + directoryname +
filename,
directoryname, bufferSize);
Invoke(
new MethodInvoker(
() => progressBar.PerformStep())
}
Invoke(
new MethodInvoker(
delegate
{
flashLabel.Text =
@" Completed";
}));
}
catch (Exception)
{
Invoke(new MethodInvoker(delegate
{
flashLabel.Text =
@"The network path was not found.";
return;
}));
}
};
var th = new Thread(ts);
th.Start();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
private static void CloseForm(Form form)
{
if (form.IsDisposed) return;
if (form.InvokeRequired)
{
CloseMethod method = CloseForm;
form.Invoke(method, new object[] {form});
}
else
{
form.Close();
}
}
#region Nested type: CloseMethod
private delegate void CloseMethod(Form form);
#endregion