Cannot stop background worker

Go To StackoverFlow.com

0

I am coding a bot that's on another tread / background worker and I am trying to stop it, but after pressing cancel the bot is still working o.O

Code is here: //Stop my bot

        private void botOff_Click(object sender, EventArgs e)
        {
            bot_worker.WorkerSupportsCancellation = true;
            if (bot_worker.IsBusy)
                bot_worker.CancelAsync();
            botOn.Text = "Enable";
            botOn.Enabled = true;
            botOff.Enabled = false;
        }
    }
}

//start my bot

private void botOn_Click(object sender, EventArgs e)
{
    if (toolStripLabel5.Text == "Not attached")
    {
        MessageBox.Show(notAttached, "Skype Pwnage - Info!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }
    else
    {
        botOn.Text = "Running";
        botOn.Enabled = false;
        botOff.Enabled = true;
        bot_worker.RunWorkerAsync();
    }
}

Edit, followed a commenters link and got this code below it does nothing at all, it still continues to run

private void bot_worker_DoWork(object sender, DoWorkEventArgs e)
{
    {
        BackgroundWorker worker = sender as BackgroundWorker;

        for (int i = 1; (i <= 1); i++)
        {
            if ((worker.CancellationPending == true))
            {
                e.Cancel = true;
                break;
            }
            else
            {
                skype.Attach(7, false);
                skype.MessageStatus += new _ISkypeEvents_MessageStatusEventHandler(skype_MessageStatus);
            }
        }
    }
}
private void skype_MessageStatus(ChatMessage msg, TChatMessageStatus status)
{
    try
    {
        string command = msg.Body.Remove(0, trigger.Length).ToLower();
        string[] lines = richTextBox4.Text.Split('\n');
        foreach (string ln in lines)
        {
            string[] commands = ln.Split(':');
            if (command == commands[0])
            {
                listBox2.Items.Add(commands[0]);
                skype.SendMessage(msg.Sender.Handle, string.Format(commands[1]));
                break;
            }
        }
    }
2012-04-05 00:20
by TomSwoobs


1

You do realise that you need to check the cancellation (by checking CancellationPending for example) in the DoWork() method? The CancelAsync() does not "magically stop the worker", it merely 'signals' that you want to cancel the worker; the worker needs to abort whatever it's doing, clean up it's stuff etc. and then return. This example might clear it up for you.

Also, from the CancelAsync documentation:

The CancelAsync method submits a request to stop the background operation and sets the CancellationPending property to true.

When you call CancelAsync, your background operation will be able to stop and exit. The operation code should periodically check the CancellationPending property to see whether it has been set to true.

2012-04-05 00:24
by RobIII
check update, I'm unsure what to do.. it's still failing - TomSwoobs 2012-04-05 00:56
Now you're just copying/pasting without having an utter clue on what you're doing. The intent of the example is that it demonstrates some kind of usage. Not to just copy/paste it. A more complete explanation can be found here, here and here - RobIII 2012-04-05 01:02
Another Hint: Where's the "work loop"? What do you think for (int i = 1; (i <= 1); i++) does? Why is it there in the first place (let me answer that: copy/pasted it blindly, didn't you? :P ). And why use a backgroundworker for this anyway? You call some Attach() method once and set up an eventhandler; what's the use of the backgroundworker here - RobIII 2012-04-05 01:08
I need it threaded / background worker so my app does not slow down nor freeze, and besides I need a way to cancel it.. I do not know any other way - TomSwoobs 2012-04-05 01:11
I am not familiar with the "skype" object you're using; I have no clue if the skype object supports some sort of Cancel() method, no idea what .Attach(7, false) does and even if it supports asynchronous operations. You might want to check out this and then check out the "skype" object's documentation for a way to "cancel" whatever it is that you're doing. Quite honestly; judging from your post your probably not ready for this kind of stuff quite yet. You might want to grasp the basics a little more before continuing - RobIII 2012-04-05 01:19
Ehh.. my app is around 4k lines so far, but never used background worker like this.. I use threading for handing message sending ect.. I started C# about a month in a half ago. So stuff I've never used I'm a little rough on - TomSwoobs 2012-04-05 01:28
Ads