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 8965461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:48:29+00:00 2026-06-15T16:48:29+00:00

when form is loaded myMethod() is called and then it executes some statements. at

  • 0

when form is loaded myMethod() is called and then it executes some statements. at some point when it gets to if statement it should stop and wait for user to press some button and then it will continue executing according to what user did. i used Thread.Sleep() and ManualResetEvent obj’s WaitOne() method but they seem to freeze the whole process nothing can be done until the time expires. I think that KeyEventHandler in should be run in background so it never feezzed… Anyway how can I do that ?

public partial class Form1 : Form
{
    bool pressed = false;
    public Form1()
    {
        InitializeComponent();
        this.KeyPreview = true;
        this.KeyDown +=new KeyEventHandler(Form1_KeyDown);
    }

    void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.E)
        {
            pressed = true;
        }
    }

    void myMethod()
    { 
        while (someBool)
        {
            //do something
            if (pressed)
            //do this
            else 
            //do that
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        myMethod();
    }
}
  • 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-15T16:48:30+00:00Added an answer on June 15, 2026 at 4:48 pm

    Try starting your method as a Task:

    private void Form1_Load(object sender, EventArgs e)
    {
        System.Threading.Tasks.Task t = new Task(() => myMethod());
        t.Start();
    }
    

    When you test for pressed in your method, set it to false:

    void myMethod()
    { 
        while (someBool)
        {
            //do something
            if (pressed)
            {
                pressed = false;
                //do this
            }
            else 
            //do that
        }
    }
    

    Of course, this can potentially cause a variety of multithreading errors, depending on what you actually do in myMethod(). I suggest that you look into thread synchronization before you start working with multiple threads.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

User registration in my application is performed in steps. After submitting the form, some
I have a form which is loaded from ajax response that contains some textarea
I am using jquery validate plugin to validate dynamically loaded form. For some cases
I have a custom control that gets focus when the form is loaded. It
I m developing a coverflow with android-coverflow http://code.google.com/p/android-coverflow/ Items are loaded form a WebService,
The ActiveScaffold list view has a search form that is loaded via ajax when
Hello i have a list view control, While the form is being loaded i
Is there a way to check if the form that has been loaded with
Im using c# .net windows form application. I have loaded names of all the
I have a tab form called Home and I would like to say for

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.