通知另一个线程,操作正在运行或已完成

时间:2016-03-30 13:35:10

标签: c# multithreading mutex

我有多线程应用程序,其中不同的线程可能想要执行操作。我试图使用Mutex来确保,如果该线程已在运行,该线程不会启动操作。

System.Threading.Mutex mutex;
bool isRunning = System.Threading.Mutex.TryOpenExisting(name, out mutex);
if (!isRunning)
{
   RunMethod();
}

在我创建互斥锁的方法中,并尝试在最后发布它:

var mutex = new Mutex(true, name);
try{   
    //do stuff, it takes some time
}
finally
{
    //TODO: I want to get rid of Mutex here
}

如何摆脱互斥锁?因为即使在我致电mutex.ReleaseMutex()mutex.Close()之后,它仍然存在且可以找到。如何通知操作当前正在运行或已完成?

还有其他办法吗?

1 个答案:

答案 0 :(得分:0)

同样,就像CodingGorilla所说,使用事件更容易。 我希望我能理解你的问题。

此示例显示了一些事件技术:

  • 等待线程已经开始。
  • 使用等待多个事件(WaitHandle.WaitAny())
  • 如何终止线程,安全。
  • 测试事件状态而不等待(。WaitOne(0))

以下是一个例子:

 <?xml version="1.0" encoding="UTF-8"?>
     <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0   http://maven.apache.org/xsd/maven-4.0.0.xsd">
         <modelVersion>4.0.0</modelVersion>
         <packaging>war</packaging>

          <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.2.0.RELEASE</version>
            <relativePath/>
            <!-- lookup parent from repository -->
        </parent>

        <groupId>ssh-on-web</groupId>
        <artifactId>ssh-on-web</artifactId>
        <version>1.0-SNAPSHOT</version>

        <properties>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>

        <dependencies>
            <dependency>
                <groupId>com.hierynomus</groupId>
                <artifactId>sshj</artifactId>
                <version>0.11.0</version>
            </dependency>
            <dependency>
                <groupId>net.sf.expectit</groupId>
                <artifactId>expectit-core</artifactId>
                <version>0.6.1</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-websocket</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.el</groupId>
                <artifactId>javax.el-api</artifactId>
                <version>2.2.4</version>
            </dependency>
        </dependencies>

        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>

                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.2</version>
                    <!-- nothing here -->
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>2.2-beta-4</version>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <archive>
                            <manifest>
                                <mainClass>com.kodcu.ApplicationStarter</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>

        </build>


    </project>