我正在用C#为Windows应用程序编写代码,我有2个类,其中1个是GUI类,其中声明了ShowLog()方法的事件和委托。下一个类类似于后端类,其中我拥有全部从GUI类调用的方法定义。我的问题是我想使用委托和事件从后端类访问一些GUI类方法。那么该怎么做。
namespace Linux_ScriptRunner
{
public delegate void MyDel(object str);
public partial class LinuxScriptRunner : Form
{
public event MyDel MyEvent;
public void pqr(object str)
{
MyEvent(str);
}
private void btnSendCommand_Click(object sender, EventArgs e)
{
lstcmd =txtBxCmdLine.Text;
if (!lstcmd_list.Contains(lstcmd))
lstcmd_list.Add(txtBxCmdLine.Text);
else
{
lstcmd_list.Remove(lstcmd);
lstcmd_list.Add(lstcmd);
}
count++;
lstcmdbtn.Enabled = true;
try
{
isButtonEnable = false;
EnableDishableButton();
string serverIPS = string.Empty;
ListView.SelectedIndexCollection indexes= this.lstVwServerList.SelectedIndices;
foreach (int index in indexes)
serverIPS += "||" + lstVwServerList.Items[index].Text;
if (serverIPS != "")
{
System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(myButtonExcutionForCommand));
th.IsBackground = true;
th.Start(txtBxCmdLine.Text.Trim() + serverIPS);
lstLastAct.Items.Add(" => " + txtBxCmdLine.Text + " on " + serverIPS);
txtBxCmdLine.Text = String.Empty;
this.lstVwServerList.SelectedItems.Clear();
// txtBxCmdLine.Text = String.Empty;
// this.lstVwServerList.SelectedItems.Clear();
}
else
{
MessageBox.Show("Select Ip for IP List \n ");
}
}
catch (Exception ex)
{
Program.WriteExceptionLog(ex);
}
}
public void ShowLog(object s1)
{
string status = ((string)s1);
try
{
if (richtxtBxResult.InvokeRequired)
{
this.Invoke(new Action<string>(ShowLog), status);
}
else
{
richtxtBxResult.Text += Environment.NewLine + status;
richtxtBxResult.SelectionStart = richtxtBxResult.Text.Length;
richtxtBxResult.ScrollToCaret();
richtxtBxResult.Refresh();
}
}
catch (Exception ex)
{
Program.WriteExceptionLog(ex);
}
}
private void myButtonExcutionForCommand(object commandAndServerIPS)
{
try
{
string[] sep = {"||"};
string[] temp = ((string)commandAndServerIPS).Split(sep, StringSplitOptions.None);
int i = 1;
while (i < temp.Count())
{
ShowLog(temp[0] + " is executing for " + temp[i]);
toolStripStatusLabel1.Text = "Command Executing ... ";
script_runner.ExcuteCommand(temp[i], temp[0]," ");
//ShowLog("");
toolStripStatusLabel1.Text = " command executed Successfully... ";
i++;
// ShowLog(temp[i]);
ShowLog("\n\n -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- \n\n");
}
}
catch (Exception ex)
{
MessageBox.Show("Fill Column Name !!!");
//richtxtBxResult.Text = "Im here exception_mybuttonec";
ShowLog(ex.Message);
Program.WriteExceptionLog(ex);
}
isButtonEnable = true;
EnableDishableButton();
}
}
}
}
2nd file class.......
namespace LinuxScriptRunner
{
class scriptRunner
{
public string ExcuteCommand(String serIp, string cmd,string serDet)
{
int port;
string un=String.Empty;
string pwd=string.Empty;
string os=String.Empty;
LinuxScriptRunner lsr = new LinuxScriptRunner();
lsr.MyEvent += new MyDel(lsr.ShowLog);
System.Threading.Thread th1 = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(lsr.pqr));
th1.IsBackground = true;
th1.Start("hi");
}
}
在代码中,LinuxSriptRunner类是我的GUI类,具有事件和委托,我想使用GUI类event从类b访问事件。 希望你明白我的观点
答案 0 :(得分:0)
我理解您提出问题的背景如下:后端类发布一个日志记录事件,在其中报告其进度或其他信息,而不必知道其显示方式。 GUI类订阅此事件,并执行适当的操作。像这样(只是草图!):
public delegate void LoggingDelegate(string str);
public partial class Gui : Form
{
public void ShowLog(string status)
{
try
{
if (richtxtBxResult.InvokeRequired)
{
this.Invoke(new Action<string>(ShowLog), status);
}
else
{
richtxtBxResult.Text += Environment.NewLine + status;
richtxtBxResult.SelectionStart = richtxtBxResult.Text.Length;
richtxtBxResult.ScrollToCaret();
richtxtBxResult.Refresh();
}
}
catch (Exception ex)
{
Program.WriteExceptionLog(ex);
}
}
public void RunBackend()
{
var backend = new BackendClass();
backend.LoggingEvent += ShowLog;
Task.Run(() => backend.SomeAction());
}
}
public class BackendClass
{
public event LoggingDelegate LoggingEvent;
public void SomeAction()
{
// ...
LoggingEvent?.Invoke("Log output");
// ...
}
}