您现在的位置是:主页 > news > dw怎么建设网站/怎么让网站被百度收录
dw怎么建设网站/怎么让网站被百度收录
admin2025/5/8 16:34:21【news】
简介dw怎么建设网站,怎么让网站被百度收录,太原网站推广怎么做,网页案例图片这天,遇到一情况,朋友建议用多线程,于是上网查了资料,写了代码。又遇一情况,发现新线程调用界面控件就无故(visual studio不报错,又想哭)退出了。。哭着问了祥哥,祥哥指点迷津,给关键…
dw怎么建设网站,怎么让网站被百度收录,太原网站推广怎么做,网页案例图片这天,遇到一情况,朋友建议用多线程,于是上网查了资料,写了代码。又遇一情况,发现新线程调用界面控件就无故(visual studio不报错,又想哭)退出了。。哭着问了祥哥,祥哥指点迷津,给关键…
,给关键字,google了一下,发现了几个方法来和界面空间交互。下面贴两个方法记录一下查询结果。
这天,遇到一情况,朋友建议用多线程,于是上网查了资料,写了代码。又遇一情况,发现新线程调用界面控件就无故(visual studio不报错,又想哭)退出了。。哭着问了祥哥,祥哥指点迷津
1,设置 CheckForIllegalCrossThreadCalls = false; //指示是否对错误线程的调用,即是否允许在创建UI的线程之外访问线程
2,使用Invoke方法
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; namespace 多线程与UI交互 { public partial class Form1 : Form { Thread ThreadOne;//线程1 Thread ThreadTwo;//线程2 public Form1() { ThreadOne = new Thread(new ThreadStart(UpdateTime)); ThreadOne.Name = "更新时间"; ThreadTwo = new Thread(new ThreadStart(UpProgressbar)); ThreadTwo.Name = "更新进度条进度";
//指示是否对错误线程的调用,即是否允许在创建UI的线程之外访问线程 CheckForIllegalCrossThreadCalls = false;
InitializeComponent(); } private void BtnStartThread_Click(object sender, EventArgs e) {
//启动线程 ThreadOne.Start(); ThreadTwo.Start(); } private void BtnCloseThread_Click(object sender, EventArgs e) {
//关闭线程 ThreadOne.Abort(); ThreadTwo.Abort(); } //使用线程1 更新界面label控件来显示时间 private void UpdateTime() { while (true) label1.Text = DateTime.Now.ToString(); } //使用线程2 更新进度条 private void UpProgressbar() { while (progressBar1.Value < progressBar1.Maximum) progressBar1.PerformStep(); } } }
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace 多线程与UI交互Invoke
{
public partial class Form1 : Form
{
Thread ThreadOne;//线程1 Thread ThreadTwo;//线程2 //定义delegate以便Invoke时使用
private delegate void UpdateProgressBar(int value);//更新进度条进度
private delegate void UpdateLabel(string value);//更新label时间
public Form1()
{
ThreadOne = new Thread(new ThreadStart(UpdateTime));
ThreadOne.Name = "更新时间";
ThreadTwo = new Thread(new ThreadStart(UpProgressbar));
ThreadTwo.Name = "更新进度条进度";
InitializeComponent();
}
private void BtnStartThread_Click(object sender, EventArgs e)
{
ThreadOne.Start();//启动线程
ThreadTwo.Start();
}
private void BtnCloseThread_Click(object sender, EventArgs e)
{
ThreadOne.Abort();//关闭线程
ThreadTwo.Abort();
}
private void UpdateTime()
{
while (true)
{
if (InvokeRequired)
{
//调用UpdateLabelText方法,传入当前时间
this.Invoke(new UpdateLabel(UpdateLabelText),DateTime.Now.ToString());
}
}
}
private void UpProgressbar()
{
int value = 1;
while (progressBar1.Value < progressBar1.Maximum)
{
if (InvokeRequired)
{
//调用UpdateProgressValue方法,传入进度条值
this.Invoke(new UpdateProgressBar(UpdateProgressValue), value++);
}
}
}
//接受参数,更新界面lable控件,显示时间
private void UpdateLabelText(string dateTime)
{
label1.Text = dateTime;
}
//接受参数,更新进度条值
private void UpdateProgressValue(int value)
{
progressBar1.Value = value;
}
}
}
转载于:https://blog.51cto.com/laohutoutou/655246