从另一个线程关闭对话框
我遇到了线程问题。我的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我经常这样做。
您需要做的是
自定义事件处理程序非常适合将消息从您的线程返回到 MTOE。
ManualResetEvent
有助于您的线程了解 MTOE 何时完成。类实例可以传递到事件处理程序中,MTOE 使用该事件处理程序来填充一些数据项,并在完成时传回线程。
通常,当我创建特殊类时,它包含事件处理程序和
ManualResetEvent
对象。在 MTOE 中,如果关闭表单,则可以向所有等待对话框发出取消信号。
这需要一些重新设计,但我认为它会给你你想要的东西。
I do this a lot.
What you're going to need to do is
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.
您可能需要查看 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());