Click here to Skip to main content
15,887,175 members
Home / Discussions / C#
   

C#

 
AnswerRe: Need code to download file from FTP with Auto Resume functionality in case of Internet disconnection. Pin
Mycroft Holmes12-Feb-21 10:08
professionalMycroft Holmes12-Feb-21 10:08 
AnswerRe: Need code to download file from FTP with Auto Resume functionality in case of Internet disconnection. Pin
Luc Pattyn13-Feb-21 5:56
sitebuilderLuc Pattyn13-Feb-21 5:56 
QuestionDoes anyone know the C# implementation for this? Pin
kangkongflea12-Feb-21 0:38
kangkongflea12-Feb-21 0:38 
SuggestionRe: Does anyone know the C# implementation for this? Pin
Richard MacCutchan12-Feb-21 1:15
mveRichard MacCutchan12-Feb-21 1:15 
AnswerRe: Does anyone know the C# implementation for this? Pin
OriginalGriff12-Feb-21 2:07
mveOriginalGriff12-Feb-21 2:07 
AnswerRe: Does anyone know the C# implementation for this? Pin
Luc Pattyn12-Feb-21 3:37
sitebuilderLuc Pattyn12-Feb-21 3:37 
AnswerRe: Does anyone know the C# implementation for this? Pin
Gerry Schmitz12-Feb-21 9:21
mveGerry Schmitz12-Feb-21 9:21 
QuestionHow to access GUI from another thread? Pin
Alex Dunlop11-Feb-21 7:10
Alex Dunlop11-Feb-21 7:10 
I tried to simulate a process in which by pressing a button, some calculations take place (I used a tutorial). Now, I want to test and learn how I can manipulate GUI from an another thread. I decided to change button1 text. I added its code to DoWork section. As I expected, it throws cross-thread exception. Please write me a code and use Invoke and Delegate to see how I can access GUI. I want to learn from your code. Thanks.
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            button1.Text = "processing";
            int sum = 0;
            for (int i = 0; i <= 100; i++)
            {
                Thread.Sleep(100);
                sum = sum + i;
                backgroundWorker1.ReportProgress(i);
                if (backgroundWorker1.CancellationPending)
                {
                    e.Cancel = true;
                    backgroundWorker1.ReportProgress(0);
                    return;
                }
            }
            e.Result = sum;
        }


        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            label1.Text = e.ProgressPercentage + "%";

        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                label1.Text = "Process canceled";
            }
            else if (e.Error != null)
            {
                label1.Text = e.Error.Message;
            }
            else
            {
                label1.Text = "Result: " + e.Result.ToString();
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!backgroundWorker1.IsBusy)
            {
                backgroundWorker1.RunWorkerAsync();
            }
            else
            {
                MessageBox.Show("Busy processing!. Please wait...");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (backgroundWorker1.IsBusy)
            {
                backgroundWorker1.CancelAsync();
            }
            else
            {
                label1.Text = "No operation is running to cancel.";
            }
        }
    }


modified 11-Feb-21 14:05pm.

AnswerRe: How to access GUI from another thread? Pin
Gerry Schmitz11-Feb-21 7:39
mveGerry Schmitz11-Feb-21 7:39 
GeneralRe: How to access GUI from another thread? Pin
Alex Dunlop11-Feb-21 7:40
Alex Dunlop11-Feb-21 7:40 
GeneralRe: How to access GUI from another thread? Pin
Alex Dunlop11-Feb-21 8:07
Alex Dunlop11-Feb-21 8:07 
GeneralRe: How to access GUI from another thread? Pin
Dave Kreskowiak11-Feb-21 13:17
mveDave Kreskowiak11-Feb-21 13:17 
AnswerRe: How to access GUI from another thread? Pin
Gerry Schmitz11-Feb-21 9:03
mveGerry Schmitz11-Feb-21 9:03 
AnswerRe: How to access GUI from another thread? Pin
Ralf Meier11-Feb-21 10:36
mveRalf Meier11-Feb-21 10:36 
AnswerRe: How to access GUI from another thread? PinPopular
Luc Pattyn11-Feb-21 11:29
sitebuilderLuc Pattyn11-Feb-21 11:29 
GeneralRe: How to access GUI from another thread? Pin
Eddy Vluggen11-Feb-21 14:01
professionalEddy Vluggen11-Feb-21 14:01 
GeneralRe: How to access GUI from another thread? Pin
Alex Dunlop11-Feb-21 18:18
Alex Dunlop11-Feb-21 18:18 
GeneralRe: How to access GUI from another thread? Pin
Alex Dunlop11-Feb-21 19:39
Alex Dunlop11-Feb-21 19:39 
AnswerMessage Closed Pin
11-Feb-21 14:18
professionalEddy Vluggen11-Feb-21 14:18 
GeneralRe: How to access GUI from another thread? Pin
Alex Dunlop11-Feb-21 18:16
Alex Dunlop11-Feb-21 18:16 
QuestionJsonConvert.DeserializeObject Pin
Member 1449252210-Feb-21 21:43
Member 1449252210-Feb-21 21:43 
AnswerRe: JsonConvert.DeserializeObject Pin
OriginalGriff10-Feb-21 22:05
mveOriginalGriff10-Feb-21 22:05 
AnswerRe: JsonConvert.DeserializeObject Pin
Member 1449252211-Feb-21 4:13
Member 1449252211-Feb-21 4:13 
AnswerRe: JsonConvert.DeserializeObject Pin
James Curran11-Feb-21 23:58
James Curran11-Feb-21 23:58 
GeneralRe: JsonConvert.DeserializeObject Pin
Member 1449252212-Feb-21 7:08
Member 1449252212-Feb-21 7:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.