0

does anyone have a notion how not to use a Thread.Sleep? I know that the question is silly but, I have more than 3 days trying how not to use it, to be able to control a for.

I explain a little what I'm trying to do, I have a project which goes through "N" number of points given by the user along with an interval also given by the user, what the interval does is put a waiting time between point to point.

The point is that if I use Thread.Sleep when activating another timer next to the one that does the above, it also stays with that timeout (which it shouldn't be).

Base code annex (test project).

Timer 1: Timer 1 (list of coordinates/points given by the user)

List<Point> points = new List<Point>();
        List<int> times = new List<int>();

        foreach (ListViewItem item in PositionsListView.Items)
        {
            int x = Convert.ToInt32(item.Text); 
            int y = Convert.ToInt32(item.SubItems[1].Text); 
            times.Add(Convert.ToInt32(item.SubItems[2].Text));

            points.Add(new Point(x, y));
        }
        try
        {

            for (int j = 0; j <= points.Count - 1; j++)
            {
                string value = Convert.ToString(points[j].Y * 65536 + points[j].X);
                SendMessageA(Convert.ToInt32(this.Handle1), 513L, Convert.ToInt64(value), 8L);
                SendMessageA(Convert.ToInt32(this.Handle1), 514L, Convert.ToInt64(value), 8L);

                Thread.Sleep(times[j]); // Time interval between each point
            }
        }
        catch (Exception exc)
        {
            MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

Timer 2: The one that controls everything (and triggers timer 3 that executes a click event)

if (GetAsyncKeyState(Keys.F1) != 0)
        {
            timer3.Start();
            timer1.Start();
        }
        if (GetAsyncKeyState(Keys.F2) != 0)
        {
            timer1.Stop();
            timer3.Stop();
        }

        if (GetAsyncKeyState(Keys.F3) != 0)
        {
            CapturarVentanaDeClick();
        }

        timer3.Interval = Convert.ToInt32(numericUpDown1.Value);
        timer1.Interval = Interval;

Could someone guide me? I'm really kind of lost.

First of all, Thanks.

Aurelio
  • 13
  • 6
  • I have no clue what this code is doing. I also don't understand "when activating another timer next to the one that does the above" means. Your question rather unclear. I suggest you read [ask] and try to fix it. – Enigmativity May 27 '22 at 07:28
  • In any case, if your code is running on the UI thread then `Sleep` will lock up the UI. If it is not then accessing `PositionsListView.Items` isn't safe to be done in any thread other than the UI. You need to clarify what you're doing. – Enigmativity May 27 '22 at 07:30
  • 2
    The Windows.Forms.Timer runs on the UI thread. When you sleep in the UI thread, it blocks. No other timer will run, no mouse clicks will be processed, no window messages will be processed. – Thomas Weller May 27 '22 at 07:30
  • Why do you use SendMessage to move the mouse and click instead of just [moving the mouse](https://stackoverflow.com/questions/8050825/how-to-move-mouse-cursor-using-c)? That's very hard to understand for a C# developer. – Thomas Weller May 27 '22 at 07:34
  • 1
    So, you might want to use a [System.Threading.Timer](https://learn.microsoft.com/en-us/dotnet/api/system.threading.timer?view=net-6.0) or maybe don't use a timer at all but use a thread instead. Your code can then sleep there. – Thomas Weller May 27 '22 at 07:36
  • @ThomasWeller - The OP needs to then remove the UI access if the code is pushed to another thread. – Enigmativity May 27 '22 at 07:39
  • @Enigmativity: yes, it will require some Invoke() stuff. If OP builds an auto clicker, understanding that is essential, IMHO. – Thomas Weller May 27 '22 at 07:42
  • You are mixing two completely different types of timers: thread timers and UI timers. The latter need a UI thread with a message pump. If you sleep using a thread timer then the message pump stops. Don't mix these two. If you need to keep the UI alive then only use a UI timer – Charlieface May 27 '22 at 08:55
  • @Charlieface It's hard to explain what I'm trying to do. Basically the Thread.Sleep is handling the wait time between each point (coordinate) selected by the user. But, by using that it stays "fixed" also to make use of a timer which handles a SendMessage event (click on specific process). Which is causing me conflict, since that timer is controlled by a numericupdown. – Aurelio May 28 '22 at 19:35
  • In conclusion, this piece of code is what is causing me conflict `hread.Sleep(Times[j]) // Wait time between one point(coordinate) to another which I need to be handled with a numericupdown and the wait time is based on the timer` – Aurelio May 28 '22 at 19:45
  • @Aurelio - What do you mean by a piece of code that is causing you conflict? Conflict is two opposing ideas or forces that are colliding. – Enigmativity May 29 '22 at 01:53
  • You also should rip out all of the `catch (Exception exc)` code that just raises a `MessageBox.Show` just let it bubble to the top and let the dev environment handle the exception or just make one unhandled exceptions handler for your app. It'll make you a better coder if you learn to write exception free code. – Enigmativity May 29 '22 at 01:57
  • @Enigmativity: having debugged a lot of 3rd party production code, I really dislike unhandled exception handlers. Because they don't work in hard cases like StackOverflow and OOM, and the debugger can no longer distinguish between first chance and second chance exceptions. – Thomas Weller May 30 '22 at 09:14
  • @ThomasWeller - Do read https://ericlippert.com/2008/09/10/vexing-exceptions/ – Enigmativity May 30 '22 at 11:11
  • @Enigmativity in that article, I only see arguments contra an unhandled exception handler. Fatal exception: you can't handle them. Boneheaded exception: you shouldn't handle them. Vexing exception: handle them (but not in an unhandled exception handler, because that one is not specific.) – Thomas Weller Jun 01 '22 at 13:25
  • @ThomasWeller - The article says what to handle. If you only handle those then everything else bubbles up. – Enigmativity Jun 01 '22 at 23:50

0 Answers0