A
airwot4
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:
http://msdn.microsoft.com/en-us/library/ms951109.aspx
Thanks
private void btnFSearch_Click_1(object sender, EventArgs e)
{
//Starts search operation or cancels running operation
....
switch (_state)
{
case RunState.Idle:
_state = RunState.Running;
btnFSearch.Text = "Cancel";
FSearchDeleteDelegate delMain = new
FSearchDeleteDelegate(FileExistsLoop);
delMain.BeginInvoke(arComputers, txtFSearch.Text, null, null);
break;
case RunState.Running:
_state = RunState.Cancelled;
btnFSearch.Enabled = false;
break;
case RunState.Cancelled:
MessageBox.Show("Operation currently cancelling");
break;
}
}
public void FileExistsLoop(ArrayList arComputers, string
strFileSearchPath)
{
//Loops through arComputers and logs back to main UI.
bool cancel = false;
AppendLog2("Checking existance of " + strFileSearchPath + " on " +
arComputers.Count + " computers.", out cancel);
foreach (string strComputer in arComputers)
{
if (cancel) break;
if (Functions.PingTest(strComputer))
{
if (Functions.FileExists(strComputer, strFileSearchPath))
AppendLog2(strComputer + " = file exists", out cancel);
else
AppendLog2(strComputer + " = file not found", out cancel);
}
else
AppendLog2(strComputer + " is not responding to ping requests",
out cancel);
}
AppendLog2("***Finished***", out cancel);
}
public void AppendLog2(string log, out bool cancel)
{
if (txtOutput.InvokeRequired)
{
LogDelegate2 delInstance = new LogDelegate2(AppendLog2);
object inoutCancel = false;
Invoke(delInstance, new object[] { log, inoutCancel });
cancel = (bool)inoutCancel;
}
else
{
//Check for cancel
cancel = (_state == RunState.Cancelled);
// 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:
http://msdn.microsoft.com/en-us/library/ms951109.aspx
Thanks
private void btnFSearch_Click_1(object sender, EventArgs e)
{
//Starts search operation or cancels running operation
....
switch (_state)
{
case RunState.Idle:
_state = RunState.Running;
btnFSearch.Text = "Cancel";
FSearchDeleteDelegate delMain = new
FSearchDeleteDelegate(FileExistsLoop);
delMain.BeginInvoke(arComputers, txtFSearch.Text, null, null);
break;
case RunState.Running:
_state = RunState.Cancelled;
btnFSearch.Enabled = false;
break;
case RunState.Cancelled:
MessageBox.Show("Operation currently cancelling");
break;
}
}
public void FileExistsLoop(ArrayList arComputers, string
strFileSearchPath)
{
//Loops through arComputers and logs back to main UI.
bool cancel = false;
AppendLog2("Checking existance of " + strFileSearchPath + " on " +
arComputers.Count + " computers.", out cancel);
foreach (string strComputer in arComputers)
{
if (cancel) break;
if (Functions.PingTest(strComputer))
{
if (Functions.FileExists(strComputer, strFileSearchPath))
AppendLog2(strComputer + " = file exists", out cancel);
else
AppendLog2(strComputer + " = file not found", out cancel);
}
else
AppendLog2(strComputer + " is not responding to ping requests",
out cancel);
}
AppendLog2("***Finished***", out cancel);
}
public void AppendLog2(string log, out bool cancel)
{
if (txtOutput.InvokeRequired)
{
LogDelegate2 delInstance = new LogDelegate2(AppendLog2);
object inoutCancel = false;
Invoke(delInstance, new object[] { log, inoutCancel });
cancel = (bool)inoutCancel;
}
else
{
//Check for cancel
cancel = (_state == RunState.Cancelled);
// Check for completion
if (cancel || (log == "***Finished***"))
{
_state = RunState.Idle;
cancel = true;
...
}
...
}
return;
}