等到用户在另一个表单的文本框中输入并返回值

时间:2011-03-01 15:16:39

标签: c# multithreading

我是C#的新手,我正试着这样做:

  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();

现在我有:

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

现在我正在寻找点1,3,6的想法你能建议我如何解决这个问题吗?

  1. 如何启动50个帖子
  2. 当用户按下
  3. 时,如何从另一种形式的文本框中获取值

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

要启动一些线程,可以使用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
    }

在第二种形式中,您可以定义此处理程序:

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

然后以第一种形式:

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