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

  • Hello Guest! Welcome to ConsoleCrunch, to help support our site check out the premium upgrades HERE! to get exclusive access to our hidden content.
  • Unable to load portions of the website...
    If you use an ad blocker addon, you should disable it because it interferes with several elements of the site and blocks more than just adverts.
  • Read Rules Before Posting Post Virus Scans with every program.

1UP

Member
Oct 9, 2014
24
10
63
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

Code:
Imports System.Threading
Public Class Form1

  Dim Max As Double = 600000
  Dim i As Integer = 0
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  ProgressBar1.Maximum = Max
  Dim threadFill As Thread = New Thread(AddressOf Thread_FillProgressbar)
  threadFill.Start()
  End Sub


  Private Sub Thread_FillProgressbar()

  For i = 0 To Max
  AccessControl()
  Next
  End Sub

  Private Sub AccessControl()
  If Me.InvokeRequired Then
  Me.Invoke(New MethodInvoker(AddressOf AccessControl))
  Else


  ProgressBar1.Value = i
  ShowInTaskbar = True
  End If
  End Sub
End Class

C#

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace CSharp_multithreading
{
  public partial class Form1 : Form
  {
  public Form1()
  {
  InitializeComponent();
  }

  int Max = 60000;
  int i = 0;
  private void Form1_Load(object sender, EventArgs e)
  {
  progressBar1.Maximum = Max;
  Thread threadFill = new Thread(Thread_FillProgressBar);
  threadFill.Start();
  }

  private void Thread_FillProgressBar()
  {
  for (i = 0; i <= Max; i++)
  {
  accessControl();
  }
  }

  private void accessControl()
  {
  if (InvokeRequired)
  {

  Invoke(new MethodInvoker(accessControl));

  }
  else
  {
  progressBar1.Value = i;
  ShowInTaskbar = true;

  }



  }
  }
}


VB.NET Source

C# Source
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Chat Bot:
    Frnkln is our newest member. Welcome!
  • Chat Bot:
    ssabol1637 is our newest member. Welcome!
  • Chat Bot:
    rebugdex_4.75 has joined the room.
  • Chat Bot:
    QM|T has joined the room.
  • Chat Bot:
    waltni42 is our newest member. Welcome!
  • Chat Bot:
    Christo has joined the room.
  • Chat Bot:
    rr88bvcom is our newest member. Welcome!
  • Chat Bot:
    pam has posted a new reply in the thread "Site upgrade 06/12/2022".
  • Chat Bot:
    8kbet8org is our newest member. Welcome!
  • Chat Bot:
    cakhiazvn is our newest member. Welcome!
  • Chat Bot:
    tintuccoca is our newest member. Welcome!
  • Chat Bot:
    rr88tvcom2 is our newest member. Welcome!
  • Chat Bot:
    sc88plus12 is our newest member. Welcome!
  • Chat Bot:
    Christo has joined the room.
  • Chat Bot:
    rr88iocom1 is our newest member. Welcome!
  • Chat Bot:
    ps3gamer68 is our newest member. Welcome!
  • Chat Bot:
    Herrybon is our newest member. Welcome!
  • Chat Bot:
    Herrybon has started a new thread called "New member here!" in New Member Introductions.
  • @ Herrybon:
    Hi guys! New member here, excited to join the community!
  • Chat Bot:
    QM|T has joined the room.
  • Chat Bot:
    QM|T has posted a new reply in the thread "New member here!".
  • Chat Bot:
    kqbdbz is our newest member. Welcome!
  • Chat Bot:
    dodge358 is our newest member. Welcome!
  • Chat Bot:
    Christo has joined the room.
  • Chat Bot:
    xxx88eucom is our newest member. Welcome!
      Chat Bot: xxx88eucom is our newest member. Welcome!