从另一个线程关闭对话框

发布于 2024-10-27 02:38:30 字数 1289 浏览 1 评论 0原文

我遇到了线程问题。我的代码:

Task.Factory.StartNew(() =>
{
    cts  = new CancellationTokenSource();
    var lines = File.ReadLines(Path.Combine(Environment.CurrentDirectory, "urls.txt"));

    try
    {
        var q = from line in lines.AsParallel().WithDegreeOfParallelism(30).WithCancellation(cts.Token)
        let result = Parse(line, cts.Token)
            select new
            {
                res = result
            };

        foreach (var x in q)
        {
            if (x != null)
            {
                Console.WriteLine("{0}", x.res);
            }
        }

    }
    catch (OperationCanceledException ex)
    {
        Console.WriteLine(ex.Message);
    }
});

现在在 Parse 中我有:

public String Parse(String url,CancellationToken ct)
{
    ct.ThrowIfCancellationRequested();
    /* many lines of code */
    InputForm iForm = new InputForm();
    iForm.setPageData(pageData);

    if (iForm.ShowDialog() == DialogResult.OK)
    {
        string userInput = iForm.textBox.Text;
        /* code block */
        return result;
    } else {
        return Parse(newUrl,ct);
    }
}

我正在使用 ShowDialog 因为我需要从 iForm 获取用户输入(此表单有一个计时器,并在 60 秒后自动关闭)。现在,当我打开大约 30 个表单并单击取消(在主表单上)时,需要手动关闭此对话框表单。单击“取消”后是否可以关闭此表单?

I'm having a problem with threads. My code:

Task.Factory.StartNew(() =>
{
    cts  = new CancellationTokenSource();
    var lines = File.ReadLines(Path.Combine(Environment.CurrentDirectory, "urls.txt"));

    try
    {
        var q = from line in lines.AsParallel().WithDegreeOfParallelism(30).WithCancellation(cts.Token)
        let result = Parse(line, cts.Token)
            select new
            {
                res = result
            };

        foreach (var x in q)
        {
            if (x != null)
            {
                Console.WriteLine("{0}", x.res);
            }
        }

    }
    catch (OperationCanceledException ex)
    {
        Console.WriteLine(ex.Message);
    }
});

Now in Parse I have:

public String Parse(String url,CancellationToken ct)
{
    ct.ThrowIfCancellationRequested();
    /* many lines of code */
    InputForm iForm = new InputForm();
    iForm.setPageData(pageData);

    if (iForm.ShowDialog() == DialogResult.OK)
    {
        string userInput = iForm.textBox.Text;
        /* code block */
        return result;
    } else {
        return Parse(newUrl,ct);
    }
}

I'm using ShowDialog because I need to get user input from iForm (this form has a timer and is auto closed after 60 seconds). Now, when I opened about 30 forms and click Cancel (on main form) this dialog forms need to be closed manualy. Is it posible to close this form after clicking Cancel?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗳卜坏 2024-11-03 02:38:30

我经常这样做。

您需要做的是

  • 创建一种与主执行线程(MTOE)通信的方法,
  • 调用主线程并等待主线程中的响应
  • ,显示对话框,
  • 的返回值
  • 设置线程信号 您的线程已完成

自定义事件处理程序非常适合将消息从您的线程返回到 MTOE。

ManualResetEvent 有助于您的线程了解 MTOE 何时完成。

类实例可以传递到事件处理程序中,MTOE 使用该事件处理程序来填充一些数据项,并在完成时传回线程。

通常,当我创建特殊类时,它包含事件处理程序和 ManualResetEvent 对象。

在 MTOE 中,如果关闭表单,则可以向所有等待对话框发出取消信号。

这需要一些重新设计,但我认为它会给你你想要的东西。

I do this a lot.

What you're going to need to do is

  • create a way to communicate with your Main Thread Of Execution (MTOE)
  • call your main thread and wait for the response
  • in your main thread, display your dialog box
  • set the return value for your thread
  • signal your thread that you are done

A custom event handler works great for getting a message from your thread back to the MTOE.

A ManualResetEvent is good for your thread to know when the MTOE is complete.

A class instance can be passed in an event handler that the MTOE uses to fill a few data items and pass back to the thread whenever it is done.

Typically, when I create my special class, it contains the event handler and the ManualResetEvent object.

From your MTOE, if you close your form, you can signal all of your waiting dialog boxes to Cancel.

This would require a little redesign, but I think it would give you what you are after.

枯叶蝶 2024-11-03 02:38:30

您可能需要查看 http://msdn .microsoft.com/en-us/library/system.windows.forms.application.openforms.aspx

您可以迭代打开的打开表单并对那些类型为InputForm的表单调用关闭

编辑:

以下评论是正确的,这会引发异常。您实际上需要类似 FormToClose.BeginInvoke(delegate ()=> FormToClose.Close()); 的东西

You may want to look at http://msdn.microsoft.com/en-us/library/system.windows.forms.application.openforms.aspx

You could iterate over the open open forms and call close on those that are of type InputForm

EDIT:

The below comment is correct this would throw an exception. You would actually need something like FormToClose.BeginInvoke(delegate ()=> FormToClose.Close());

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文