主界面一个测试按钮。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2_TestMessageBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
m_process = new TProcess();
m_process.MessageBoxShowHandler = MessageBoxShow;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private TProcess m_process;
private void button1_Click(object sender, EventArgs e)
{
m_process.Start();
}
public DialogResult MessageBoxShow(string msg)
{
return MessageBox.Show(msg, "hello", MessageBoxButtons.YesNo);
}
}
class TProcess
{
public TProcess()
{
}
public Func<string, DialogResult> MessageBoxShowHandler;
public void Start()
{
Thread m_Th = new Thread(new ThreadStart(() => { MessageBoxShow("Hello Hi 你好!"); }));
m_Th.IsBackground = true;
m_Th.Start();
}
public void MessageBoxShow(string msg)
{
MessageBox.Show("第一");
DialogResult result = DialogResult.None;
Task.Run(() =>
{
if (MessageBoxShowHandler != null)
{
result = /*MessageBoxShowHandler(msg)*/MessageBox.Show(msg, "Task 内部测试", MessageBoxButtons.YesNo);
}
});
Task.Run(() =>
{
if (MessageBoxShowHandler != null)
{
//result = MessageBoxShowHandler(msg);
result = (DialogResult)(MessageBoxShowHandler?.Invoke(msg + "1"));
}
});
while (true)
{
if (result == DialogResult.Yes)
{
Console.WriteLine("Yes");
break;
}
}
}
}
}
标签:MessageBox,C#,阻塞,System,MessageBoxShowHandler,msg,using,DialogResult,public
From: https://www.cnblogs.com/huvjie/p/17115258.html