c# - 方法没有被调用
问题描述
我正在用 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");
}
}
在代码中,作为我的 GUI 类的 LinuxSriptRunner 类具有事件和委托,我想使用 GUI 类事件从类 b 访问事件。我希望你能得到我的分数
解决方案
我理解你的问题的背景如下:后端类发布一个日志事件,它报告它的进度或其他信息,而不必知道它是如何显示的。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");
// ...
}
}
推荐阅读
- symfony - laravel 和 symfony 的比较
- grafana - 在grafan中将图表重置为0
- javascript - 对复杂数组进行分组
- javascript - 在 node.js 上使用 bcrypt 散列密码问题
- javascript - Instantsearch.js:使用来自 Algolia 的查询参数过滤初始搜索
- json - 在 PostgreSQL 中,如何使用 JSONB 将列表添加到现有 JSON 对象
- angular - 如何以角度动态创建表的 td?
- powershell - Outlook/Exchange 全局地址列表 (GAL) 上的自由文本搜索
- java - JDBC 可调用语句在本地执行,但在 Jenkins 中运行时“没有为参数号 2 设置值”
- java - 由于明显的类路径冲突,球衣 WadlAutoDiscoverable 转换错误?