I have the following code to send a query to youtube and send the total result to a textbox. If I just alert the result, it's OK but I can't assign the result to a textbox. Please explain to me why?
private void SearchVideo(string keyword)
{
string orderBy = "";
switch (cboSortBy.SelectedIndex)
{
case 1: orderBy = "published"; break;
case 2: orderBy = "viewCount"; break;
case 3: orderBy = "rating"; break;
default: orderBy = "relevance"; break;
}
SearchDelegate sd = Search;
sd.BeginInvoke(keyword, orderBy, SearchCompleted, sd);
}
private void SearchCompleted(IAsyncResult ar)
{
if (null == ar) return;
SearchDelegate sd = ar.AsyncState as SearchDelegate;
Feed<Video> result = sd.EndInvoke(ar);
txtSearch.Text = result.TotalResults.ToString();
}
private Feed<Video> Search(string keyword, string orderBy)
{
YouTubeQuery query = new YouTubeQuery(YouTubeQuery.DefaultVideoUri);
query.OrderBy = orderBy;
query.Query = keyword;
query.SafeSearch = YouTubeQuery.SafeSearchValues.None;
return GetRequest().Get<Video>(query);
}
And the error
Cross-thread operation not valid: Control 'txtSearch' accessed from a thread other than the thread it was created on.