Improving the user interface in Windows Forms Client with low Internet speed and using the WCF web service located far

I have a live WCF service hosted in the USA. I want to access it from a Windows Forms application that can be used from anywhere in the world. I am currently testing it in India and am trying to access a web service hosted in the USA. This application can be used with any connection speed and at any distance from the server. Thus, the response speed from the server can be different. I decided that when a user performs a function, my application will first update the interface and then complete the task of updating the server in the background. This will prevent the application from freezing or freezing. The following is one of these scenarios. But the problem is that even I use asynchronous functions and threads to update the interface and server, the application still freezes.

Actually, I have a button that acts like a switch between similar and dissimilar. When a user clicks on him, he must change unlike him, and then he starts a thread that updates the server in the background. The following is the button click event code:

async private void btnLike_Click(object sender, EventArgs e) { new System.Threading.Thread(new System.Threading.ThreadStart(changeLike)).Start(); await LikeContent(); } 

changeLike :

 private void changeLike() { if(btnLike.Text.Equals("Like")) btnLike.Text="Unlike"; else btnLike.Text="Like"; } 

LikeContent :

 async Task<int> LikeContent() { await Global.getServiceClient().addContentLikeAsync(cid, uid); System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(reloadLikes)); t.Start(); return 0; } 

addContentLikeAsync() function is a WCF web service function that updates the user on the server. reloadLikes() updates the number of likes from the server after the user liked the content.

Please tell me, how can I change my code so that the application instantly updates the LIKE button instead of freezing for some time, and then updates the LIKE button? Because it "once" will create a bad impression on users with lower Internet speeds.

+4
source share
1 answer

First, I assume that the btnLike_Click method btnLike_Click called in the user interface thread. In this case, do not call changeLike() on a separate thread.

 async private void btnLike_Click(object sender, EventArgs e) { changeLike(); await LikeContent(); } 

Second, remember to return to the user interface thread before reloadLikes()

 async Task<int> LikeContent() { await Global.getServiceClient().addContentLikeAsync(cid, uid); // Ensures this occurs on the UI thread - WinForms Invoke((Action)reloadLikes); return 0; } 
+3
source

Source: https://habr.com/ru/post/1499756/


All Articles