我有一个Winforms应用程序,通过它的API调用CorelDraw,然后在保存之前对图像进行一些处理。我尝试使用Threads,BackgroundWorker和继承BackgroundWorker但允许Thread.Abort的类来设置它。我完成这项工作的每一种方式,我都会遇到两个问题之一。线程不在后台运行,在这种情况下我无法按下表单上的任何按钮,因为它被冻结或线程在后台运行但在我第一次调用CorelDraw时立即停止处理任何内容。
有没有替代解决方案?我们处理这个问题的旧方法是让一个观察者WinForm产生另一个Process()
,它将在CorelDraw中完成所有工作。使用Process()调用的进程将重要信息保存到.txt文件中以供父进程检查,如果子进程在同一图像上工作的时间过长,则父进程会调用Kill()
。到目前为止,这是一种笨重且容易出错的方式,这就是为什么我在寻找更好的解决方案。
答案 0 :(得分:1)
设计师代码:
makefile();
这是表格代码:
namespace Tasks
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(41, 43);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(131, 43);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(240, 116);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
}
如果您创建异步任务,则可以在后台运行Corel应用程序,然后在关闭时将完成任务。我在那里添加了额外的东西,如Progress和cts用于取消父窗口关闭时的任务。