Okay. I have an issue an i need help. I am sending a message to a list of numbers stored in a text file. What i do is to loop through all the numbers and send an sms to one after the other using the method used to send to a single number.
Issue is: i want to display progress in a maquee progressbar untill when i finish sending. I guess this works hand in hand with a backgroundworker. I have tried this but i get an error "Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on." on the line with "objSmsMessage.Data = tbMessage.Text;"
below is my code. I need help. Iam so daft when it comes to progressbars and the backgroundworker.
SENDING MESSAGE
Code:private void btnStart_Click(object sender, EventArgs e) { lblConnectionStatus.Text = ""; btnCancel.Enabled = true; btnStart.Enabled = false; ProgressBar1.Value = 0; Cursor = Cursors.WaitCursor; backgroundWorker1.RunWorkerAsync(); }Code:private void SendBulk() { // Set Cursor //Cursor.Current = Cursors.WaitCursor; for (int i = 0; i < 100; i++) { using (StreamReader reader = File.OpenText(tbMessageRecipient.Text)) { string line; while ((line = reader.ReadLine()) != null) { objSmsMessage.Recipient = line; string strMessageReference; object obj; objSmsMessage.Clear(); objSmsMessage.RequestDeliveryStatus = _frmSmsSendOptions.bDeliveryReport ? -1 : 0; //Add Many Reciepients and message is sent line by line. objSmsMessage.Recipient = line; objSmsMessage.Data = tbMessage.Text; objSmsMessage.Format = objConstants.asMESSAGEFORMAT_TEXT; if (chkUnicode.Checked) { objSmsMessage.Format = objConstants.asMESSAGEFORMAT_UNICODE; if (_frmSmsSendOptions.bImmediateDisplay) objSmsMessage.Format = objConstants.asMESSAGEFORMAT_UNICODE_FLASH; if (_frmSmsSendOptions.bMultipart) objSmsMessage.Format = objConstants.asMESSAGEFORMAT_UNICODE_MULTIPART; } else { objSmsMessage.Format = objConstants.asMESSAGEFORMAT_TEXT; if (_frmSmsSendOptions.bImmediateDisplay) objSmsMessage.Format = objConstants.asMESSAGEFORMAT_TEXT_FLASH; if (_frmSmsSendOptions.bMultipart) objSmsMessage.Format = objConstants.asMESSAGEFORMAT_TEXT_MULTIPART; } if (_frmSmsSendOptions.bUDH) objSmsMessage.Format = objConstants.asMESSAGEFORMAT_DATA_UDH; obj = objSmsMessage; //SEND MESSAGE strMessageReference = objSmppProtocol.Send(ref obj); UpdateResult(objSmppProtocol.LastError); if (objSmppProtocol.LastError == 0) { //Add sent data to the datagridview. //get the last mID and add 1 List<OutBox> messageAdded = new List<OutBox>(); messageAdded.Add(new OutBox(1, objSmsMessage.Data, Convert.ToDateTime(DateTime.Now.ToString()), "Delivered", Convert.ToDateTime(DateTime.Now.ToString()), Convert.ToDateTime(DateTime.Now.ToString()))); dgvOutbox.DataSource = messageAdded; FormatDgvOutBox(); } // Reset Options in case of a UDH message (i.e.: WAP, Picture, Ringtone) if (_frmSmsSendOptions.bUDH) { // If this is not done, you cannot send plain text after you've sent WAP, Ringines, Pictures _frmSmsSendOptions.ResetFlags(); tbMessage.Text = ""; } } // Reset Cursor //Cursor.Current = Cursors.Default; } backgroundWorker1.ReportProgress(i); Thread.Sleep(100); } }Code:private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { // Get the BackgroundWorker that raised this event. //BackgroundWorker worker1 = sender as BackgroundWorker; //e.Result = SumNumbers(Convert.ToDouble(e.Argument),worker1,e); SendBulk(); } private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { ProgressBar1.Value = e.ProgressPercentage; } private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Error != null) MessageBox.Show(e.Error.Message); else if (e.Cancelled) MessageBox.Show("Cancelled"); else lblConnectionStatus.Text = "Complete"; btnStart.Enabled = true; btnCancel.Enabled = false; Cursor = Cursors.Default; } private void btnCancel_Click(object sender, EventArgs e) { backgroundWorker1.CancelAsync(); btnCancel.Enabled = false; btnStart.Enabled = true; }




Reply With Quote