c# 多线程中值的值的传递
在另一个线程中每达到一定要求就向主线程输出一个值应该怎么做比如for(inti=1;i<10000;i++){if(i%10==0){}}如果是10的倍数就向主线程输出这...
在另一个线程中 每达到一定要求 就向主线程输出一个值 应该怎么做
比如for (int i = 1; i < 10000; i++)
{
if (i % 10 == 0)
{
}
}
如果是10的倍数 就向主线程输出这个值 主线程立刻用控件将这个值显示
该怎么做 展开
比如for (int i = 1; i < 10000; i++)
{
if (i % 10 == 0)
{
}
}
如果是10的倍数 就向主线程输出这个值 主线程立刻用控件将这个值显示
该怎么做 展开
展开全部
你可以使用delegate实现,但delegate使用的是线程池,一个线程池默认最大25个线程. 有次我就超了(实际自己只能再创建23个),后来只得改为 new Thread 了.
下面是Inside C# 上面的一个例子:
using System;
using System.Threading;
class AsyncDelegatesBlocked
{
public static int Add(int op1, int op2, out int result)
{
Thread.Sleep(3000); // Simulating work
return (result = op1 + op2);
}
public delegate int AddDelegate(int op1, int op2,
out int result);
static void Main()
{
int result;
AddDelegate add = new AddDelegate(Add);
Console.WriteLine("[Main] Invoking the asynchronous " +
"Add method");
IAsyncResult iAR = add.BeginInvoke(6, 42, out result,
null, null);
// Here we're simulating doing some work before
// blocking on the Add method's completion.
Console.Write("[Main] Doing other work");
for (int i = 0; i < 10; i++)
{
Thread.Sleep(200);
Console.Write(".");
}
Console.WriteLine("\n[Main] Waiting for Add to finish");
iAR.AsyncWaitHandle.WaitOne(); // Blocks until thread
// is completed
Console.WriteLine("[Main] Add finished, cleaning up");
add.EndInvoke(out result, iAR);
Console.WriteLine("[Main] The result is {0}", result);
Console.ReadLine();
}
};
下面是Inside C# 上面的一个例子:
using System;
using System.Threading;
class AsyncDelegatesBlocked
{
public static int Add(int op1, int op2, out int result)
{
Thread.Sleep(3000); // Simulating work
return (result = op1 + op2);
}
public delegate int AddDelegate(int op1, int op2,
out int result);
static void Main()
{
int result;
AddDelegate add = new AddDelegate(Add);
Console.WriteLine("[Main] Invoking the asynchronous " +
"Add method");
IAsyncResult iAR = add.BeginInvoke(6, 42, out result,
null, null);
// Here we're simulating doing some work before
// blocking on the Add method's completion.
Console.Write("[Main] Doing other work");
for (int i = 0; i < 10; i++)
{
Thread.Sleep(200);
Console.Write(".");
}
Console.WriteLine("\n[Main] Waiting for Add to finish");
iAR.AsyncWaitHandle.WaitOne(); // Blocks until thread
// is completed
Console.WriteLine("[Main] Add finished, cleaning up");
add.EndInvoke(out result, iAR);
Console.WriteLine("[Main] The result is {0}", result);
Console.ReadLine();
}
};
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
改史诩原贴:
public partial class Form1 : Form
{
private delegate void ReportNumCallBack(int num);
private ReportNumCallBack report;
public Form1()
{
InitializeComponent();
report = new ReportNumCallBack(SetText);
}
private void Form1_Load(object sender, EventArgs e)
{
Thread thread = new Thread(new ThreadStart(ThreadWork));
thread.Start();
}
private void ThreadWork()
{
for (int i = 0; i < 100; i++)
{
Thread.Sleep(10);
if (i % 10 == 0)
{
SetText(i);
}
}
}
private void SetText(int nValue)
{
if (this.textBox1.InvokeRequired)
{
this.textBox1.Invoke(report, nValue);
}
else
{
this.textBox1.Text = nValue.ToString();
}
}
}
public partial class Form1 : Form
{
private delegate void ReportNumCallBack(int num);
private ReportNumCallBack report;
public Form1()
{
InitializeComponent();
report = new ReportNumCallBack(SetText);
}
private void Form1_Load(object sender, EventArgs e)
{
Thread thread = new Thread(new ThreadStart(ThreadWork));
thread.Start();
}
private void ThreadWork()
{
for (int i = 0; i < 100; i++)
{
Thread.Sleep(10);
if (i % 10 == 0)
{
SetText(i);
}
}
}
private void SetText(int nValue)
{
if (this.textBox1.InvokeRequired)
{
this.textBox1.Invoke(report, nValue);
}
else
{
this.textBox1.Text = nValue.ToString();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public delegate void ReportNumCallBack(int num);
private event ReportNumCallBack reportNumCallBack;
//private delegate void UpdateTextBoxEventHandler(int num);
private void Form1_Load(object sender, EventArgs e)
{
reportNumCallBack = new ReportNumCallBack(ShowNum);
Thread thread=new Thread(new ThreadStart(ThreadWork));
thread.Start();
}
private void ShowNum(int num)
{
this.Invoke(new ReportNumCallBack(delegate(int number)
{
textBox1.Text = number.ToString();
}), new object[] { num });
}
private void ThreadWork()
{
for (int i = 0; i < 10000; i++)
{
Thread.Sleep(10);
if (i % 10 == 0)
{
reportNumCallBack(i);
}
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public delegate void ReportNumCallBack(int num);
private event ReportNumCallBack reportNumCallBack;
//private delegate void UpdateTextBoxEventHandler(int num);
private void Form1_Load(object sender, EventArgs e)
{
reportNumCallBack = new ReportNumCallBack(ShowNum);
Thread thread=new Thread(new ThreadStart(ThreadWork));
thread.Start();
}
private void ShowNum(int num)
{
this.Invoke(new ReportNumCallBack(delegate(int number)
{
textBox1.Text = number.ToString();
}), new object[] { num });
}
private void ThreadWork()
{
for (int i = 0; i < 10000; i++)
{
Thread.Sleep(10);
if (i % 10 == 0)
{
reportNumCallBack(i);
}
}
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询