using System;
using System.Threading;// The ThreadWithState class contains the information needed for
// a task, the method that executes the task, and a delegate
// to call when the task is complete.
//
public class ThreadWithState {
// State information used in the task.
private string boilerplate;
private int value; // Delegate used to execute the callback method when the
// task is complete.
private ExampleCallback callback; // The constructor obtains the state information and the
// callback delegate.
public ThreadWithState(string text, int number,
ExampleCallback callbackDelegate)
{
boilerplate = text;
value = number;
callback = callbackDelegate;
}
// The thread procedure performs the task, such as
// formatting and printing a document, and then invokes
// the callback delegate with the number of lines printed.
public void ThreadProc()
{
Console.WriteLine(boilerplate, value);
if (callback != null)
callback(1);
}
}// Delegate that defines the signature for the callback method.
//
public delegate void ExampleCallback(int lineCount);// Entry point for the example.
//
public class Example
{
public static void Main()
{
// Supply the state information required by the task.
ThreadWithState tws = new ThreadWithState(
"This report displays the number {0}.",
42,
new ExampleCallback(ResultCallback)
); Thread t = new Thread(new ThreadStart(tws.ThreadProc));
t.Start();
Console.WriteLine("Main thread does some work, then waits.");
t.Join();
Console.WriteLine(
"Independent task has completed; main thread ends.");
} // The callback method must match the signature of the
// callback delegate.
//
public static void ResultCallback(int lineCount)
{
Console.WriteLine(
"Independent task printed {0} lines.", lineCount);
}
}
using System.Threading;// The ThreadWithState class contains the information needed for
// a task, the method that executes the task, and a delegate
// to call when the task is complete.
//
public class ThreadWithState {
// State information used in the task.
private string boilerplate;
private int value; // Delegate used to execute the callback method when the
// task is complete.
private ExampleCallback callback; // The constructor obtains the state information and the
// callback delegate.
public ThreadWithState(string text, int number,
ExampleCallback callbackDelegate)
{
boilerplate = text;
value = number;
callback = callbackDelegate;
}
// The thread procedure performs the task, such as
// formatting and printing a document, and then invokes
// the callback delegate with the number of lines printed.
public void ThreadProc()
{
Console.WriteLine(boilerplate, value);
if (callback != null)
callback(1);
}
}// Delegate that defines the signature for the callback method.
//
public delegate void ExampleCallback(int lineCount);// Entry point for the example.
//
public class Example
{
public static void Main()
{
// Supply the state information required by the task.
ThreadWithState tws = new ThreadWithState(
"This report displays the number {0}.",
42,
new ExampleCallback(ResultCallback)
); Thread t = new Thread(new ThreadStart(tws.ThreadProc));
t.Start();
Console.WriteLine("Main thread does some work, then waits.");
t.Join();
Console.WriteLine(
"Independent task has completed; main thread ends.");
} // The callback method must match the signature of the
// callback delegate.
//
public static void ResultCallback(int lineCount)
{
Console.WriteLine(
"Independent task printed {0} lines.", lineCount);
}
}
解决方案 »
- c#多线程问题-求大侠指教!
- |zyciis| 用bat注册了一个Windows服务后如何让他是自动启动的啊 还有我现在这个服务没有效果不知道哪里出错,要如何调试呢
- winform项目-信息保存到专用文档
- C#中怎样继承已经创建好的Form
- 用Process执行外部程序(cmd.exe)中途停止
- PC与PDA之间通过红外(IrDA)传输文件如何用C#实现?
- 如何获取任务栏中当前开着的窗体句柄??
- 在DataGrid1中用radiobuttonlist
- 根据网上源码综合的使用代理的投票程序,几个问题解决不了,讨论下
- winform用filestream生成文档,总是会在C盘windows目录下再生成一个文档
- vs2005 新建窗体,如何让Form1.cs与Form1.Designer.cs合并为一个cs,拒绝手工合并!!
- 新手请教 类型转换问题 先谢了
vs2003调试通过
使用一个事件就行了。
*************************************************************
Form1.cs
**************************************************************/private void button1_Click(object sender, System.EventArgs e)
{
Class1 c= new Class1();
c.doSomething += new Class1.DoSomething(DoSome);
c.Start ();
}private void DoSome(int i)
{
label1.Text = i.ToString();
}
/*************************************************************
Class1.cs
**************************************************************/using System;
using System.Threading ;namespace test
{//public DoSomething doSomething = null;
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Class1
{
//定义委托
private delegate void DoSomething (int i);
//定义事件
public event DoSomething doSomething;
public Class1()
{
//
// TODO: 在此处添加构造函数逻辑
//
}private void Thread1()
{
for(int i =0 ;i<1000;i++)
{
//触发事件
doSomething(i);
}
}public void Start()
{
Thread thread = new Thread(new ThreadStart(Thread1));
thread.IsBackground =true;
thread.Name="thread1";
thread.Start();
}
}
}
[DllImport("User32.dll")]
private static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);把SendMessage()这个函数导进来,就可以用了