Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

stop a thread c#

/* Below is a basic thread.
 * Since Thread.Abort() is no longer supported, you can use the following
 * as a replacement.
 * First create a global bool variable, I named my "END" and set it to: false
 * When you want to kill/abort your thread, write: END = true;
 */

bool END = false;
Thread thread = new Thread(() =>
{
  int x = 1;
  Thread.CurrentThread.IsBackground = true;

  // this thread will print the elapsed seconds until the END variable is: true
  while (true)
  {
    if (END) return; // terminates the thread
  	Thread.Sleep(1000);
  	Console.Writeline($"{x} seconds have passed");
  }
}).Start();

// the thread will run until this line is read:
END = true;
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# string.join 
Csharp :: get hard disk serial number 
Csharp :: how to loop over array in c# 
Csharp :: unique items in list c# 
Csharp :: c# compile code at runtime 
Csharp :: maximize window c# console 
Csharp :: how to set up blender with unity units 
Csharp :: c# filter list 
Csharp :: prevent page refresh 
Csharp :: how to use distinct in linq query in c# 
Csharp :: c# dictionary loop key value 
Csharp :: csharp 
Csharp :: c# run loop x times 
Csharp :: how to make a custom cursor in windows forms c# 
Csharp :: git find commits by message 
Csharp :: c# get getter set setter method 
Csharp :: c# using file.io 
Csharp :: system.drawing.color from hex 
Csharp :: convert object to array in c# 
Csharp :: C# Unit test IConfiguration 
Csharp :: c# console wait for input 
Csharp :: c# field vs property 
Csharp :: selection sort in c# 
Csharp :: how to check if a path is a directory or file c# 
Csharp :: wpf make size fill all grid 
Csharp :: Unity Children Destroy 
Csharp :: Net.ServicePointManager.SecurityProtocol .net framework 4 
Csharp :: No migrations configuration type was found in the assembly 
Csharp :: particle system start color 
Csharp :: calculate distance using latitude and longitude c# 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =