Activity Stream
48,167 MEMBERS
6890 ONLINE
besthostingforums On YouTube Subscribe to our Newsletter besthostingforums On Twitter besthostingforums On Facebook besthostingforums On facebook groups

Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Hybrid View

litewarez [C#] Thread Safe in - How to? 5th Jul 2010, 06:50 PM
Hyperz Your form runs on a different... 5th Jul 2010, 06:57 PM
litewarez Ok so because its an event the... 5th Jul 2010, 07:01 PM
Hyperz No. Whatever is calling your method... 5th Jul 2010, 07:08 PM
litewarez Okay. For your information heres... 5th Jul 2010, 07:22 PM
Hyperz lambda + yourForm.Invoke() =... 5th Jul 2010, 07:53 PM
litewarez With your code i still get a few... 5th Jul 2010, 07:57 PM
Hyperz Whoops var a = new Action(()... 5th Jul 2010, 08:00 PM
litewarez OK that seemed to have fixed the... 5th Jul 2010, 08:15 PM
Hyperz What's confusing about that? 5th Jul 2010, 10:10 PM
litewarez whats confusing me is the way you... 6th Jul 2010, 06:47 AM
Hyperz C# is a 110% OO language, unlike... 6th Jul 2010, 12:17 PM
jayfella "Thread cannot access object in... 6th Jul 2010, 02:50 PM
Previous Post Previous Post   Next Post Next Post
  1.     
    #1
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com

    Default [C#] Thread Safe in - How to?

    heya guys

    Im working on a C# Application and im having some issues with the ThreadSafe thingimabob.

    When i try execute the a command i get the Thread not safe.

    heres my Code!

    PHP Code: 
    using System;
    using System.Threading;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using ComponentFactory.Krypton.Toolkit;
    using System.Windows.Forms;

    namespace 
    XXXXX
    {
        public 
    partial class Login KryptonForm
        
    {

            public 
    Login()
            {
                
    InitializeComponent();
            }

            private 
    void Login_Load(object senderEventArgs e)
            {
                
    //Hide Elements
                
    this.login_progress_bar.Visible false;
            }

            private 
    void ExitApp_Click(object senderEventArgs e)
            {
                
    this.Close();
            }

            private 
    void doLogin(object senderEventArgs e)
            {
                
    string u login_username.Text;
                
    string p login_password.Text;

                
    //Check for default values

                
    if (u.Length == || p.Length == 0)
                {
                    
    this.SetLoginStatus("Please fill out all login feilds");
                    return;
                }
                
    this.LoginEnabled(false);
               
    /*
                          Removed all execution code here because its private application :) 
               */
            
    }

            
    //Handlers



            /*I Run the this.SetLoginStatus witch is located below, but it strikes an unsafe thread error*/
            
    private void j_XError(object senderException ex) { this.SetLoginStatus("Incorrect Username or Password");}




            private 
    void j_XAuth(object sender){/*ShowDialog("Authenticated: " + sender.ToString());*/}
            private 
    void j_XWrite(object senderstring txt){if (txt == " ") return;/*ShowDialog("SEND: " + txt);*/}
            private 
    void j_XRead(object senderstring txt){if (txt == " ") return;/*ShowDialog("RECV: " + txt);*/}
          
            
    //Dialog Box
            
    private void ShowDialog(string Message){MessageBox.Show(Message);}

            private 
    void SetLoginStatusTest(string message)
            {
                
    label1.Text message;
            }

            private 
    void SetLoginStatus(string message)
            {
                
    label1.Text message;
            }

            private 
    void LoginEnabled(bool status
            {
                
    login_username.Enabled login_password.Enabled login_button.Enabled status;
            }

            
    //Menu Actions
            
    private void OptionsMenuOpen(object senderEventArgs e)
            {
                
    Settings Settings = new Settings();
                
    Settings.Show();
            }
        }

    Ok so i have j_XError above witch calls the SetLoginStatus But i get an error about not being Thread Safe.

    Can someone explain to me whats going in
    litewarez Reviewed by litewarez on . [C#] Thread Safe in - How to? heya guys Im working on a C# Application and im having some issues with the ThreadSafe thingimabob. When i try execute the a command i get the Thread not safe. heres my Code! using System; Rating: 5
    Join Litewarez.net today and become apart of the community.
    Unique | Clean | Advanced (All with you in mind)
    Downloads | Webmasters


    Notifications,Forum,Chat,Community all at Litewarez Webmasters


  2.   Sponsored Links

  3.     
    #2
    Respected Developer
    Your form runs on a different thread so you can't call members just like that. If it's a normal winforms application you can use the Invoke method.

    For example:
    Code: 
    // some code running in a new thread
    // change the text of a label on a form:
    this.Invoke(() => this.lblTest.Text = "my string");
    http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx

  4.     
    #3
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com
    Ok so because its an event the event is being executed by another object from another name-space and therefore it cant cross thread ?
    Join Litewarez.net today and become apart of the community.
    Unique | Clean | Advanced (All with you in mind)
    Downloads | Webmasters


    Notifications,Forum,Chat,Community all at Litewarez Webmasters


  5.     
    #4
    Respected Developer
    No. Whatever is calling your method is doing so from a thread that is not the GUI thread. It has little to do with objects or namespaces. It can't help you any further since you use non standard controls.

  6.     
    #5
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com
    Okay.

    For your information heres wahts going on

    I have a library that's i use to contact a Jabber Server, and i Assign callbacks / events for Successful Connection, Login Failed etc etc... Its the methods that i tell it to call that has has problem.

    What ive done is create a separate class to hold the Methods that will be called on the event attached to it... I have to learn about the threading to get the Callbacks class to be able to modify the the Form class.. I think

    So now i have defined my callbacks Class in my Form Class like so:

    PHP Code: 
    private XXXX.JabberCallbacks.JabberCallbacks _JabberCallbacks = new XXXX.JabberCallbacks.JabberCallbacks(); 
    And then when the login button is click it will run a method within the login Class, After basic input validation i assign the callbacks like So

    PHP Code: 
    Jabber.OnError += new bedrock.ExceptionHandler(_JabberCallbacks.OnError); 
    So that _JabberCallbacks.OnError will be called if an error accrues.

    On the _JabberCllbacks class heres the OnError Method:

    PHP Code: 
    public void OnError(object senderException ex)
            {
                  
    //Here i need to be able to set a Label in the Login Form :/
            

    Join Litewarez.net today and become apart of the community.
    Unique | Clean | Advanced (All with you in mind)
    Downloads | Webmasters


    Notifications,Forum,Chat,Community all at Litewarez Webmasters


  7.     
    #6
    Respected Developer
    lambda + yourForm.Invoke() = solution
    As I said in the first post .

    So, this
    PHP Code: 
            private void SetLoginStatus(string message)
            {
                
    label1.Text message;
            } 
    becomes
    PHP Code: 
            private void SetLoginStatus(string message)
            {
                
    this.Invoke(() => this.label1.Text message);
            } 
    Also, give your controls meaningfull names lol. lblTitle > label1. txtPassword > textbox1.

  8.     
    #7
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com
    With your code i still get a few errors witch im unsure of:

    Code: 
    Error	1
    Cannot convert lambda expression to type 'System.Delegate' because it is not a delegate type
    C:\Users\XXXX\documents\visual studio 2010\Projects\XXXX\XXXX\Login.cs
    Edit, I Also changed the Private to Public so i can call it from another Class
    Join Litewarez.net today and become apart of the community.
    Unique | Clean | Advanced (All with you in mind)
    Downloads | Webmasters


    Notifications,Forum,Chat,Community all at Litewarez Webmasters


  9.     
    #8
    Respected Developer
    Whoops

    PHP Code: 
    var = new Action(() => this.label1.Text message);
    this.Invoke(a); 

  10.     
    #9
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com
    OK that seemed to have fixed the errors but im confused about whats going on!


    Ok so i have this line:
    PHP Code: 
    //From the main form:DoLogin()
    Jabber.OnError += new bedrock.ExceptionHandler(_JabberCallbacks.OnError); 
    Witch then will run the following method:

    PHP Code: 
    public void OnError(object senderException ex)
    {
        
    //Here i want to run the follwing
        
    Login.SetLoginStatus("Error: " ex.ToString());

       
    /*
            The line above strings the following error!
            An object reference is required for the non-static field, method, or property 'XXXX.Login.SetLoginStatus(string)'

       */


    Witch is located in the main form
    PHP Code: 
    public void SetLoginStatus(string message)
    {
        var 
    = new Action(() => this.label1.Text message);
        
    this.Invoke(a);

    Join Litewarez.net today and become apart of the community.
    Unique | Clean | Advanced (All with you in mind)
    Downloads | Webmasters


    Notifications,Forum,Chat,Community all at Litewarez Webmasters


  11.     
    #10
    Respected Developer
    What's confusing about that?

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 23rd May 2012, 07:46 AM
  2. Replies: 2
    Last Post: 13th Nov 2011, 12:23 PM
  3. Thread Closing & Thread Titles
    By Loget in forum News and Announcements
    Replies: 1
    Last Post: 6th Sep 2011, 07:22 AM
  4. can post new thread or reply on thread
    By [AcE] in forum Technical Help Desk Support
    Replies: 3
    Last Post: 25th May 2011, 06:07 AM
  5. Which Is Safe PP/LR/MB
    By EsotorisK in forum Other
    Replies: 34
    Last Post: 13th Feb 2010, 02:30 PM

Tags for this Thread

BE SOCIAL