110,567
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Threading;
namespace TestAsyncCallback
{
public delegate string GetReplyDelegate(int id, string msg);
static class Program1
{
internal static GetReplyDelegate d = null;
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
d = new GetReplyDelegate(GetReplyDelay);
Start();
Console.ReadLine();
}
static string GetReplyDelay(int id, string msg)
{
Console.WriteLine("add:" + id.ToString());
Thread.Sleep(10000);
return msg + id.ToString();
}
static void Start()
{
for (int i = 1; i <= 1000; i++)
{
//ParameterizedThreadStart t = new ParameterizedThreadStart(GetReplyProc);
//Thread thread = new Thread(t);
//thread.IsBackground = true;
//thread.Start(new object[] { i, "test" });
d.BeginInvoke(i, "test", Callback, null);
}
}
static void Callback(IAsyncResult ar)
{
string s = d.EndInvoke(ar);
Console.WriteLine(s);
}
private static void GetReplyProc(object obj)
{
object[] os = (object[])obj;
string s = GetReplyDelay((int)os[0], os[1].ToString());
Console.WriteLine(s);
}
}
}
using System;
using System.Threading;
namespace TestAsyncCallback
{
public delegate string GetReplyDelegate(int id, string msg);
static class Program1
{
internal static GetReplyDelegate d = null;
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
d = new GetReplyDelegate(GetReplyDelay);
Start();
Console.ReadLine();
}
static string GetReplyDelay(int id, string msg)
{
Console.WriteLine("add:" + id.ToString());
Thread.Sleep(10000);
return msg + id.ToString();
}
static void Start()
{
for (int i = 1; i <= 1000; i++)
{
ParameterizedThreadStart t = new ParameterizedThreadStart(GetReplyProc);
Thread thread = new Thread(t);
thread.IsBackground = true;
thread.Start(new object[] { i, "test" });
//d.BeginInvoke(i, "test", Callback, null);
}
}
static void Callback(IAsyncResult ar)
{
string s = d.EndInvoke(ar);
Console.WriteLine(s);
}
private static void GetReplyProc(object obj)
{
object[] os = (object[])obj;
string s = GetReplyDelay((int)os[0], os[1].ToString());
Console.WriteLine(s);
}
}
}