小编Gra*_*amS的帖子

你如何防止IDisposable传播到你的所有课程?

从这些简单的类开始......

假设我有一组这样简单的类:

class Bus
{
    Driver busDriver = new Driver();
}

class Driver
{
    Shoe[] shoes = { new Shoe(), new Shoe() };
}

class Shoe
{
    Shoelace lace = new Shoelace();
}

class Shoelace
{
    bool tied = false;
}
Run Code Online (Sandbox Code Playgroud)

A Bus有一个Driver,Driver有两个Shoe,每个Shoe有一个Shoelace.一切都很傻.

将一个IDisposable对象添加到Shoelace

后来我决定对它进行一些操作Shoelace可能是多线程的,所以我添加了一个EventWaitHandle用于与之通信的线程.所以Shoelace现在看起来像这样:

class Shoelace
{
    private AutoResetEvent waitHandle = new AutoResetEvent(false);
    bool tied = false;
    // ... other stuff .. …
Run Code Online (Sandbox Code Playgroud)

.net c# garbage-collection dispose idisposable

137
推荐指数
3
解决办法
7614
查看次数

如何获取Win32中可用串行端口的列表?

我有一些遗留代码,它通过调用EnumPorts()函数然后过滤以"COM"开头的端口名称来提供PC上可用COM端口的列表.

出于测试目的,如果我可以将此代码用于com0com这样的代码,它将非常有用,它提供了作为零调制解调器循环在一起的虚拟COM端口对.

但是该EnumPorts()函数找不到com0com端口(即使没有过滤"COM").HyperTerminal和SysInternals PortMon都可以看到它们,所以我确信它安装正确.

那么还有其他一些Win32功能可以提供可用串口的明确列表吗?

winapi serial-port

49
推荐指数
3
解决办法
6万
查看次数

'const struct'与'struct'有什么不同?

什么const struct意思?它有什么不同struct吗?

c syntax

42
推荐指数
2
解决办法
7万
查看次数

谁处置了IDisposable公共财产?

如果我有一个SomeDisposableObject实现的类IDisposable:

class SomeDisposableObject : IDisposable
{
    public void Dispose()
    {
        // Do some important disposal work.
    }
}
Run Code Online (Sandbox Code Playgroud)

我有另一个叫做的类AContainer,它有SomeDisposableObject一个公共属性的实例:

class AContainer
{
    SomeDisposableObject m_someObject = new SomeDisposableObject();

