Deprecated: The behavior of unparenthesized expressions containing both '.' and '+'/'-' will change in PHP 8: '+'/'-' will take a higher precedence in /home/iano/public_html/tpforums-vb5/forum/includes/class_core.php on line 5842

PHP Warning: Use of undefined constant MYSQL_NUM - assumed 'MYSQL_NUM' (this will throw an Error in a future version of PHP) in ..../includes/init.php on line 165

PHP Warning: Use of undefined constant MYSQL_ASSOC - assumed 'MYSQL_ASSOC' (this will throw an Error in a future version of PHP) in ..../includes/init.php on line 165

PHP Warning: Use of undefined constant MYSQL_BOTH - assumed 'MYSQL_BOTH' (this will throw an Error in a future version of PHP) in ..../includes/init.php on line 165

PHP Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in ..../includes/functions_navigation.php on line 588

PHP Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in ..../includes/functions_navigation.php on line 612

PHP Warning: Use of undefined constant archive_postsperpage - assumed 'archive_postsperpage' (this will throw an Error in a future version of PHP) in ..../archive/index.php on line 456
Thread handling [Archive] - Forums

PDA

View Full Version : Thread handling



tretus
07-16-2013, 07:13 PM
Hello everyone!
Firstly I say that I am new to that forum, and my English is not perfect, so if You don't understand something just pm me or answer to my Thread.
The thing is that I am new to C# too. My programming expierience includes programming in Java, Python, AS3 - so my point is that I know some staff etc, but I can't figure out handling threads in C#.
So here is my problem:
I've watched few tuts about reading memory etc, and I 've created a simple Tibia Manipulator - just to check how it works, before I go on depth water to start scripting cave bots. Here is picture of my work:
<img src="http://images50.fotosik.pl/2011/9d8e91af71cdff36.png" />

Now I want to add a thread, which will update coordinates and other staff let's say - every 1 second.
How to do this?
I've tried Timer.cs from TibiaApi but it didn't work.

XtrmJash
07-16-2013, 07:44 PM
Hello everyone!
Firstly I say that I am new to that forum, and my English is not perfect, so if You don't understand something just pm me or answer to my Thread.
The thing is that I am new to C# too. My programming expierience includes programming in Java, Python, AS3 - so my point is that I know some staff etc, but I can't figure out handling threads in C#.
So here is my problem:
I've watched few tuts about reading memory etc, and I 've created a simple Tibia Manipulator - just to check how it works, before I go on depth water to start scripting cave bots. Here is picture of my work:
<img src="http://images50.fotosik.pl/2011/9d8e91af71cdff36.png" />

Now I want to add a thread, which will update coordinates and other staff let's say - every 1 second.
How to do this?
I've tried Timer.cs from TibiaApi but it didn't work.

You'll want to add:


using System.Threading;

Then either use a Timer or Thread (from System.Threading class) to do things automatically. To use a timer:


Timer myTimer = new Timer(myTimer_Callback);

Then add a function:


myTimer_Callback(object state)
{
// Do shit here
}

You can change the delay by doing:


myTimer.Change(int TimeToNextCall, int TimeBetweenCalls);

E.g:


myTimer.Change(100, 1000);

This code will make the timer next run in 100ms, and after that it will run every 1000ms (1 second). Due to the nature of timers and threading, you may need to invoke GUI elements (if you plan to update the GUI in a thread which is not the GUI thread, that is), which will look something like this:


this.Dispatcher.Invoke(delegate
{
this.Title = "Disconnected";
MyLabel.Text = "MyLabelText";
});

Good luck! Any questions just ask :)

Edit:

To restart the timer, you'll need to dispose of the object:


myTimer.Dispose();

Then you'll need to declare it as a new instance:


myTimer = new Timer(myTimer_Callback);

Then of course you need to set the interval again... Not so complex, but something you may get stuck on if you're making a healer or such...

tretus
07-16-2013, 08:04 PM
this.Dispatcher.Invoke(delegate
{
this.Title = "Disconnected";
MyLabel.Text = "MyLabelText";
});

