I have the below code for a multithreaded winforms application. It
works successfully apart from the cancel function.
AppendLog2 has an out parameter which should return true if either of
two conditions are met (to cancel operation). This parameter is not
being passed back when AppendLog2 invokes itself, which is everytime
due to the operation.
Any ideas how I could better accomplish this? I followed this article:
Thanks
private void btnFSearch_Clic k_1(object sender, EventArgs e)
{
//Starts search operation or cancels running operation
....
switch (_state)
{
case RunState.Idle:
_state = RunState.Runnin g;
btnFSearch.Text = "Cancel";
FSearchDeleteDe legate delMain = new
FSearchDeleteDe legate(FileExis tsLoop);
delMain.BeginIn voke(arComputer s, txtFSearch.Text , null, null);
break;
case RunState.Runnin g:
_state = RunState.Cancel led;
btnFSearch.Enab led = false;
break;
case RunState.Cancel led:
MessageBox.Show ("Operation currently cancelling");
break;
}
}
public void FileExistsLoop( ArrayList arComputers, string
strFileSearchPa th)
{
//Loops through arComputers and logs back to main UI.
bool cancel = false;
AppendLog2("Che cking existance of " + strFileSearchPa th + " on " +
arComputers.Cou nt + " computers.", out cancel);
foreach (string strComputer in arComputers)
{
if (cancel) break;
if (Functions.Ping Test(strCompute r))
{
if (Functions.File Exists(strCompu ter, strFileSearchPa th))
AppendLog2(strC omputer + " = file exists", out cancel);
else
AppendLog2(strC omputer + " = file not found", out cancel);
}
else
AppendLog2(strC omputer + " is not responding to ping requests",
out cancel);
}
AppendLog2("*** Finished***", out cancel);
}
public void AppendLog2(stri ng log, out bool cancel)
{
if (txtOutput.Invo keRequired)
{
LogDelegate2 delInstance = new LogDelegate2(Ap pendLog2);
object inoutCancel = false;
Invoke(delInsta nce, new object[] { log, inoutCancel });
cancel = (bool)inoutCanc el;
}
else
{
//Check for cancel
cancel = (_state == RunState.Cancel led);
// Check for completion
if (cancel || (log == "***Finished*** "))
{
_state = RunState.Idle;
cancel = true;
...
}
...
}
return;
}
works successfully apart from the cancel function.
AppendLog2 has an out parameter which should return true if either of
two conditions are met (to cancel operation). This parameter is not
being passed back when AppendLog2 invokes itself, which is everytime
due to the operation.
Any ideas how I could better accomplish this? I followed this article:
Thanks
private void btnFSearch_Clic k_1(object sender, EventArgs e)
{
//Starts search operation or cancels running operation
....
switch (_state)
{
case RunState.Idle:
_state = RunState.Runnin g;
btnFSearch.Text = "Cancel";
FSearchDeleteDe legate delMain = new
FSearchDeleteDe legate(FileExis tsLoop);
delMain.BeginIn voke(arComputer s, txtFSearch.Text , null, null);
break;
case RunState.Runnin g:
_state = RunState.Cancel led;
btnFSearch.Enab led = false;
break;
case RunState.Cancel led:
MessageBox.Show ("Operation currently cancelling");
break;
}
}
public void FileExistsLoop( ArrayList arComputers, string
strFileSearchPa th)
{
//Loops through arComputers and logs back to main UI.
bool cancel = false;
AppendLog2("Che cking existance of " + strFileSearchPa th + " on " +
arComputers.Cou nt + " computers.", out cancel);
foreach (string strComputer in arComputers)
{
if (cancel) break;
if (Functions.Ping Test(strCompute r))
{
if (Functions.File Exists(strCompu ter, strFileSearchPa th))
AppendLog2(strC omputer + " = file exists", out cancel);
else
AppendLog2(strC omputer + " = file not found", out cancel);
}
else
AppendLog2(strC omputer + " is not responding to ping requests",
out cancel);
}
AppendLog2("*** Finished***", out cancel);
}
public void AppendLog2(stri ng log, out bool cancel)
{
if (txtOutput.Invo keRequired)
{
LogDelegate2 delInstance = new LogDelegate2(Ap pendLog2);
object inoutCancel = false;
Invoke(delInsta nce, new object[] { log, inoutCancel });
cancel = (bool)inoutCanc el;
}
else
{
//Check for cancel
cancel = (_state == RunState.Cancel led);
// Check for completion
if (cancel || (log == "***Finished*** "))
{
_state = RunState.Idle;
cancel = true;
...
}
...
}
return;
}
Comment