| View previous topic :: View next topic |
| Author |
Message |
Wiw3K Grandmaster Cheater
Reputation: 0
Joined: 09 Jul 2006 Posts: 762 Location: Poland.
|
Posted: Wed Feb 11, 2009 3:22 pm Post subject: [C#]Background Worker is busy or it freezes my app. |
|
|
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 |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Wed Feb 11, 2009 4:56 pm Post subject: |
|
|
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(); }));
|
_________________
|
|
| Back to top |
|
 |
Wiw3K Grandmaster Cheater
Reputation: 0
Joined: 09 Jul 2006 Posts: 762 Location: Poland.
|
Posted: Wed Feb 11, 2009 6:11 pm Post subject: |
|
|
@up
i am trying to make AutoClicker but fail :O cuz app freeze
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Thu Feb 12, 2009 3:51 pm Post subject: |
|
|
| 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();
}
}
|
_________________
|
|
| Back to top |
|
 |
Wiw3K Grandmaster Cheater
Reputation: 0
Joined: 09 Jul 2006 Posts: 762 Location: Poland.
|
Posted: Thu Feb 12, 2009 4:23 pm Post subject: |
|
|
@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();
}
} |
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Thu Feb 12, 2009 5:59 pm Post subject: |
|
|
Whoops, lol.
new Thread(), not new _autoClicker()
_________________
|
|
| Back to top |
|
 |
Wiw3K Grandmaster Cheater
Reputation: 0
Joined: 09 Jul 2006 Posts: 762 Location: Poland.
|
Posted: Fri Feb 13, 2009 7:11 am Post subject: |
|
|
@up
Success but:
1.Its not even clicking.
2.If you untick it + tick again = http://i41.tinypic.com/dy4u2x.png
| 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 |
|
 |
killersamurai Expert Cheater
Reputation: 0
Joined: 10 Sep 2007 Posts: 197 Location: Colorado
|
Posted: Fri Feb 13, 2009 2:01 pm Post subject: |
|
|
| Wiw3K wrote: | @up
Success but:
1.Its not even clicking.
2.If you untick it + tick again = http://i41.tinypic.com/dy4u2x.png
| 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 |
|
 |
Wiw3K Grandmaster Cheater
Reputation: 0
Joined: 09 Jul 2006 Posts: 762 Location: Poland.
|
|
| Back to top |
|
 |
killersamurai Expert Cheater
Reputation: 0
Joined: 10 Sep 2007 Posts: 197 Location: Colorado
|
Posted: Fri Feb 13, 2009 3:09 pm Post subject: |
|
|
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 |
|
 |
Wiw3K Grandmaster Cheater
Reputation: 0
Joined: 09 Jul 2006 Posts: 762 Location: Poland.
|
Posted: Fri Feb 13, 2009 3:19 pm Post subject: |
|
|
@up
good , no errors and no crash form but does nothing
|
|
| Back to top |
|
 |
NINTENDO Grandmaster Cheater Supreme
Reputation: 0
Joined: 02 Nov 2007 Posts: 1371
|
Posted: Mon Feb 16, 2009 3:38 pm Post subject: |
|
|
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 |
|
 |
|