Good luck! Any questions just ask :)

Edit:

To restart the timer, you'll need to dispose of the object:


myTimer.Dispose();

Then you'll need to declare it as a new instance:


myTimer = new Timer(myTimer_Callback);

Then of course you need to set the interval again... Not so complex, but something you may get stuck on if you're making a healer or such...

I am getting cross-thread error ...
Where i should put this dispatcher?
Maybe i paste here some code:


public Form1()
{
InitializeComponent();
contr = new Control();
hpBox.Text = contr.getHP();
manaBox.Text = contr.getMana();
soulBox.Text = contr.getSoul();
levelBox.Text = contr.getLevel();
expBox.Text = contr.getExp();
System.Threading.Timer myTimer = new System.Threading.Timer(updateChar);
myTimer.Change(1000, 1000);
}

private void updateChar(object state)
{

xPosBox.Text = contr.getPosX();
yPosBox.Text = contr.getPosY();
zPosBox.Text = contr.getPosZ();

}

XtrmJash
07-16-2013, 08:07 PM
The dispatcher should go inside of your timers tick. In the dispatcher you should put any code which changes textboxes or labels, as they are only accessible to their own thread (GUI thread), and you are working in the timer thread.

tretus
07-16-2013, 08:18 PM
Hmm i'm getting new error :


Error 1 'SimpleManipulator.Form1' does not contain a definition for 'Dispatcher' and no extension method 'Dispatcher' accepting a first argument of type 'SimpleManipulator.Form1' could be found (are you missing a using directive or an assembly reference?)

