可以反对构造返回一个空? [英] Can object constructor return a null?

查看:93
本文介绍了可以反对构造返回一个空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经采取了一些.NET 1.1的Windows服务code,它生成线程读取的消息掀起了队列(SeeBeyond的博意门的JMS队列,但是这并不重要),并反过来生成线程来处理目标的消息应用服务。我们不断遇到的逻辑和设计决策是令人费解我们没有结束。这里是一个例子,其中所述消息(lsMessage)已被检索从队列和准备处理

We have taken over some .NET 1.1 Windows Service code that spawns threads to read messages off a queue (SeeBeyond eGate JMS queue, but that is not important) and in turn spawns threads to process the message in the target application service. We are continually encountering logic and design decisions that is puzzling us to no end. Here is one example, where the message (lsMessage) has been retrieved from the queue and ready for processing

if(lsMessage != null)
{
    // Initialize a new thread class instance, pass in message
    WorkerThread worker = new WorkerThread(lsMessage);

Process:
    // Start a new thread to process the message
    Thread targetWorker = new Thread(new ThreadStart(worker.ProcessMessage));
    if(targetWorker != null)
    {
    	targetWorker.Priority = ThreadPriority.Highest;
        targetWorker.Name = "Worker " + queueKey.ToString();
    	targetWorker.Start();

    	// wait for worker thread to join back in specified period
    	bool isFinished = targetWorker.Join(SYNC_THREAD_TIMEOUT);

    	string message = worker.replyMsg;

    	if ( !isFinished )	// BF is timeout
    	{
    		targetWorker.Abort();

    		// [obscure developer name] 25/10/2004: calling Join() to wait for thread to terminate.
    		// for EAI listener threads problem, ensure no new thread is started 
    		// before the old one ends
    		targetWorker.Join();

    		// prepare reply message
    		string errorMsg = string.Format("EAIMsg {0}: BF is timeout. Send sync message back to caller.", worker.messageKey);
    		log.Debug(errorMsg);

    		message = worker.GenErrorCode(message, errorMsg);
    	}

    	// Commit message
    	MQ.ReceiverCommit(queueKey, worker.messageKey, false);

    	// Send back the response to the caller
    	MQ.RespondSend(queueKey, message); 
    } 
    else 
    {
    	log.Debug(string.Format("Fail to start worker thread to process sync message. Thread returned is null. Sleep for {0} milliseconds.", LIMIT_RESOURCE_SLEEP));
    	Thread.Sleep(LIMIT_RESOURCE_SLEEP);
    	goto Process;
    }
}

请忽略标签和GOTO 的暂时的使用;这不是问题。我们的困惑是在检查线程对象是否是实例化后,空权。下面的else语句,似乎暗示$ P $之前pvious开发商都遇到过的情况是这样的。当然,原来的开发商早已不复存在。所以,我们想知道,可以在调用构造函数之后的CLR真的实例化一个对象,并返回一个空的?我们不知道这种可能性。

Please ignore the use of label and goto for the moment; that is not the question. Our bewilderment is the check whether the Thread object is null right after instantiation. The else statement below seems to suggest the previous developers have encountered situations like this before. Of course, the original developers are long gone. So we would like to know, can the CLR really instantiate an object after the call to the constructor and return a null? We have no knowledge of such a possibility.

推荐答案

在我看来,在其他语句表明,previous开发商并不知道自己的C#。构造函数总是返回构造的对象或将引发异常。

In my opinion, the else statement suggests that the previous developers didn't know their C#. A constructor always returns a constructed object or throws an exception.

在很老的时候,C ++的构造函数可以返回,也许问题来自于这一点。这不再是真实的C ++或者,至少是默认的运营商。

In the very old times, C++ constructors could return null, so maybe the problem comes from that. This is no longer true in C++ either, at least for the default new operator.

这篇关于可以反对构造返回一个空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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