现在有个需求,要同步一个操作,我选择用多线程的方式(三,四个线程左右,不固定,动态添加),原本是把时间控制放在线程里面,发现在主程序上 不好控制线程开关
所以把时间控制写在了外面 每次时间到了在开启关闭线程
问题是每个线程的信息我该如何显示在UI上?
所以把时间控制写在了外面 每次时间到了在开启关闭线程
问题是每个线程的信息我该如何显示在UI上?
解决方案 »
- 高手帮忙看下这个错误怎么解决
- C# 插件开发
- 关闭imageList中的图片问题
- 0开始,想用c#写个小软件
- datagridview排序以及获取单元格列索引
- 一个SQL语句问题。在线等。
- 写一个服务程序,提供一个服务,在客户端写个程序用来控制这个服务的启动和停止,求给个思路
- c# 二进制流转成excel
- 请问:我在用视频转换工具转换电影文件的格式时为啥会出现这样的错误:"无法找到动态链接库DDRAW.dll于指定的路径C:\WINNT\System32;C:\W
- 最后的一个问题,最后的收尾工作,关于软件的打包部署,希望大家能给我一些帮助?????????????
- 图片显示的问题
- C#winform的textbox中怎么验证输入是数字
2 控制线程可以在主线程里,也可以使用一个新的线程,控制线程开关都是一样的啊
3 时间控制线程可以使用Timer当然也可以使用线程间交互控制
4 线程信息可以通过获取当前线程来解决,要在UI上显示吗,要考虑线程UI交互的特殊处理
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading; public class MyFormControl : Form
{
public delegate void AddListItem(String myString);
public AddListItem myDelegate;
private Button myButton;
private Thread myThread;
private ListBox myListBox;
public MyFormControl()
{
myButton = new Button();
myListBox = new ListBox();
myButton.Location = new Point(72, 160);
myButton.Size = new Size(152, 32);
myButton.TabIndex = 1;
myButton.Text = "Add items in list box";
myButton.Click += new EventHandler(Button_Click);
myListBox.Location = new Point(48, 32);
myListBox.Name = "myListBox";
myListBox.Size = new Size(200, 95);
myListBox.TabIndex = 2;
ClientSize = new Size(292, 273);
Controls.AddRange(new Control[] {myListBox,myButton});
Text = " 'Control_Invoke' example ";
myDelegate = new AddListItem(AddListItemMethod);
}
static void Main()
{
MyFormControl myForm = new MyFormControl();
myForm.ShowDialog();
}
public void AddListItemMethod(String myString)
{
myListBox.Items.Add(myString);
}
private void Button_Click(object sender, EventArgs e)
{
myThread = new Thread(new ThreadStart(ThreadFunction));
myThread.Start();
}
private void ThreadFunction()
{
MyThreadClass myThreadClassObject = new MyThreadClass(this);
myThreadClassObject.Run();
}
}
public class MyThreadClass
{
MyFormControl myFormControl1;
public MyThreadClass(MyFormControl myForm)
{
myFormControl1 = myForm;
}
String myString; public void Run()
{
for (int i = 1; i <= 5; i++)
{
myString = "Step number " + i.ToString() + " executed";
Thread.Sleep(400);
// Execute the specified delegate on the thread that owns
// 'myFormControl1' control's underlying window handle with
// the specified list of arguments.
myFormControl1.Invoke(myFormControl1.myDelegate,
new Object[] {myString});
}
}
}