[VB.NET][C#] Multi-thread / Crossthreading Example

Someone was asking how to do multi-threading in the shoutbox and my original example didn’t work because of cross-threads. This basically means I couldn’t set the value of a control within a thread since it’s considered a different process. Well I did manage to get it to work and I figured I’d post the code here. I will also include the source code both both the VB.net and C# versions below.

VB

  1. Imports System.Threading
  2. Public Class Form1
  3.  
  4. Dim Max As Double = 600000
  5. Dim i As Integer = 0
  6. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  7. ProgressBar1.Maximum = Max
  8. Dim threadFill As Thread = New Thread(AddressOf Thread_FillProgressbar)
  9. threadFill.Start()
  10. End Sub
  11.  
  12. Private Sub Thread_FillProgressbar()
  13.  
  14. For i = 0 To Max
  15. AccessControl()
  16. Next
  17. End Sub
  18.  
  19. Private Sub AccessControl()
  20. If Me.InvokeRequired Then
  21. Me.Invoke(New MethodInvoker(AddressOf AccessControl))
  22. Else
  23.  
  24. ProgressBar1.Value = i
  25. ShowInTaskbar = True
  26. End If
  27. End Sub
  28. End Class

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Threading;
  10. namespace CSharp_multithreading
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18.  
  19. int Max = 60000;
  20. int i = 0;
  21. private void Form1_Load(object sender, EventArgs e)
  22. {
  23. progressBar1.Maximum = Max;
  24. Thread threadFill = new Thread(Thread_FillProgressBar);
  25. threadFill.Start();
  26. }
  27.  
  28. private void Thread_FillProgressBar()
  29. {
  30. for (i = 0; i <= Max; i++)
  31. {
  32. accessControl();
  33. }
  34. }
  35.  
  36. private void accessControl()
  37. {
  38. if (InvokeRequired)
  39. {
  40.  
  41. Invoke(new MethodInvoker(accessControl));
  42.  
  43. }
  44. else
  45. {
  46. progressBar1.Value = i;
  47. ShowInTaskbar = true;
  48.  
  49. }
  50.  
  51. }
  52. }
  53. }

VB.NET Source

C# Source

VB.NET Detect Key Presses

This bit of code will allow you to detect key presses while the form is not the active window. I currently use this for macros and for my trainer programs for turning on cheats.

All you need to have is a timer on the form. You can either drag and drop one on the form or declare it in code and write the timer.Tick event.

In this example I check for 2 key presses.

  1. Dim altkey As Boolean
  2. Dim Num_1 As Boolean
  3. Dim Num_2 As Boolean
  4. Dim Num_3 As Boolean
  5.  
  6. Private Sub tmr_Keys_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr_Keys.Tick
  7. altkey = GetAsyncKeyState(Keys.LMenu)
  8. Num_1 = GetAsyncKeyState(Keys.NumPad1)
  9. Num_2 = GetAsyncKeyState(Keys.NumPad2)
  10. Num_3 = GetAsyncKeyState(Keys.NumPad3)
  11. If altkey And Num_1 = True Then 'Infinite Health Player 1
  12. btn_P1InfiniteHealth_Click(sender, e)
  13. ElseIf altkey And Num_2 = True Then
  14. btn_P1InfiniteKi_Click(sender, e) ' Infinite Ki Player 1
  15. ElseIf altkey And Num_3 = True Then
  16.  
  17. End If
  18. End Sub

VB.NET Setting an External Program’s Title window

I have been working on making some trainers for some games and I wanted a basic way of showing that a cheat was enabled.
I couldn’t figured out how to draw to the screen so I figured the title bar would be fine.

  1. Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hWnd As IntPtr, ByVal lpString As String) As Boolean
  2.  
  3. Public Sub SetTitleText(ByVal processName() As Process, ByVal textToSend As String)
  4.  
  5. For Each proc In processName
  6. SetWindowText(proc.MainWindowHandle, textToSend)
  7. Next
  8.  
  9. End Sub

The code above will allow you to set the title window to an external program.
An example on how to use it

  1. Dim notepad() As Process = Process.GetProcessesByName("notepad")
  2.  
  3. Private Sub btn_Notepad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Notepad.Click
  4. SetTitleText(notepad,"Insert some text here")
  5. End Sub

VB.NET Using a Binary File as a resource

This was something I used in order to try and keep my programs down to 1 file. Instead of including a seperate file that handled the updating, I simply embedded it into the main program. To do this all I did was make it a resource and when I needed to use it I would write it out to the root directory that the program was in.

To add a file as a resource, right click your project and click properties.

Click on the Resources tab. On the top click on strings and select files. Drag and drop the file you want to include as the resource into this window.

Click save or save all.

Now when you decide you need to use that file you can write it out to a directory then run it.

  1. IO.File.WriteAllBytes("./" & "Updater.exe",My.Resources.Updater)
  2. Process.Start("Updater.exe")

Taskmanager Plus FileRemoval Update

This was a feature I have been planning for some time and finally got around to working on it. Basically it will have default folders that will be in the program. These folders will be where your temp files are located. All you would have to do from here is click the clean button next to the item and it will remove those files.

You are also able to add your own folders that you wish to be included in the program. So anytime you start up the application you will get the list of folders and the amount of space they are taking up.

The design in the video isn’t what I plan on having it look like on release but it’s functionality will be the same.

Taskmanager Plus Update v 1.0.1

taskmanager
Version 1.0.1

-Added the ability to stop multiple processes at once. Simple control click the ones you want to stop and click End process.
-Forms have changed, new features will be added that deal with clean up on temp files and fixing other policies within windows

If you have the program already simply click file and check for updates. It will say an update was found click yes and then in the new windows click update. Or go to the download link in this post
Download

Hardware Profiler

This was something that I started maybe last year and well with work and other things I haven’t been able to really put work into it. As of now I finally got the windows portion of the program done and all that needs to be done is the web part.

The image below isn’t what the final product would look like, I just wanted to get it functional.
Hardware Profiler

Basic Video Poker

Basic Video Poker

How to play:

It’s pretty straight forward. Choose your bet (default is 10) and click Deal. Depending what you get will determine the payouts which are:

Pair = 1x bet
Two Pair = 2x bet
Three Of A Kind = 3x bet
Four Of A Kind = 25x bet
Straight = 5x bet
Flush = 6x bet
Full House = 10x bet
Straight Flush = 100x bet
Royal Flush = 250x Bet

Download