multi thread behavior

Jun 2, 2015 at 2:04 PM
Hi,

I am new into this subject, so maybe this is a basic question but I dont know the answer. I am having multithreads in my application but in this case the application is running very slowly. I think tweetinvi library is not sending making the commands asynchronously. All the commands coming to the library at the same time are being queued and operated one by one. Is this the actual behavior or can the commands be sent at the same time from different thread of applications?
Coordinator
Jun 2, 2015 at 4:33 PM
Hi,

I am not sure what is your problem here. Tweetinvi provides 2 set of methods, async and synchronous.
Now I have to admit that some of the methods does not have their async equivalent.

But if you need an operation to be run another why don't you just simply do so?
Task.Factory.StartNew(() =>
{
    var user = User.GetLoggedUser();
    Console.WriteLine(user);
});
Now you need to understand the credentials system between threads.
As you are probably aware each thread uses a distinct Credentials object.

It means that invoking TwitterCredentials.SetCredentials in one thread will not affect the other threads.
Though when a thread is created, Tweetinvi will initialize the ThreadCredentials to the value stored in TwitterCredentials.ApplicationCredentials.

Tweetinvi async methods are ThreadSafe. Now if you want to execute your code and ensure that your credentials are ThreadSafe it is a good idea to set the credentials at each start of a new Thread.
Task.Factory.StartNew(() =>
{
    // Ensuring the credentials information of the Thread
    TwitterCredentials.SetCredentials("", "", "", "");
    var user = User.GetLoggedUser();
    Console.WriteLine(user);
});
If you need more help concerning Multi-Threading I will need some piece of code.
Cheers,
Linvi
Coordinator
Jun 2, 2015 at 5:13 PM
Edited Jun 2, 2015 at 5:13 PM
Also,

I have reconsidered my position over the static Sync class.

In coming version of Tweetinvi (0.9.8.0) you will have access to a static class TSync.
With this class you will be able to execute any operation asynchronously without having to worry about ThreadSafe credentials.

Code you will be able to use in coming release :
// simple thread task
TSync.ExecuteTaskAsync(() =>
{
    var user = User.GetLoggedUser();
    Console.WriteLine(user);
});

// using await to join thread task and get result
var user = await TSync.ExecuteTaskAsync(() =>
{
    return User.GetLoggedUser();
});

Console.WriteLine(user);
Linvi
Jun 7, 2015 at 12:17 AM
This is more than enough for me. Thank you.