简单的多线程消息传递系统 [英] Simple Multi-Threaded Messaging System

查看:71
本文介绍了简单的多线程消息传递系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

如果采用正确的方法,我在理解时会遇到一些麻烦.我对C#还是很陌生-主要是在VB中进行编程.
我希望通过中央线程管理器在子线程之间建立一个强大的线程消息中继系统,该系统可以杀死它们/必要时重新启动它们.

我有一个从ApplicationContext派生的抽象基线程类(因为我想要不必附加到表单的持久性线程).
主类派生自基线程(重写SendMessage函数和其他一些东西).
所有子线程都将从基础线程派生.

Hi Guys,

I''m having a bit of trouble understanding if I''m taking the correct approach. I''m fairly new to C# - mostly been programming in VB.
I would like a robust thread message relay system between child threads through a central thread manager which can kill them/restart them if necessary.

I have an abstract base thread class that derives from ApplicationContext (as I want persistent threads that don''t have to be attached to a form).
The main class derives from base thread (overriding the SendMessage function and some other stuff).
All child threads will be derived from base thread.

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Collections;
using System.Threading;
using System.Linq;
using System.Text;

namespace Aperture
{
    public abstract class cThread : ApplicationContext
    {   
        protected int mThreadID;                                    
        SynchronizationContext mSyncContext;                        
        Queue<tmsg> msgQueue;                                       

        protected delegate bool delSendThreadMsg(tMsg tMsg);       
        delSendThreadMsg delSendThreadMsg;

        protected virtual void New(object State)
        {
            this.mSyncContext = (SynchronizationContext)State;
            this.mThreadID = Thread.CurrentThread.ManagedThreadId;
        }

        protected virtual void StartThread()
        {
          //start some stuff
        }

        protected virtual bool sendThreadMsg(tMsg tMsg)
        {
           return this.mSyncContext.Send(delSendThreadMsg,new object[]{mThreadID,tMsg}); //Problem :(
        }

        protected virtual bool recThreadMsg(object tmpObj)
        {
            //add tMsg to queue
            return true;
        }
    }
}



我遇到的问题是尝试在尚未实例化任何东西的抽象类中创建通用的sendMsg/RecMsg系统.

在我的上一个多线程程序中,我能够将mainForm传递给子线程并以这种方式调用SendMessage方法,但是现在我试图在一个抽象类中做到这一点绕开我的脑袋.

在此先感谢,
Pete



The problem I have is trying to create a generic sendMsg/RecMsg system in the abstract class where nothing has been instantiated yet.

In my last multi-Threaded program I was able to pass the mainForm to the child threads and invoke the SendMessage method that way, but now that I''m trying to do it in an abstract class I can''t get my head round it.

Thanks in Advance,
Pete

推荐答案

一个抽象类需要由一个具体的类实现,这时与您创建的任何其他类都没有区别

了解C#抽象类 [
An abstract class needs to be implemented by a concrete class which at that point is no different than any other class you create

Understanding C# Abstract Classes[^]


这被称为我们为自己创造了一些巨大的困难,然后勇敢地克服了这些困难".

您过去的将表单传递给线程的设计非常糟糕,因为您将不同的应用程序层混合在一起,并且肯定会遇到一些您可能解决的问题(仅为什么?).

现在,您正在尝试创建一个抽象类,该类没有任何合理的理由.抽象类仅在需要使用多态性时才有用,请参见
http://en .wikipedia.org/wiki/Polymorphism_%28computer_science%29 [ ^ ],但并非在所有情况下都适用,因为有时常见的基类可能是伪抽象(并且由于存在基于接口的另一种多态性).

一起使用 late绑定不能为抽象类提供证明.如果将此抽象类的变量用作具有通用功能的编译时类型,则仅需要 .

顺便说一下,看看真正有用的线程包装器:
如何将ref参数传递给线程 [ ^ ],
启动后更改线程(生产者)的参数 [ ^ ].

—SA
This is called "we are creating for ourselves some great difficulties and then bravely overcome them".

Your past design with passing a form to the thread was pretty bad, because you mixed together different application layers and certainly had some problems with that which you probably resolved (only why?).

Now, you''re trying to create an abstract class which is not justified by any rational reasons. The abstract class is only useful when you need to use polymorphism, see http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29[^], and not for all cases, because sometimes a common base class can be pseudo-abstract (and because there is another type of polymorphism, based on interfaces).

Using late binding along does not provide a justification for an abstract class; it is needed only if you use variables of this abstract class as a compile-time type carrying common functionality.

By the way, take a look at the really useful thread wrapper:
How to pass ref parameter to the thread[^],
change paramters of thread (producer) after it started[^].

—SA


这篇关于简单的多线程消息传递系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