I've tried to find Dispatcher class but i don't see it :(

XtrmJash
07-16-2013, 08:22 PM
Hmm i'm getting new error :


Error 1 'SimpleManipulator.Form1' does not contain a definition for 'Dispatcher' and no extension method 'Dispatcher' accepting a first argument of type 'SimpleManipulator.Form1' could be found (are you missing a using directive or an assembly reference?)

I've tried to find Dispatcher class but i don't see it :(

Change Dispatcher to System.Windows.Threading.Dispatcher, see if that works

Blequi
07-16-2013, 08:30 PM
Hmm i'm getting new error :


Error 1 'SimpleManipulator.Form1' does not contain a definition for 'Dispatcher' and no extension method 'Dispatcher' accepting a first argument of type 'SimpleManipulator.Form1' could be found (are you missing a using directive or an assembly reference?)

I've tried to find Dispatcher class but i don't see it :(


Change Dispatcher to System.Windows.Threading.Dispatcher, see if that works

As far as I know, Dispatcher is available for WPF projects only.

@tretus, if you are in winforms, use System.Windows.Forms.Timer to update your UI properly, if its your intent.

pater
07-16-2013, 08:30 PM
Use this



private void updateChar(object state)
{
this.Invoke(()=>
{
xPosBox.Text = contr.getPosX();
yPosBox.Text = contr.getPosY();
zPosBox.Text = contr.getPosZ();
});
}

tretus
07-16-2013, 08:47 PM
Okey It's finally working ! :)
Thanks for quick help!

Here is the final code( if someone spotted same problem )


public Form1()
{
InitializeComponent();
contr = new Control();
hpBox.Text = contr.getHP();
manaBox.Text = contr.getMana();
soulBox.Text = contr.getSoul();
levelBox.Text = contr.getLevel();
expBox.Text = contr.getExp();
System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
myTimer.Tick += new EventHandler(updateChar);
myTimer.Interval = 100;
myTimer.Start();
}




private void updateChar(Object myObject, EventArgs myEventArgs)
{
xPosBox.Text = contr.getPosX();
yPosBox.Text = contr.getPosY();
zPosBox.Text = contr.getPosZ();
}

Sketchy
07-16-2013, 08:51 PM
@tretus, if you are in winforms, use System.Windows.Forms.Timer to update your UI properly, if its your intent.

It should also be kept in mind that this particular timer runs on the window's thread via its message loop, it uses SetTimer in the background, and thus will block the UI while running. So it would only be advisable to use this timer when your code won't block the UI for any long period of time, if it might you should use the System.Threading timer and run any UI modification code through the (Begin)Invoke method of the form/control object.

XtrmJash
07-16-2013, 09:20 PM
Apologies, I'm too used to working with WPF, as opposed to WinForms.

@OP - I highly recommend using WPF, it will make things a lot easier for you in the long run :)

Blequi
07-16-2013, 09:27 PM
So it would only be advisable to use this timer when your code won't block the UI for any long period of time, if it might you should use the System.Threading timer and run any UI modification code through the (Begin)Invoke method of the form/control object.

I took a while to understand your point, but it makes sense if you have heavy tasks. I'll save this tip in my trap bag :o

tretus
07-17-2013, 08:55 AM
Apologies, I'm too used to working with WPF, as opposed to WinForms.

@OP - I highly recommend using WPF, it will make things a lot easier for you in the long run :)

Hmm how WPF will make things easier in long run?
I thought it's only advantage is more attractive design, but perhaps I'm wrong, because as I said I'm new to MS Technology :)

Blequi
07-17-2013, 12:01 PM
Hmm how WPF will make things easier in long run?
I thought it's only advantage is more attractive design, but perhaps I'm wrong, because as I said I'm new to MS Technology :)

winforms has no comparison with WPF regarding design capabilities, WPF is really flexible to modify UI design and if you use Blend (old expression blend) you'll see the difference still higher. If you don't care to design support at all, data binding in WPF is probably the best feature to cite.

Even winforms being capable of data binding in certain controls, as far as I know, winforms doesn't support data binding in controls like ListView (I suspect it doesn't support data binding in list-like controls as treeview, listbox, etc, but I didn't test... I did test only ListView).

WPF also splits UI behavior and its layout, it means you can work on code while your colleague (a designer guy) work on the visual aspect of the program. It leads to a pattern called MVVM (Model View ViewModel), which codes separately the Models (your code behavior), the Views (how the controls looks) and the ViewModels (data bindings between the Model and the View). I used to write winforms programs and it were a total mess. After I gave a start in WPF and learnt how to code in MVVM, my code became much more cleaner than before.

Many people say that WPF has poor performance. I'd say it is true when repainting some surface manually like you do in winforms, but you can often work around such things.

In short, I highlight design support + data bindings.

Honestly, I'd tell you to start with winforms to understand at least the basics of it, then when you get confortable with winforms, switch to WPF.

XtrmJash
07-17-2013, 12:42 PM
winforms has no comparison with WPF regarding design capabilities, WPF is really flexible to modify UI design and if you use Blend (old expression blend) you'll see the difference still higher. If you don't care to design support at all, data binding in WPF is probably the best feature to cite.

Even winforms being capable of data binding in certain controls, as far as I know, winforms doesn't support data binding in controls like ListView (I suspect it doesn't support data binding in list-like controls as treeview, listbox, etc, but I didn't test... I did test only ListView).

WPF also splits UI behavior and its layout, it means you can work on code while your colleague (a designer guy) work on the visual aspect of the program. It leads to a pattern called MVVM (Model View ViewModel), which codes separately the Models (your code behavior), the Views (how the controls looks) and the ViewModels (data bindings between the Model and the View). I used to write winforms programs and it were a total mess. After I gave a start in WPF and learnt how to code in MVVM, my code became much more cleaner than before.

Many people say that WPF has poor performance. I'd say it is true when repainting some surface manually like you do in winforms, but you can often work around such things.

In short, I highlight design support + data bindings.

Honestly, I'd tell you to start with winforms to understand at least the basics of it, then when you get confortable with winforms, switch to WPF.

I'll actually agree with that, WinForms is good for the whole "getting started thing", but when you know a bit, try out WPF, and you will see the benefits quickly. Its slightly more complex, but a lot more functional!

DarkstaR
07-17-2013, 02:34 PM
Blequi, I feel like you just gave me an excerpt from a book. Great explanation.