    public SomeDisposableObject SomeObject
    {
        get { return m_someObject; }
        set { m_someObject = value; }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后FxCop将坚持AContainer也是IDisposable.

这是很好的,但我看不出我可以安全地调用m_someObject.Dispose()AContainer.Dispose(),作为另一个类可能还是要在一个参考m_someObject实例.

避免这种情况的最佳方法是什么?

(假设其他代码依赖于AContainer.SomeObject始终具有非空值,因此只是将实例的创建移到外部AContainer不是一个选项)

编辑:我将扩展一些例子,因为我认为一些评论者错过了这个问题.如果我只实现一个调用m_someObject.Dispose()的Dispose()方法,AContainer那么我将留下这些情况:

// Example One
AContainer …
Run Code Online (Sandbox Code Playgroud)

.net c# dispose idisposable

28
推荐指数
3
解决办法
5914
查看次数

如何在断开连接后干净地重新连接boost :: socket?

我的客户端应用程序使用a boost::asio::ip::tcp::socket连接到远程服务器.如果应用程序失去与此服务器的连接(例如,由于服务器崩溃或被关闭),我希望它定期尝试重新连接,直到成功为止.

我需要在客户端做什么来干净地处理断开连接,整理然后重复尝试重新连接?

目前,我的代码中有趣的部分看起来像这样.

connect喜欢这个:

bool MyClient::myconnect()
{
    bool isConnected = false;

    // Attempt connection
    socket.connect(server_endpoint, errorcode);

    if (errorcode)
    {
        cerr << "Connection failed: " << errorcode.message() << endl;
        mydisconnect();
    }
    else
    {
        isConnected = true;

        // Connected so setup async read for an incoming message.
        startReadMessage();

        // And start the io_service_thread
        io_service_thread = new boost::thread(
            boost::bind(&MyClient::runIOService, this, boost::ref(io_service)));
    }
    return (isConnected)
}
Run Code Online (Sandbox Code Playgroud)

runIOServer()方法就是:

void MyClient::runIOService(boost::asio::io_service& io_service)
{
    size_t executedCount = io_service.run();
    cout << "io_service: …
Run Code Online (Sandbox Code Playgroud)

c++ sockets boost boost-asio

27
推荐指数
2
解决办法
2万
查看次数

boost :: asio干净地断开连接

有时boost :: asio似乎在我想要之前断开连接,即在服务器正确处理断开连接之前.我不确定这是怎么可能的,因为客户端似乎认为它完全发送了消息,但是当服务器发出错误时它甚至没有读取消息头...在测试期间,这种情况可能只发生在5次中,服务器接收客户端关闭消息,并干净地断开客户端.

错误:"远程主机强行关闭现有连接"

客户端断开连接:

void disconnect()
{
    boost::system::error_code error;
    //just creates a simple buffer with a shutdown header
    boost::uint8_t *packet = createPacket(PC_SHUTDOWN,0);
    //sends it
    if(!sendBlocking(socket,packet,&error))
    {
        //didnt get here in my tests, so its not that the write failed...
        logWrite(LOG_ERROR,"server",
            std::string("Error sending shutdown message.\n")
            + boost::system::system_error(error).what());
    }

    //actaully disconnect
    socket.close();
    ioService.stop();
}
bool sendBlocking(boost::asio::ip::tcp::socket &socket,
    boost::uint8_t *data, boost::system::error_code* error)
{
    //get the length section from the message
    boost::uint16_t len = *(boost::uint16_t*)(data - 3);
    //send it
    asio::write(socket, asio::buffer(data-3,len+3),
        asio::transfer_all(), *error); …
Run Code Online (Sandbox Code Playgroud)

c++ sockets network-programming tcp boost-asio

26
推荐指数
3
解决办法
4万
查看次数

我需要Dispose()或Close()一个EventWaitHandle吗?

如果我使用EventWaitHandle(或AutoResetEvent,ManualResetEvent)来同步线程之间然后做我需要调用Close()Dispose()方法对事件处理的时候,我用它做什么?

EventWaitHandle继承自WaitHandle,实现IDisposable.如果我没有IDisposable在包含任何类的任何类上实现,FxCop会抱怨EventWaitHandle.所以这表明我确实需要打电话给它.

但是,这些MSDN用法示例都没有调用Dispose()Close():

http://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle(VS.80).aspx http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent( VS.80).aspx http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent(VS.80).aspx

这只是微软无视自己建议的一个例子吗?

.net c# multithreading synchronization

21
推荐指数
2
解决办法
1万
查看次数

是否有STL/boost算法来检查容器中的所有元素是否匹配值?

是否有STL/boost算法将测试两个迭代器之间的所有元素是否匹配给定值?或者谓词返回true所有这些?

即类似的东西

template<class InputIterator, class T>
InputIterator all_match (InputIterator first, InputIterator last, const T& value)
{
    bool allMatch = true;
    while(allMatch && first!=last)
        allMatch = (value == *first++);
    return allMatch;
}
Run Code Online (Sandbox Code Playgroud)

要么

template <class InputIterator, class Predicate>
bool all_true (InputIterator first, InputIterator last, Predicate pred)
{
    bool allTrue = true;
    while (allTrue && first != last) 
        allTrue = pred(*first++);
    return allTrue;
}
Run Code Online (Sandbox Code Playgroud)

c++ algorithm boost stl

15
推荐指数
2
解决办法
3060
查看次数

如何在LynxOS/POSIX中同步对共享内存的访问?

我正在LynxOS SE(POSIX conformant)系统上实现两个进程,它们将通过共享内存进行通信.

一个过程将充当"生产者"而另一个过程将充当"消费者".在多线程系统中,我的方法是使用互斥和condvar(条件变量)对,消费者在condvar(with pthread_cond_wait)上等待,生产者pthread_cond_signal在更新共享内存时用信号通知它(with ).

如何在多进程架构而不是多线程架构中实现这一目标?

是否有LynxOS/POSIX方法来创建可在进程之间使用的condvar/mutex对?
或者在这种情况下,其他一些同步机制更合适吗?

c posix ipc shared-memory lynxos

10
推荐指数
4
解决办法
1万
查看次数

如何从LPCTSTR转换为std :: string?

我有一个LPCTSTR想要调用带std::string参数的函数.

我需要做什么转换?

c++ string visual-c++

9
推荐指数
2
解决办法
4万
查看次数