关于线程问题

geniusdhc 2007-04-29 06:01:35
我想再主窗口中创建一个线程,通过线程取得的数据再通知给主窗口,请问怎样设计,大家给个思路.
...全文
279 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
hazrael 2007-05-01
  • 打赏
  • 举报
回复
2005中使用代理
jimgreat 2007-04-30
  • 打赏
  • 举报
回复
using System.Runtime.InteropServices;


[DllImport("User32.dll")]
private static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);

把SendMessage()这个函数导进来,就可以用了
老牛73 2007-04-30
  • 打赏
  • 举报
回复
看一看Backgroudworker.(.Net 2005)新加的组件,专门处理后台线程。
cinray 2007-04-30
  • 打赏
  • 举报
回复
Up
geniusdhc 2007-04-30
  • 打赏
  • 举报
回复
最近很忙,没有及时反馈,先谢谢大家!
我用的2005版本.

请问: C#是否可以向窗口直接发送消息,如何实现.

祝大家51节玩得开心!
sswwee 2007-04-29
  • 打赏
  • 举报
回复
msdn里有详细的代码啊,就是用委托回调。
fengyecsdn 2007-04-29
  • 打赏
  • 举报
回复
最简单的办法是用一个类(或者静态,或者里边的成员作静态)把它作为数据传输的介质
具体数据传输的格式和模式你可以自己设计,最好作的通用点,以后也可以用。
然后你再扩展,比如给两个线程对象内加些事件用来传递消息或者报告行为
不过多线程的程序要认真的考虑下线程的状态、进度、同步、通信。以写出一个符合你的要求和习惯的的方案
m12345 2007-04-29
  • 打赏
  • 举报
回复
学习一下!
vivitoday 2007-04-29
  • 打赏
  • 举报
回复
学习
jimgreat 2007-04-29
  • 打赏
  • 举报
回复
上面的方法VS2003好使

VS2005就不行了

不知楼主用的是什么?
commandos 2007-04-29
  • 打赏
  • 举报
回复

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();
}
}
}
noway8881 2007-04-29
  • 打赏
  • 举报
回复
说明白点
liudng 2007-04-29
  • 打赏
  • 举报
回复
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);
}
}

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