这是静态可能吗 [英] Is this static posible

查看:72
本文介绍了这是静态可能吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
这可以吗?会稳定吗?

Hi All,
Is this posible? Will it be stable?

namespace A1
{
    public class A
    {
        private static Queue<object> q = new Queue<object>(); //??????????
        public static void EnQueue(object obj)
        {
            q.Enqueue(obj);
        }
        public static objedt DeQueue()
        {
            return q.Dequeue();
        }
        public static object PeekFirst()
        {
            return q.Peek();
        }
    }
}
namespace B1
{
    public class B
    {
        //use class A.
        internal void DoWork()
        {
            //do some work here.
            A1.A.EnQueue();//?????
            //do some work here.
            A1.A.PeekFirst();//?????
            A1.A.DeQueue();//?????
        }
    }
}

推荐答案

我不明白为什么不应该这样做.

至于稳定性,只要您仅使用一个线程访问队列,一切都应该正常工作.默认情况下,Queue类不是线程安全的.引用 MSDN :

此类型的公共静态(在Visual Basic中为Shared)成员是线程安全的.
不保证任何实例成员都是线程安全的.
只要不修改集合,一个Queue(T)就可以同时支持多个阅读器. (...)
I can''t see why it should not be possible.

As for stability, everything should work fine as long as you use only one thread to access the queue. The Queue class is not thread safe by default. Quote from MSDN:

Public static (Shared in Visual Basic) members of this type are thread safe.
Any instance members are not guaranteed to be thread safe.
A Queue(T) can support multiple readers concurrently, as long as the collection is not modified. (...)


是的(如果您解决了编译问题-"objedt"成为"object","DoWork"应使用"A.)

但是,这是一个麻烦的解决方案.如果可能的话,尽量不要使用静态变量.
谢谢.
我的目的是使用Queue系统,在同一解决方案的2个项目之间共享数据.在项目1中.接收方在此队列上有2个班级.在Project 2中,提供者端将一类数据放入Queue.
您对这个问题有更好的解决方法吗,拜托,我全力以赴.
我想用一个实例解决这个问题,为所有项目和类提供服务.
最适合你.
"


哦!不-不行!
如果您有两个项目,那么您将有两个过程-即使它们在同一个解决方案中.每个进程都有自己的内存空间-它们不共享.因此,即使您在一个类中有一个静态变量,如果您有两个进程,它们也不会共享相同的静态变量.

换句话说:如果将数据放在某个进程的静态队列中,则必须在同一进程中将其删除(即使它位于不同的线程上).您不能将其从其他进程的静态队列中删除.

相反,请查看进程间通信-套接字在这里可能有点笨拙,但它们可能是最简单的方法!
参见此处 [
Yes, it would work (provided you fix the compilation problems - "objedt" becomes "object", "DoWork" should use the results from "A".)

It''s a messy solution though. Try not to use statics if you possibly can.
"Than you.
My intension is to use the Queue system, shoveling data between 2 Projects in the same solution. In Project 1. The receiver side there are 2 classes depenging on this Queue. In Project 2, the giver side, one class puting data in Queue.
Do you have a better solution to the problem, Please, Im all ears off course.
I want to solve this problem with one instance, serving all projects and classes.
Best to you.
"


Oooh! No - it won''t work!
If you have two projects, then you have two processes - even if they are in the same solution. Each process has it''s own memory space - they do not share. So even if you have a static variable in a class, if you have two processes they do not share the same static variable.

To put it another way: If you put data on the static queue in a process, you must remove it in the same process (even if it is on a different thread). You cannot remove it from a different processes static queue.

Instead, look at Inter-process communciations - sockets may be a little heavy handed here but they may be the easiest way to do it!
See Here[^] for a quick tutorial.


是的,我们很有可能.您实际上可以尝试使用单例设计模式来实现类似的功能.
Yes it us quite possible. You might actually try and use the singleton design pattern to achieve something similar.


这篇关于这是静态可能吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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