我遇到线程问题。我的代码:从另一个线程关闭对话框
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);
}
});
现在在解析我:
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个窗体并单击取消(在主窗体上)时,此对话窗体需要手动关闭。点击取消后关闭此表单是否可行?
这是一个Windows窗体或WPF,什么是“问题”异常? – CodingGorilla 2011-03-29 14:05:32
Windows窗体。问题是我不知道如何关闭打开的对话框automaticaly – JanZiolko 2011-03-29 15:10:37
我不确定我是否理解,只需调用Close方法即可。你在问什么? – CodingGorilla 2011-03-29 15:11:51