PDA

View Full Version : سوال: مشکل در رباط تلگرام



rezaAcc
یک شنبه 31 اردیبهشت 1396, 23:31 عصر
سلام و وقت بخیر
دوستان منبا یه رباط تلگرام نوشتم ولی برای وارد کردن متن ارسالی کاربر در تکست باکس ، به تکست باکسم ایراد میگیره و نمیإاره مقادیر رو در تکس باکس وارد کنم
اما در مسیج باکش ایرادی نمیگیره
ممنون میشم راهنماییم کنید


Telegram.Bot ورژن 10 استفاده کردم

اینم ارورش:




System.InvalidOperationException: 'Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on.'







System.InvalidOperationException occurred
HResult=0x80131509
Message=Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on.
Source=System.Windows.Forms
StackTrace:
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.set_WindowText(String value)
at System.Windows.Forms.TextBoxBase.set_WindowText(St ring value)
at System.Windows.Forms.Control.set_Text(String value)
at System.Windows.Forms.TextBoxBase.set_Text(String value)
at System.Windows.Forms.TextBox.set_Text(String value)
at WindowsFormsApp1.Form1.GetUpdates() in C:\Users\NP\documents\visual studio 2017\Projects\WindowsFormsApp1\WindowsFormsApp1\Fo rm1.cs:line 40
at System.Threading.ThreadHelper.ThreadStart_Context( Object state)
at System.Threading.ExecutionContext.RunInternal(Exec utionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionCon text executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionCon text executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()









اینم کد برنامه ام




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;


namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
Telegram.Bot.TelegramBotClient bot = new Telegram.Bot.TelegramBotClient("376*********************QkI");


Thread a;
public Form1()
{
InitializeComponent();
}

public void GetUpdates()
{
int offset = 0;
while(true)
{
Telegram.Bot.Types.Update[] updates = bot.GetUpdatesAsync(offset).Result;
foreach (var update in updates)
{
offset = update.Id + 1;
if (update.Message == null)
continue;


var from = update.Message.From.Username;
var textt = update.Message.Text;
long chatId = update.Message.Chat.Id;


textBox1.Text = chatId.ToString();





}




}
}


private void button1_Click(object sender, EventArgs e)
{
bot.SendTextMessageAsync("357935583", "salam");
}


private void Form1_Load(object sender, EventArgs e)
{
a = new Thread(new ThreadStart(GetUpdates));
a.Start();
}


private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
try { a.Abort(); }
catch { }
}
}
}

rezaAcc
سه شنبه 02 خرداد 1396, 01:29 صبح
از دوستان کسی راهکاری برای حل این مشکل نداره؟

YasserDivaR
چهارشنبه 03 خرداد 1396, 22:26 عصر
از دوستان کسی راهکاری برای حل این مشکل نداره؟


public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;

rezaAcc
جمعه 05 خرداد 1396, 12:05 عصر
اما یه سوال دیگه
چطور میتونم به پیام هایی که قبل از اجرای برنامه کنترل رباطم به رباط فرستاده شده کاری نداشته باشم و پیام هایی رو که از زمان اجرای نرم افزار فرستاده میشه رو پردازش کنم؟

YasserDivaR
شنبه 06 خرداد 1396, 16:41 عصر
اما یه سوال دیگه
چطور میتونم به پیام هایی که قبل از اجرای برنامه کنترل رباطم به رباط فرستاده شده کاری نداشته باشم و پیام هایی رو که از زمان اجرای نرم افزار فرستاده میشه رو پردازش کنم؟

در حالت عادی چنین چیزی امکان پذیر نیست
چون پیامها از کلاینت اول یه سرور و سپس از اونجا به کلاینت دوم ارسال میشه
البته یه روش وجود داره
اونم اینه که زمانی که برنامه متصل نیست با موبوگرام ، توکن ربات و وارد کن و متصل شو
تا پیام ها رو اونجا دریافت کنی تا وقتی برنامه اجرا شد کاری به پیام های قبل از اجرا نداشته باشه

_behnam_
دوشنبه 08 خرداد 1396, 22:41 عصر
اما یه سوال دیگه
چطور میتونم به پیام هایی که قبل از اجرای برنامه کنترل رباطم به رباط فرستاده شده کاری نداشته باشم و پیام هایی رو که از زمان اجرای نرم افزار فرستاده میشه رو پردازش کنم؟
سلام. باید از offset استفاده کنید.
بهتره که API های تلگرام رو مطالعه کنید.
https://core.telegram.org/bots/api#getupdates
(https://core.telegram.org/bots/api#getupdates)