Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7922669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:02:48+00:00 2026-06-03T17:02:48+00:00

This is a slightly weird setup, and I’m planning on redoing it, but, if

  • 0

This is a slightly weird setup, and I’m planning on redoing it, but, if nothing else, I’m hoping that I can learn a lot more about threading as a result of this.

At the moment, I’ve got an application I’m building which uses a TabControl Windows Form as the basis for its design. I’ve got a number of different things I need to do, so I figured this might be the best way to do it.

Now, I’m coding in a Macro function into the program, such that a user can record and playback mouse and keyboard actions. That has been perfectly fine, and mixing it with System.Timers.Timer has been ok as well. However, the DLL I’m using uses Thread.Sleep(x) to pause the thread in between keystroke and mouse executions.

At this point, the button is on a different tabpage and switches to a different page. However, the point blank setup resulted in the program hanging while waiting for the macro execution to complete.

To explain a little better:

  • User clicks button in main tabcontrol.
  • Tabcontrol.SelectedIndex is switched to the proper page, but does not refresh page (I tried calling this.Refresh(), it didn’t work)
  • Macro executes
  • Tabcontrol finishes loading the page, but by this point, the necessary macro has already executed and the loaded page is superfluous.

To get around this, I started researching threading and seeing how I could use that. I came up with the following code inside a class:

Class X{
  Thread othread;
  ...        
  public void Play()
  {
    if ((othread != null) && !(othread.IsAlive))
    {
        othread = new Thread(playValues);
        GC.Collect();
    }
    else if (othread == null) othread = new Thread(playValues);
    else return;
    othread.Start();
  }

  public void PlayValues(){
  ...
  Thread.CurrentThread.Abort();
  }

}

The reason why the thread is destroyed is because, every time the user clicks “Play” in the main form, X.Play() is called, so I need to either reuse the thread (I researched Monitor.Pulse(), but that gave me asynchronous call errors), or destroy it permanently each time it completes.

Now, at the moment, the code works fine. However, the fact that I’m consistently creating and destroying threads every time I have to play a macro is slightly concerning, and the ridiculous amount of GC calls is also worrying me.

Can I get some pointers on threading, and how I might be able to do this better, without this setup? As a point of fact, the secondary tab is a webpage, and the macros are executed on the webpage’s form.

  • Edit: I don’t know if I didn’t make this clear, but the macro is using a mouse and keyboard simulator. The other tab is a webbrowser setup, and it nests an applet inside, so I’m emulating the hardware functions in order to perform automated setups. This helps because I don’t need to lock anything (all mouse and keyboard calls are operated by the simulators, and have no bearing on values controlled by the tertiary tab), I just need to be able to a) have the thread perform the same function whenever a user presses a button, and b) remove some of the excess materials.

I was also informed that ‘Thread.CurrentThread.Abort’ is threadsafe and therefore a fine way to end a thread if you wanted to end it forever, but if I can have the thread repeat its functions instead of killing it, that’d be wonderful…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T17:02:56+00:00Added an answer on June 3, 2026 at 5:02 pm

    To reuse the play thread, you could use an AutoResetEvent that you Set() from the main thread. The same goes for when the thread is idle – the main thread prepares the data required to play, then Set()s a different AutoResetEvent, causing the play thread to start.

    So the startPlayingAutoResetEvent in below example causes the thread to wait until the main thread tells it to “go”, while the stopPlayingAutoResetEvent causes the play thread to respond to the main thread telling it to “stop” whenever it is not performing a step.

    Main thread:

    // prepare data to play
    startPlayingAutoResetEvent.Set();
    // ...
    stopPlayingAutoResetEvent.Set();
    

    Play thread:

    while (true)
    {
        // wait for the main thread to signal "start"
        startPlayingAutoResetEvent.WaitOne();
        // ensure the stopPlaying is in an unsignalled state
        stopPlayingAutoResetEvent.Reset();
    
        // gather the data the main thread has prepared
    
        while (stepsToPlay)
        {
            // wait until main thread signals to stop OR timeout has elapsed
            if (stopPlayingAutoResetEvent.WaitOne(timeout))
            {
                // abort current run and put thread in idle mode, waiting for new commands
                break;
            }
            else
            {
                // play next step
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question might be slightly subjective, but I am unsure where else it would
Using a string.CompareTo(string) i can get around this slightly but is not easy to
OK, this is a slightly weird question. We have a touch-screen application (i.e., no
This is slightly off topic of programming but still has to do with my
I apologize if this is slightly off-topic. I'm hoping to find a software-as-a-service CRM
This is a slightly tricky question. I am using NSDateFormatter on the iPhone but
Apologies if this is a slightly noob question, but looking to clarify my thoughts
This is a sample (edited slightly, but you get the idea) of my XML
This answer says that Linq is targeted at a slightly different group of developers
I've already asked a similar question but this is slightly different so i Thought

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.