Code Monkey home page Code Monkey logo

dad-of-code / threadmanager Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 11 KB

Efficiently manage and control threading operations within your .NET applications using ThreadManager. This small library simplifies the creation, monitoring, and termination of threads, ensuring optimized performance and seamless thread lifecycle management.

Home Page: https://dadofcode.com

License: MIT License

Visual Basic .NET 50.62% C# 49.38%
cs-net threading threads vb-net

threadmanager's Introduction

ThreadManager Documentation

The ThreadManager class is designed to facilitate thread creation, management, stopping, and cleanup in a .NET application. This comprehensive guide will help you utilize the ThreadManager effectively in your projects.

Getting Started

Start the Thread Manager

Optimally initialize the ThreadManager object in your main form or, better yet, in a module so you can refer to it globally. This ensures that thread management is accessible throughout your application.

Public SystemThreadManager As New ThreadManager()

Create a Thread

To create a new thread, assign it a name and the address of the function that will run in the thread. This approach allows for easy identification and management of threads.

Example:

SystemThreadManager.AddThread("Thread1", new Thread(Sub() ThreadMethod()), New CancellationTokenSource())

Ensure to import the necessary namespaces at the beginning of your file:

Imports System.Collections.Concurrent
Imports System.Collections.Generic
Imports System.Net.Sockets
Imports System.Threading

Class Overview

Properties

  • threads: A concurrent dictionary that stores thread information, indexed by thread name.
  • threadDescriptions: A list that holds descriptions of all threads for display or logging purposes.

Methods

  • GetThreadsList(): Retrieves a list of thread descriptions, indicating whether each thread is alive.
  • AddThread(name As String, thread As Thread, cts As CancellationTokenSource) As Boolean: Adds a new thread with a specified name, thread object, and cancellation token source.
  • AddThread(name As String, action As Action) As Boolean: Overload for adding a thread by specifying an action to be executed in the thread.
  • StopThread(name As String): Attempts to stop a thread by its name, gracefully ending its execution if possible.
  • StopAllThreads(): Stops all threads managed by the ThreadManager, ensuring a clean shutdown of all background activities.

Usage Examples

Stopping a Thread

To stop a thread, simply call the StopThread method with the name of the thread you wish to stop:

SystemThreadManager.StopThread("Thread1")

Stopping All Threads

To ensure that all threads are properly stopped, especially upon application exit, use the StopAllThreads method:

SystemThreadManager.StopAllThreads()

Conclusion

The ThreadManager class provides a structured approach to managing threads within .NET applications, making it easier to start, monitor, and terminate threads as required. By following the guidelines and examples provided, you can effectively manage thread lifecycles and ensure your application runs smoothly and efficiently.

threadmanager's People

Contributors

dad-of-code avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.