class ThreadOutput
{
int _rowCount = 0;
int _colCount = 0;
char _char = '*';
int _ret = 0;
/**//// <summary>
/// 输入参数
/// </summary>
public int RowCount
{
set { _rowCount = value; }
}
public int ColCount
{
set { _colCount = value; }
}
public char Character
{
set { _char = value; }
}
/**//// <summary>
/// 输出参数
/// </summary>
public int RetVal
{
get { return _ret; }
}
public void Output()
{
for (int row = 0; row < _rowCount; row++)
{
for (int col = 0; col < _colCount; col++)
{
Console.Write("{0} ", _char);
_ret++;
}
Console.Write("\n");
}
}
ThreadOutput to1 = new ThreadOutput();
to1.RowCount = 10;
to1.ColCount = 20;
Thread thrd = new Thread(new ThreadStart(to1.Output));
// 设置为后台线程,主要是为不影响主线程的结束
thrd.IsBackground = true;
thrd.Start();
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace UsingThread
{
struct RowCol
{
public int row;
public int col;
};
class ThreadOutput
{
// 建立等待时间
static public ManualResetEvent prompt = new ManualResetEvent(false);
int _rowCount = 0;
int _colCount = 0;
char _char = '*';
int _ret = 0;
/**//// <summary>
/// 输入参数
/// </summary>
public int RowCount
{
set { _rowCount = value; }
}
public int ColCount
{
set { _colCount = value; }
}
public char Character
{
set { _char = value; }
}
/**//// <summary>
/// 输出参数
/// </summary>
public int RetVal
{
get { return _ret; }
}
public void Output()
{
for (int row = 0; row < _rowCount; row++)
{
for (int col = 0; col < _colCount; col++)
{
Console.Write("{0} ", _char);
_ret++;
}
Console.Write("\n");
}
// 激活事件
prompt.Set();
}
public void Output(Object rc)
{
RowCol rowCol = (RowCol)rc;
for (int i = 0; i < rowCol.row; i++)
{
for (int j = 0; j < rowCol.col; j++)
Console.Write("{0} ", _char);
Console.Write("\n");
}
}
}
class Program
{
static void Main(string[] args)
{
ThreadOutput to1 = new ThreadOutput();
to1.RowCount = 10;
to1.ColCount = 20;
Thread thrd = new Thread(new ThreadStart(to1.Output));
// 设置为后台线程,主要是为不影响主线程的结束
thrd.IsBackground = true;
thrd.Start();
// 建立事件等待
ThreadOutput.prompt.WaitOne();
Console.WriteLine("{0}", to1.RetVal);
ThreadOutput to2 = new ThreadOutput();
to2.RowCount = 5;
to2.ColCount = 18;
to2.Character = '^';
Thread thrds = new Thread(new ThreadStart(to2.Output));
thrds.IsBackground = true;
thrds.Start();
thrds.Join();
Console.WriteLine("{0}", to2.RetVal);
// 传递参数给线程的另一种实现
RowCol rc = new RowCol();
rc.row = 12;
rc.col = 13;
to1.Character = '@';
if (ThreadPool.QueueUserWorkItem(new WaitCallback(to1.Output), rc))
Console.WriteLine("Insert Pool is success!");
else
Console.WriteLine("Insert Pool is failed!");
Thread.Sleep(1000);
// 使用新的ThreadStart委托来传递参数
rc.col = 19;
to2.Character = '#';
Thread thrdt = new Thread(new ParameterizedThreadStart(to2.Output));
thrdt.Start(rc);
thrdt.Join();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有