Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C#]Background Worker is busy or it freezes my app.

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Wiw3K
Grandmaster Cheater
Reputation: 0

Joined: 09 Jul 2006
Posts: 762
Location: Poland.

PostPosted: Wed Feb 11, 2009 3:22 pm    Post subject: [C#]Background Worker is busy or it freezes my app. Reply with quote

Hai~

If i do BackgroundWorker this way:

Code:
       private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
       {
           BackgroundWorker worker = sender as BackgroundWorker;

           if (backgroundWorker1.IsBusy != true)
           {
               SendKeys.SendWait("{1}");
           }
           else
           {
               MessageBox.Show("Error: Background Worker is currently Busy.", "FindWindow");
               return;
           }

       }


Application closing + MessageBox with error up there /\

if i do this way:

Code:
       private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
       {
           BackgroundWorker worker = sender as BackgroundWorker;
           SendKeys.SendWait("{1}");
       }


this works but it Freezes my whole Windows and i need to CTRL+ALT+DEL - Kill process...
Back to top
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Wed Feb 11, 2009 4:56 pm    Post subject: Reply with quote

Uhh...
So what are you asking?

I don't know what BackgroundWorker is, but I'd just use a thread, and use a queue for it, or something.

Code:

//using System;
//using System.Threading;
//using System.Collections.Generic;

private Thread _backgroundWorker;
private List<Action> _bgwQueue;

private void ThreadProc()
{
     for (;; Sleep(10) )
     {
          if (_bgwQueue.Count == 0)
              continue;
          _bgwQueue[0]();
     }
}

//...

//code:
if (_backgroundWorker == null)
{
    _backgroundWorker = new Thread(new ThreadStart(ThreadProc));
    _backgroundWorker.IsBackground = true;
    _backgroundWorker.Start();
}

_bgwQueue.Add(new Action( () => { SendKeys.SendWait("{1}"); }));


For stopping the worker:

Code:

_bgwQueue.Add(new Action( () => { Application.ExitThread(); }));

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
Wiw3K
Grandmaster Cheater
Reputation: 0

Joined: 09 Jul 2006
Posts: 762
Location: Poland.

PostPosted: Wed Feb 11, 2009 6:11 pm    Post subject: Reply with quote

@up
i am trying to make AutoClicker but fail :O cuz app freeze Shocked
Back to top
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Thu Feb 12, 2009 3:51 pm    Post subject: Reply with quote

Code:

private bool _stopClicker = false;
private Thread _autoClicker;

private void ClickLoop()
{
    for (; !_stopClicker; Thread.Sleep(10))
    {
        SendKeys.SendWait("{1}");
    }
}

private void btnACOnOff(object sender, EventArgs e)
{
    _stopClicker = !_stopClicker;
    if (_stopClicker)
    {
        if (_autoClicker == null)
            _autoClicker = new _autoClicker(new ThreadStart(ClickLoop));

        _autoClicker.Start();
    }
}

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
Wiw3K
Grandmaster Cheater
Reputation: 0

Joined: 09 Jul 2006
Posts: 762
Location: Poland.

PostPosted: Thu Feb 12, 2009 4:23 pm    Post subject: Reply with quote

@up
error CS0118: 'Pin_Type.Form1._autoClicker' is a 'field' but is used like a 'type'

Quote:
private void checkBox8_CheckedChanged(object sender, EventArgs e)
{
_stopClicker = !_stopClicker;
if (_stopClicker)
{
if (_autoClicker == null)
_autoClicker = new _autoClicker(new ThreadStart(ClickLoop));

_autoClicker.Start();
}
}



Shocked
Back to top
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Thu Feb 12, 2009 5:59 pm    Post subject: Reply with quote

Whoops, lol.


new Thread(), not new _autoClicker()

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
Wiw3K
Grandmaster Cheater
Reputation: 0

Joined: 09 Jul 2006
Posts: 762
Location: Poland.

PostPosted: Fri Feb 13, 2009 7:11 am    Post subject: Reply with quote

@up
Success but:
1.Its not even clicking.
2.If you untick it + tick again = http://i41.tinypic.com/dy4u2x.png
Shocked

Code:
        private bool _stopClicker = false;
        private Thread _autoClicker;

        private void ClickLoop()
        {
            for (; !_stopClicker; Thread.Sleep(10))
            {
                SendKeys.SendWait("{1}");
            }
        }


Code:
        private void checkBox8_CheckedChanged(object sender, EventArgs e)
        {
            _stopClicker = !_stopClicker;
            if (_stopClicker)
            {
                if (_autoClicker == null)
                    _autoClicker = new Thread(new ThreadStart(ClickLoop));

                _autoClicker.Start();
            }
        }
Back to top
View user's profile Send private message
killersamurai
Expert Cheater
Reputation: 0

Joined: 10 Sep 2007
Posts: 197
Location: Colorado

PostPosted: Fri Feb 13, 2009 2:01 pm    Post subject: Reply with quote

Wiw3K wrote:
@up
Success but:
1.Its not even clicking.
2.If you untick it + tick again = http://i41.tinypic.com/dy4u2x.png
Shocked

Code:
        private bool _stopClicker = false;
        private Thread _autoClicker;

        private void ClickLoop()
        {
            for (; !_stopClicker; Thread.Sleep(10))
            {
                SendKeys.SendWait("{1}");
            }
        }


Code:
        private void checkBox8_CheckedChanged(object sender, EventArgs e)
        {
            _stopClicker = !_stopClicker;
            if (_stopClicker)
            {
                if (_autoClicker == null)
                    _autoClicker = new Thread(new ThreadStart(ClickLoop));

                _autoClicker.Start();
            }
        }


1. What ever window you want to send keystrokes to has to have focus and can processes the message. That's assuming I remember how SendKeys work.
2. The thread didn't stop and you're trying to run it again.
Try this
Code:

private void ClickLoop()
        {
            while (!_stopClicker)
            {
                SendKeys.SendWait("{1}");
                Thread.Sleep(10);
            }
        }
private void checkBox8_CheckedChanged(object sender, EventArgs e)
        {
            _stopClicker = !_stopClicker;

            if (_stopClicker)
            {
                _autoClicker.Join(); // lets the thread finish what it is doing before exiting
            }
            else
            {
                _autoClicker = new Thread(ClickLoop);
                _autoClicker.Start();
               
                while (!_autoClicker.IsAlive);
            }
        }


Last edited by killersamurai on Fri Feb 13, 2009 2:39 pm; edited 1 time in total
Back to top
View user's profile Send private message
Wiw3K
Grandmaster Cheater
Reputation: 0

Joined: 09 Jul 2006
Posts: 762
Location: Poland.

PostPosted: Fri Feb 13, 2009 2:39 pm    Post subject: Reply with quote

@up
http://i40.tinypic.com/14ujoya.jpg
Back to top
View user's profile Send private message
killersamurai
Expert Cheater
Reputation: 0

Joined: 10 Sep 2007
Posts: 197
Location: Colorado

PostPosted: Fri Feb 13, 2009 3:09 pm    Post subject: Reply with quote

This code I actually tested, so it should work. You're using a check box, so it would be better to check that instead of doing things based on the boolean. The error is caused when the thread has already stopped and you're trying to close an already stopped thread. It's my fault for not providing a check on that.
Code:

 Boolean stopThread = true;
        System.Threading.Thread t;

        void SendKeyThread()
        {
            System.Threading.Thread.Sleep(5000); // 5 seconds to get to a window
            while (!stopThread)
            {
                SendKeys.SendWait("Hello World\n");
                System.Threading.Thread.Sleep(1000);
            }
        }
        private void chkStart_CheckedChanged(object sender, EventArgs e)
        {
            if (!chkStart.Checked)
            {
                stopThread = true;
               
                if (t.ThreadState != System.Threading.ThreadState.Stopped)
                    t.Join();
            }
            else
            {
                stopThread = false;
                t = new System.Threading.Thread(SendKeyThread);
                t.Start();

                while (!t.IsAlive) ;
            }
        }
Back to top
View user's profile Send private message
Wiw3K
Grandmaster Cheater
Reputation: 0

Joined: 09 Jul 2006
Posts: 762
Location: Poland.

PostPosted: Fri Feb 13, 2009 3:19 pm    Post subject: Reply with quote

@up
good , no errors and no crash form but does nothing Shocked Shocked
Back to top
View user's profile Send private message
NINTENDO
Grandmaster Cheater Supreme
Reputation: 0

Joined: 02 Nov 2007
Posts: 1371

PostPosted: Mon Feb 16, 2009 3:38 pm    Post subject: Reply with quote

I'm just curious. Why do you create a new instance of background worker inside the background worker? Dosen't that just freeze your computer? Or will it only run the method once?
_________________
Intel over amd yes.
Back to top
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites