Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
C#
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
using TelegramBotSharp;
using TelegramBotSharp.Types;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public static TelegramBot bot;
        public Form1()
        {
            InitializeComponent();
        }

        private  void button1_Click(object sender, EventArgs e)
        {
            bot = new TelegramBot("146900545:*****************-gUjWMwZ7-FyoSPg");
            new Task(PollMessages).Start();

        }

         async void PollMessages()
        {
            while (true)
            {
                var result = await bot.GetMessages();
                foreach (TelegramBotSharp.Types.Message m in result)
                {
                    if (m.Text == null) return;
                    MessageTarget target = (MessageTarget)m.Chat ?? m.From;
                    bot.SendMessage(target, m.Text);
////////this error///////////////////////
                    label1.Text =m.Text;
/////////////////////////////////////////
                }

            }
        }

    }

}


What I have tried:

i canot do this ( label1.Text =m.Text; )
Posted
Updated 25-Apr-16 23:31pm
v2
Comments
Jochen Arndt 26-Apr-16 5:03am    
What is label1 (it is nowhere defined in your code snippet)?

What is the exact error message?
vahidsj2 26-Apr-16 5:09am    
it is a user control that i added it from tool box
it can be any user control
Jochen Arndt 26-Apr-16 5:23am    
Use the Reply button right of a name to answer to a comment. Then the poster will get an email notification. So I saw your answer just by chance.

I had a look at your code and did not saw any definition for 'label1'. The compiler is doing something similar:
It will search for the definition and throw an error if there is none.

Therefore, I asked for the exact error message. It will help to find the problem.
phil.o 26-Apr-16 6:08am    
You are stating there is an error but you do not indicate what is the error message. How are we supposed to deal with that?

Controls can only be manipulated by the UI thread. If you want to change something about a control from a different thread then you have to invoke the action that should be performed on the control on the UI thread:
C#
label1.Invoke((Action)(() => { label1.Text = m.Text; }));
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Apr-16 10:46am    
5ed.
—SA
Sascha Lefèvre 26-Apr-16 11:17am    
Thank you, Sergey.
BillWoodruff 26-Apr-16 23:09pm    
+5
Sascha Lefèvre 27-Apr-16 7:10am    
Thank you, Bill!
First check the control you name exists. If it doesn't, it'll be underlined in red and you'll get an error: "The name 'label1' does exist in the current context"
Make sure you added the control to the right Form, and that it's name has not been changed via the Properties Pane.

But even when you fix that, it won't work: you are setting the value of a string from within a loop - so it will only ever show the last string you assign. If you want to show multiple strings, then try this:
C#
StringBuilder sb = new StringBuilder();
foreach (TelegramBotSharp.Types.Message m in result)
    {
    if (m.Text == null) return;
    MessageTarget target = (MessageTarget)m.Chat ?? m.From;
    bot.SendMessage(target, m.Text);
    sb.AppendLine(m.Text);
    }
label1.Text = sb.ToString();


But even when you fix that...it won't work, because you are trying to access a UI control from a different thread from that which you created it on - and that's not allowed.
Instead, you have to Invoke it: Control.Invoke Method (Delegate) (System.Windows.Forms)[^]
I'd be tempted to suggest that you might be better off using a BackgroundWorker and passing messages to display via the ProgressChanged event and displaying them in your UI thread directly.
 
Share this answer
 
Comments
BillWoodruff 26-Apr-16 23:10pm    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900