Post: [VB.NET][C#] Multi-thread / Crossthreading Example
07-31-2013, 03:31 PM #1
Default Avatar
Oneup
Guest
(adsbygoogle = window.adsbygoogle || []).push({}); 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

    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#

    
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;

}



}
}
}



You must login or register to view this content.

You must login or register to view this content.
Last edited by Oneup ; 08-04-2013 at 09:05 AM.

The following user thanked Oneup for this useful post:

The Epic
07-31-2013, 03:34 PM #2
The Epic
I wont stop
=3
You're awesome
07-31-2013, 05:37 PM #3
BLiNDzZ
Who’s Jim Erased?
Originally posted by UP View Post
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


    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
' Code wasn't working in the threading sub
' Code wasn't working in the threading sub
' Code wasn't working in the threading sub
' Code wasn't working in the threading sub
' Code wasn't working in the threading sub

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




C#

    
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;

}



}
}
}



You must login or register to view this content.

You must login or register to view this content.

Maybe you can help me on skype, i PMed you
08-12-2013, 04:46 PM #4
Shebang
Bring back the smileys!
Nice work on the tutorial, good for new users as oppose to them turning off checking for illegal cross-thread calls.

The following user thanked Shebang for this useful post:

Pichu

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo