I triedand its still throwing the same error.Code:Client c = clinet.GetClients().firstOrDefault();
What I have:If i could find a way to re write the tick part of expTimer.Tick += (sender, e) => { expTextBox.Text = (P.Experience.ToString()); };Code:public Form1() { InitializeComponent(); Client c = Client.GetClients().FirstOrDefault(); Player P; P = c.GetPlayer(); int ms = 1000; System.Windows.Forms.Timer expTimer = new System.Windows.Forms.Timer(); expTimer.Tick += (sender, e) => { expTextBox.Text = (P.Experience.ToString()); }; expTimer.Interval = ms; expTimer.Start();
the P.Experience.ToString()); is part of the cause of the error
Don't forget to include using System.Threading;
Code:public Form1() { InitializeComponent(); Client c = Client.GetClients().FirstOrDefault(); Player P = c.GetPlayer(); new Thread(new ThreadStart( delegate { while (true) { this.Invoke(new ThreadStart( delegate { expTextBox.Text = P.Experience.ToString(); } )); Thread.Sleep(1000); } })).Start(); }
Last edited by ottizy; 03-09-2013 at 09:28 PM.
Dude, that's horrible...
Note that you'll have to kill all threads when exiting your application, personally I use Environment.Exit(Environment.ExitCode) in the FormClosing event.Code:new Thread(delegate() { // try-catch, otherwise the entire program will crash if the thread crashes try { while (true) { // skip string.Format if you don't want to make it pretty expTextBox.Invoke(delegate() { expTextBox.Text = string.Format("{N}", P.Experience); }); Thread.Sleep(1000); } } catch { } }).Start();
Anyway, timers are probably more suitable for you (OP), as they're friendlier to beginners.
Form1() is a constructor, you can start threads in constructors.Originally Posted by XtrmJash
Perhaps it was a bad example, its not even gonna work I see now, corrected some. I know a timer would be better and more user friendly but since it was already shown how to do that I thought I could show another way since he still isn't getting it to work.
Also, why would you need need a try block? It shouldn't raise any exceptions?
Last edited by ottizy; 03-09-2013 at 09:25 PM.
My bet goes to: he's using some ot fully edited (maybe not even a tibia client, like OTClient, that Tibia's class name is not matching) or 'logged in' constant is wrong. As I took a look in the tibia api source and it should be the possible bugs, because I'm pretty sure my timer example works and he had a post explaining an "NotLoggedInException".
Never run code in non-UI threads without a try-catch block, unless you are absolutely sure it won't raise exceptions. The only time I don't run code in try-catch blocks is when I create local variables (I instantiate them inside the try block) that needs to be accessed in catch or finally.
There are several exceptions that may be raised, i.e. ThreadAbortException or NullReferenceException (expTextBox, P or P.Experience being null).