Results 1 to 10 of 13
Hybrid View
-
5th Jul 2010, 06:50 PM #1OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.com[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 sender, EventArgs e)
{
//Hide Elements
this.login_progress_bar.Visible = false;
}
private void ExitApp_Click(object sender, EventArgs e)
{
this.Close();
}
private void doLogin(object sender, EventArgs e)
{
string u = login_username.Text;
string p = login_password.Text;
//Check for default values
if (u.Length == 0 || 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 sender, Exception ex) { this.SetLoginStatus("Incorrect Username or Password");}
private void j_XAuth(object sender){/*ShowDialog("Authenticated: " + sender.ToString());*/}
private void j_XWrite(object sender, string txt){if (txt == " ") return;/*ShowDialog("SEND: " + txt);*/}
private void j_XRead(object sender, string 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 sender, EventArgs e)
{
Settings Settings = new Settings();
Settings.Show();
}
}
}
Can someone explain to me whats going inlitewarez 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: 5Join 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
-
5th Jul 2010, 06:57 PM #2Respected 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");
-
5th Jul 2010, 07:01 PM #3OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comOk 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
-
5th Jul 2010, 07:08 PM #4Respected 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.
-
5th Jul 2010, 07:22 PM #5OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comOkay.
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();
PHP Code:Jabber.OnError += new bedrock.ExceptionHandler(_JabberCallbacks.OnError);
On the _JabberCllbacks class heres the OnError Method:
PHP Code:public void OnError(object sender, Exception 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
-
5th Jul 2010, 07:53 PM #6Respected Developer
lambda + yourForm.Invoke() = solution
As I said in the first post.
So, this
PHP Code:private void SetLoginStatus(string message)
{
label1.Text = message;
}
PHP Code:private void SetLoginStatus(string message)
{
this.Invoke(() => this.label1.Text = message);
}
-
5th Jul 2010, 07:57 PM #7OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comWith 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
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
-
5th Jul 2010, 08:00 PM #8Respected Developer
Whoops
PHP Code:var a = new Action(() => this.label1.Text = message);
this.Invoke(a);
-
5th Jul 2010, 08:15 PM #9OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comOK 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);
PHP Code:public void OnError(object sender, Exception 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 a = 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
-
5th Jul 2010, 10:10 PM #10Respected Developer
What's confusing about that?
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
there is more then 1 million thread in a forum but showing 0 thread 0 post
By anandimhere in forum vBulletinReplies: 2Last Post: 23rd May 2012, 07:46 AM -
Specific Wordpress thread is taking me to my main website instead of that thread
By happyvalentine in forum WordpressReplies: 2Last Post: 13th Nov 2011, 12:23 PM -
Thread Closing & Thread Titles
By Loget in forum News and AnnouncementsReplies: 1Last Post: 6th Sep 2011, 07:22 AM -
can post new thread or reply on thread
By [AcE] in forum Technical Help Desk SupportReplies: 3Last Post: 25th May 2011, 06:07 AM -
Which Is Safe PP/LR/MB
By EsotorisK in forum OtherReplies: 34Last Post: 13th Feb 2010, 02:30 PM
themaLeecher - leech and manage...
Version 5.03 released. Open older version (or...