简体   繁体   中英

Wait until user press enter in textbox in another form and return value

I am new to C# and I'm trying to do sth like this:

  myList = list of 1000+ string values;
  1.StartNewThreads(50); //50 is the numbers of new threads
  2.DoSth1(next value from myList);
  3.DoSth2();
  4. var value = {
     ShowNewImageForm(); //show only if not another ImageForm is displayed if another is show - wait
     WaitUntilUserPressEnterInTextBox();
     ReturnValueFormTextbox();
  }
  5.DoSth3();
  6.StartNewThread();

For now I have:

foreach(String s in myList ) {
 DoSth1(s);  
 DoSth2();     
 DoSth3();    
}

And now I'm looking for ideas to points 1,3,6 Can You suggest me how to resolve this?

  1. How to start 50 threads
  2. How to get value from textbox in another form when user press enter

对于第二个问题:只需在子表单的构造函数中传递父表单/所有者表单,然后使用该引用来传递/设置返回值,或者(更优雅)定义一个事件,一旦用户按下return就会在子表单上触发键,然后在您的父表单中添加事件处理程序。

For start some Threads, you can use a Thread[]

    public static void StartThreads(int n)
    {
        System.Threading.Thread[] threads = new System.Threading.Thread[n];
        for (int i = 0; i < threads.Length; i++)
        {
            threads[i] = new System.Threading.Thread(new System.Threading.ThreadStart(DoThread));
        }
    }

    public static void DoThread()
    {
        //do some here
    }

In the second form you can define this handler:

    public static void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            TextBox myText = (TextBox)sender;
            //your code here...
        }
    }

Then in the first form:

textBox1.KeyDown += new KeyEventHandler(Form2.textBox1_KeyDown);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM