线程间操作ui
Posted liuyudong0825
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了线程间操作ui相关的知识,希望对你有一定的参考价值。
using EasyNetQ.Logging; using Quartz; using Quartz.Impl; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp2 { public partial class Form1 : Form { private static IScheduler _scheduler; SynchronizationContext synchronizationContext = null; delegate void ChangeText1(string strmessage); private AsyncOperation operation; public event EventHandler labclick; public Form1() { InitializeComponent(); synchronizationContext = SynchronizationContext.Current; operation = AsyncOperationManager.CreateOperation(null); } //public int localPort = 0; private void Form1_Load(object sender, EventArgs e) { IPHostEntry ipe = Dns.GetHostEntry(Dns.GetHostName()); IPAddress[] ip = ipe.AddressList; IPAddress iPAddress = ip[1]; //IPAddress localIp = IPAddress.Parse("127.0.0.1"); IPEndPoint iep = new IPEndPoint(ip[1], 80); this.label2.Text = iep.Port.ToString(); string strip= ip.Select(o=>o).Where(o=>o.AddressFamily.ToString()== "InterNetwork").FirstOrDefault().ToString(); this.label1.Text = strip; //ChangeText1 = (strmessage) => { setText1value(strmessage); }; SettextBox2Value(Thread.CurrentThread.ManagedThreadId.ToString(),""); // //PlaySound(); Thread thread = new Thread(new ThreadStart(GetMessage)); thread.IsBackground = true; thread.Start(); //ConsoleLogProvider logProvider = new ConsoleLogProvider(); //logProvider.SetLogCallBack((log) => //{ // this.rchMessage.AppendText(log); //}); //MusicJob._printLogCallBack = (log) => //{ // synchronizationContext.Send((obj) => // { // this.textBox2.AppendText(log.ToString()); // }, null); //}; //MusicJob._printLogCallBack = delegate (string log,string strThread) //{ // synchronizationContext.Post(delegate { SettextBox2Value(log.ToString(), strThread.ToString()); }, null); //}; MusicJob._printLogCallBack = delegate (string log, string strThread) { synchronizationContext.Post(delegate { SettextBox2Value(log.ToString(), strThread.ToString()); }, null); }; //LogProvider.SetCurrentLogProvider(logProvider); } public string SettextBox2Value(string log,string strThread) { this.textBox2.AppendText(log.ToString()+ strThread+ Thread.CurrentThread.ManagedThreadId.ToString()); return log.ToString() + strThread; } //public Form1(int localPort) //{ // this.localPort = localPort; //} /// <summary> /// 接收button传递过来的信息 /// </summary> private void GetMessage() { UdpClient udpClient = new UdpClient(5839); IPEndPoint ip = null; // 保证一直处于接收状态 while (true) { byte[] message = udpClient.Receive(ref ip);//接收 string strmessage = Encoding.UTF8.GetString(message); //string strmessage = "ceshi111"; //synchronizationContext.Post(setTextvalue, strmessage); //this.BeginInvoke(new ChangeText1(setText1value),strmessage);异步执行委托 this.Invoke(new ChangeText1(setText1value), strmessage);//委托很重要 } } private void setText1value(object str) { this.textBox1.Text = this.textBox1.Text+ str.ToString(); } private void button1_Click(object sender, EventArgs e) { UdpClient uc = new UdpClient("127.0.0.1", 5839);//udp协议发送 byte[] message = Encoding.UTF8.GetBytes("1"); uc.Send(message, message.Length); uc.Close(); } public string Sound { get; set; } private async Task PlaySound() { //synchronizationContext.Send(SettextBox2Value, Thread.CurrentThread.ManagedThreadId.ToString()); //1.通过工厂获取一个调度器的实例 StdSchedulerFactory factory = new StdSchedulerFactory(); _scheduler = await factory.GetScheduler(); await _scheduler.Start(); //创建任务对象 IJobDetail job = JobBuilder.Create<MusicJob>() .WithIdentity("job1", "group1") .Build(); //创建触发器 ITrigger trigger = TriggerBuilder.Create() .WithIdentity("trigger1", "group1") .StartNow() .WithCronSchedule("30 0/1 * * * ?")//每分钟的第30秒执行 .Build(); ////将任务加入到任务池 await _scheduler.ScheduleJob(job, trigger); job = JobBuilder.Create<PrintTxtJob>() .WithIdentity("job2", "group1") .Build(); trigger = TriggerBuilder.Create() .WithIdentity("trigger2", "group1") .StartNow() .WithCronSchedule("0 0/1 * * * ?")//每分钟的第0秒执行 .Build(); await _scheduler.ScheduleJob(job, trigger); } private void label1_Click(object sender, EventArgs e) { } } }
以上是关于线程间操作ui的主要内容,如果未能解决你的问题,请参考以下文章