Running a windows form in a separate thread

前端 未结 4 1413
孤独总比滥情好
孤独总比滥情好 2021-01-15 02:10

I am dealing with running a control in a form, however the form itself is of no value to me. I essentially want the form to run a task and return a value, however for that I

4条回答
  •  深忆病人
    2021-01-15 02:41

    I think the simplest solution is to just raise an event from the form once the task has completed.

    void RunTask()
    {
        Form form = new Form();
        form.TaskCompleted += new EventHandler(form_TaskCompleted);
        form.Show();
    }
    
    void form_TaskCompleted(object sender, EventArgs e)
    {
        object result = ((Form)sender).GetResult();
    }
    

    Edit: Of course you'd want to dispose the form and unhook that event once it's completed etc..

提交回复
热议问题