gpt4 book ai didi

c# - 经过漫长的过程后显示对话框

转载 作者:太空宇宙 更新时间:2023-11-03 10:59:29 25 4
gpt4 key购买 nike

在下面的代码中,我有一个名为 GetExcelData 的长时间运行的进程。完成后,我想显示一个对话框以将其内容保存到 TXT 文件中。

问题是,在调试时,出现以下错误:

Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.

这是我的代码。错误发生在读取 saveFileDialog1.ShowDialog();

的行上
FileInfo existingFile = new FileInfo("C:\\MyExcelFile.xlsx");

ConsoleApplication2.Program.ExcelData data = ConsoleApplication2.Program.GetExcelData(existingFile, _worker);

var json = new JavaScriptSerializer().Serialize(data);

SaveFileDialog saveFileDialog1 = new SaveFileDialog();

saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.ShowDialog();

if (saveFileDialog1.FileName != "")
{
File.WriteAllText(saveFileDialog1.FileName, json);
}

我已经尝试将 [STAThread] 属性添加到我从中调用它的方法,但它似乎没有用。

请让我提供更多代码,以便更清楚地说明我正在尝试做什么:

引用我的控制台项目的 WPF 项目中存在以下内容:

private BackgroundWorker _backgroundWorker = new BackgroundWorker();

public MainWindow()
{
InitializeComponent();

// Set up the BackgroundWorker.
this._backgroundWorker.WorkerReportsProgress = true;
this._backgroundWorker.WorkerSupportsCancellation = true;
this._backgroundWorker.DoWork += new DoWorkEventHandler(bw_DoWork);
this._backgroundWorker.ProgressChanged +=
new ProgressChangedEventHandler(bw_ProgressChanged);
}

private void Button_Click(object sender, RoutedEventArgs e)
{
if (this._backgroundWorker.IsBusy == false)
{
this._backgroundWorker.RunWorkerAsync();
}
e.Handled = true;
}

void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// Set the Value porperty when porgress changed.
this.progressBar1.Value = (double)e.ProgressPercentage;
}

void bw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker _worker = sender as BackgroundWorker;
if (_worker != null)
{
FileInfo existingFile = new FileInfo("C:\\MyExcelFile.xlsx");

ConsoleApplication2.Program.ExcelData data = ConsoleApplication2.Program.GetExcelData(existingFile, _worker);

var json = new JavaScriptSerializer().Serialize(data);

SaveFileDialog saveFileDialog1 = new SaveFileDialog();

saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.ShowDialog();

if (saveFileDialog1.FileName != "")
{
File.WriteAllText(saveFileDialog1.FileName, json);
}
}
}

最佳答案

将与 UI 交互的代码移至处理 UI 元素的同一线程。最简单的方法是通过 RunWorkerCompleted 事件

  this._backgroundWorker.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(bw_WorkComplete);

....

void bw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker _worker = sender as BackgroundWorker;
if (_worker != null)
{
FileInfo existingFile = new FileInfo("C:\\MyExcelFile.xlsx");
ConsoleApplication2.Program.ExcelData data = ConsoleApplication2.Program.GetExcelData(existingFile, _worker);

e.Result = new JavaScriptSerializer().Serialize(data);
}
}

private void bw_WorkComplete(object sender, RunWorkerCompletedEventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.ShowDialog();

if (saveFileDialog1.FileName != "")
{
string json = e.Result.ToString();
File.WriteAllText(saveFileDialog1.FileName, json);
}
}

在 DoWork 方法中,将 json 字符串保存在 DoWorkEventArgs 类的 e.Result 属性中,并从同名的 RunWorkerCOmpletedEventArgs 属性的 RunWorkerCompleted 事件中检索它。

关于c# - 经过漫长的过程后显示对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18212350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com