#2
zj8912182020-03-29 15:26
|
程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Threading;
namespace cs
{
class Program
{
static void Main(string[] args)
{
Guo锅 g = new Guo锅();
g.CCEvent += G_CCEvent;
g.Play();
string r = Console.ReadLine();
if (r == "s")
{
g.Stop();
}
}
private static void G_CCEvent(string Msg)
{
Console.WriteLine(Msg);
//throw new NotImplementedException();
}
}
public class Guo锅
{
public delegate void CC炒菜EventHandler(string Msg);
public event CC炒菜EventHandler CCEvent;
private bool State=false;
public void Play()
{
State = true;
while (State)
{
//触发事件
if (CCEvent != null)
{
CCEvent("炒菜中……" + DateTime.Now.ToString());
}
Thread.Sleep(1000);
}
}
public void Stop()
{
State = false;
}
}
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Threading;
namespace cs
{
class Program
{
static void Main(string[] args)
{
Guo锅 g = new Guo锅();
g.CCEvent += G_CCEvent;
g.Play();
string r = Console.ReadLine();
if (r == "s")
{
g.Stop();
}
}
private static void G_CCEvent(string Msg)
{
Console.WriteLine(Msg);
//throw new NotImplementedException();
}
}
public class Guo锅
{
public delegate void CC炒菜EventHandler(string Msg);
public event CC炒菜EventHandler CCEvent;
private bool State=false;
public void Play()
{
State = true;
while (State)
{
//触发事件
if (CCEvent != null)
{
CCEvent("炒菜中……" + DateTime.Now.ToString());
}
Thread.Sleep(1000);
}
}
public void Stop()
{
State = false;
}
}
程序开始后,一直显示在炒菜,怎么才能让我输入s然后暂停掉呢?怎么在触发事件这里转移控制权?