【发布时间】:2020-02-19 12:45:26
【问题描述】:
我有一个 超时 在此超时之后我有一个使用 WCF 在 Windows 服务和 Windows 窗体之间发送消息。
我已经可以从 windows 窗体向 windows 服务发送消息了:
WCF
public class Interact : IInteract
{
private Func<string, int> callback;
public Interact(Func<string, int> callback)
{
this.callback = callback;
}
public void SendRequest(string name)
{
var output = this.callback(name + " callback");
}
public string AskIfAlive()
{
return "ask";
}
}
服务窗口
public partial class LMService : ServiceBase
{
private ServiceHost host;
public LMService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
Class1.Init();
Interact instance = new Interact(ReceiveMsg);
host = new ServiceHost(instance);
host.Open();
}
private int ReceiveMsg(string data)
{
// Message from Windows form to windows service
Log.writeEventLog(data);
return 1;
}
}
定时器
public static class Class1
{
static int Timeout = 0;
static Timer tm = new Timer();
public static void Init()
{
tm.Interval = 1000;
tm.Elapsed += Tm_Elapsed;
tm.Start();
}
private static void Tm_Elapsed(object sender, ElapsedEventArgs e)
{
Timeout++;
if(Timeout >= 10)
{
// SEND MESSAGE TO WINDOWS FORM
tm.Stop();
}
}
}
我想在 windows 服务超时后向 windows 窗体发送一些东西,但我不知道该怎么做,有人可以帮助我吗?
【问题讨论】:
标签: c# winforms wcf windows-services