Results 1 to 8 of 8
  1. #1
    Member
    Join Date
    Nov 2010
    Posts
    74
    Points
    138
         User Info     Contact     Gamer ID
    Join Date
    Nov 2010
    Posts
    74
    Points
    138

    Default [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

    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

    0 Not allowed! Not allowed!

  2. #2
    ($user_isGay == TRUE)? giveAids() : break; aids's Avatar

    Badge

    Join Date
    Jul 2008
    Location
    /dev/null
    Posts
    20,361
    Points
    27,190
         User Info     Contact     Gamer ID
    Join Date
    Jul 2008
    Location
    /dev/null
    Posts
    20,361
    Points
    27,190
    Gamer IDs

    PSN ID: TheOriginalVirus

    Contact info:

    Youtube Channel: http://www.youtube.com/riottape Kik Username: aids gtalk account: codeleakers.com@gmail.com

    Default

    are you interested in programming for the site? if so, and you're serious PM me.

    0 Not allowed! Not allowed!
    BTC: 1AidsG8MHF4cEN6KpY1mhJPBhXtms65GVE

  3. #3
    The Quiet One. 0zzy's Avatar
    Join Date
    Apr 2010
    Posts
    4,171
    Points
    7,514
         User Info     Contact     Gamer ID
    Join Date
    Apr 2010
    Posts
    4,171
    Points
    7,514

    Default

    Your back??

    0 Not allowed! Not allowed!
    There's a difference between us. You think the people of this country exist to provide you with position. I think your position exists to provide those people with freedom. And I go to make sure that they have it.

    Free bitcoin's referal link

    Site BBcodes

  4. #4
    Member
    Join Date
    Nov 2010
    Posts
    74
    Points
    138
         User Info     Contact     Gamer ID
    Join Date
    Nov 2010
    Posts
    74
    Points
    138

    Default

    Quote Originally Posted by 0zzy View Post
    Your back??
    Hope that's not a bad thing :/

    0 Not allowed! Not allowed!

  5. #5
    Member
    Join Date
    Nov 2010
    Posts
    74
    Points
    138
         User Info     Contact     Gamer ID
    Join Date
    Nov 2010
    Posts
    74
    Points
    138

    Default

    Quote Originally Posted by aids View Post
    are you interested in programming for the site? if so, and you're serious PM me.
    Well I pmed you a while back but have not heard back from you.

    0 Not allowed! Not allowed!

  6. #6
    Desu Development
    Pomf pomf :3
    Fuck Sky's Avatar
    Join Date
    Sep 2013
    Location
    WOW ANIME MAN WOW
    Posts
    1,137
    Points
    1,687
         User Info     Contact     Gamer ID
    Join Date
    Sep 2013
    Location
    WOW ANIME MAN WOW
    Posts
    1,137
    Points
    1,687
    Gamer IDs

    Gamertag: Hexpresso PSN ID: WideDongWeary Steam ID: twitchhexpresso

    Contact info:

    Youtube Channel: http://www.youtube.com/ Kik Username: Wownicehack

    Default

    Question: You clearly aren't stupid, so why are you using .NET? To quote Steve Jobs, I don't care that Microsoft's successful, I care that they don't have any of their own ideas.


    C# is basically Java which can only be ran on Windows(while stable at least).

    0 Not allowed! Not allowed!
    geadszf1231qewrg

  7. #7
    Member
    Join Date
    Nov 2010
    Posts
    74
    Points
    138
         User Info     Contact     Gamer ID
    Join Date
    Nov 2010
    Posts
    74
    Points
    138

    Default

    Quote Originally Posted by Anne Gank View Post
    Question: You clearly aren't stupid, so why are you using .NET? To quote Steve Jobs, I don't care that Microsoft's successful, I care that they don't have any of their own ideas.


    C# is basically Java which can only be ran on Windows(while stable at least).
    If I am going to be using an interpreted language, I'd rather it not be bloated. You do anything really worth while in java and it just eats resources.

    0 Not allowed! Not allowed!

  8. #8
    σиє ℓιfє. ℓινє ιт. C0D-FR34K's Avatar
    Join Date
    Aug 2011
    Location
    Heaven and Hell
    Posts
    1,444
    Points
    920
         User Info     Contact     Gamer ID
    Join Date
    Aug 2011
    Location
    Heaven and Hell
    Posts
    1,444
    Points
    920
    Gamer IDs

    PSN ID: xC0D--FR34Kx Steam ID: c0d-fr34k

    Default

    Quote Originally Posted by 0zzy View Post
    Your back??
    This is what I was thinking. I knew I had seen his name before.

    0 Not allowed! Not allowed!

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -10. The time now is 05:25 PM.
Powered by vBulletin®
Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.