这个问题与c#有关。方案是当我点击按钮时,文件读取,数据操作和文件转储等操作将会发生。每次操作完成后,我将更新状态(即,文件读取完成,数据操作完成)在UI中的标签(FORM-frmTesting)
按钮点击事件是
namespace frmTesting
{
public partial class Form1 : Form
{
private void button1_Click_1(object sender, EventArgs e)
{
class1 l_objClass1 = new class1();
l_objClass1.DoOperation();
}
}
public class class1
{
public int DoOperation()
{
ReadTextFile();
ParsingData();
SaveTextFile();
return 0;
}
private int ReadTextFile()
{
//Read the text File
return 0;
}
private int ParsingData()
{
// Data manipulation
return 0;
}
private int SaveTextFile()
{
// save the file
return 0;
}
}
}
是否可以使用代理人来完成?
答案 0 :(得分:1)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace WindowsFormsApplication3
{
public delegate void MyDelagate();
class Class1
{
public event MyDelagate _myDelegate;
private String s1 = String.Empty;
public String s
{
get
{
return s1;
}
set
{
s1 = value;
if(_myDelegate != null)
_myDelegate();
}
}
public int DoOperation()
{
s = "Started";
ReadTextFile();
ParsingData();
SaveTextFile();
s = "Completed";
return 0;
}
private int ReadTextFile()
{
s = "Read Text File";
Thread.Sleep(3000);
return 0;
}
private int ParsingData()
{
s = "Parsing Data";
Thread.Sleep(3000);
return 0;
}
private int SaveTextFile()
{
s = "Save Text File";
Thread.Sleep(3000);
return 0;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
Class1 x = new Class1();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
x._myDelegate += new MyDelagate(UpdateStatus);
x.DoOperation();
}
void UpdateStatus()
{
label1.Text = x.s;
Validate();
}
}
}
答案 1 :(得分:0)
不确定。
您可以在Class1上设置您的委托属性。
创建Class1之后(或者可以在构造函数中执行此操作),将函数分配给delegate属性。
当您的操作发生/完成时,他们会检查委托是否为空,然后使用他们想要的任何事件参数执行委托(状态级别,完成状态,那种事情)。
然后,传递给委托的Form1函数将处理参数的处理并为文本字段赋值。