Click here to Skip to main content
15,889,116 members
Home / Discussions / C#
   

C#

 
QuestionMulti-threading and function recursion with a form [modified] Pin
fdsfsa76f7sa66-Sep-10 3:11
fdsfsa76f7sa66-Sep-10 3:11 
AnswerRe: Mulit-threading and function recursion with a form Pin
Pete O'Hanlon6-Sep-10 3:23
mvePete O'Hanlon6-Sep-10 3:23 
GeneralRe: Mulit-threading and function recursion with a form Pin
fdsfsa76f7sa66-Sep-10 4:17
fdsfsa76f7sa66-Sep-10 4:17 
GeneralRe: Mulit-threading and function recursion with a form Pin
Kubajzz6-Sep-10 4:31
Kubajzz6-Sep-10 4:31 
GeneralRe: Mulit-threading and function recursion with a form Pin
fdsfsa76f7sa66-Sep-10 5:13
fdsfsa76f7sa66-Sep-10 5:13 
GeneralRe: Mulit-threading and function recursion with a form Pin
Pete O'Hanlon6-Sep-10 4:59
mvePete O'Hanlon6-Sep-10 4:59 
AnswerRe: Mulit-threading and function recursion with a form Pin
Cracked-Down6-Sep-10 8:21
Cracked-Down6-Sep-10 8:21 
AnswerDammit, stop blabbing and give him the code already... Pin
lepipele7-Sep-10 4:53
lepipele7-Sep-10 4:53 
Solution on: http://www.sendspace.com/file/f4pcjo

// http://www.codeproject.com/Messages/3589122/Multi-threading-and-function-recursion-with-a-form.aspx
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        _tw = new ThreadingWrapper(this);
    }

    private ThreadingWrapper _tw;
    private void button1_Click(object sender, EventArgs e)
    {
        button1.Enabled = false;
        _tw.ButtonClicked();
    }

    // We are using object parameter so we can call this method with BeginInvoke using WaitCallback delegate
    // If you create your own delegate you can use whatever method signature you want
    public void SetText(object o)
    {
        string text = (string)o;
        label1.Text = text;
    }

    public void WaitingOnClick(object o)
    {
        label1.Text += " - WAITING";
        button1.Enabled = true;
    }
}

public class ThreadingWrapper
{
    private AutoResetEvent _are = new AutoResetEvent(false);
    private bool _waiting;

    private Form1 _form;
    public ThreadingWrapper(Form1 toControl)
    {
        _form = toControl;
    }

    internal void ButtonClicked()
    {
        if (_waiting)
        {
            _waiting = false;
            _are.Set();
        }
        else
        {
            // initialize new background thread to process call
            ThreadPool.QueueUserWorkItem(new WaitCallback(functionCall), 0);
        }
    }

    private void functionCall(object p)
    {
        // perform calculations and set label
        int num = (int)p;
        Thread.Sleep(500);
        // we need to use BeginInvoke because we are setting GUI elements from background thread
        _form.BeginInvoke(new WaitCallback(_form.SetText), num.ToString());

        if (num % 10 == 0)
        {
            // again setting of GUI element
            _form.BeginInvoke(new WaitCallback(_form.WaitingOnClick), "");

            _waiting = true;
            _are.WaitOne();
        }

        functionCall(num + 1);
    }
}


Let me know if you need abort functionality... and I'm also open to any suggestions on how to improve code... always love to learn something new.
GeneralRe: Dammit, stop blabbing and give him the code already... Pin
lepipele7-Sep-10 5:10
lepipele7-Sep-10 5:10 
GeneralRe: Dammit, stop blabbing and give him the code already... Pin
fdsfsa76f7sa69-Sep-10 1:27
fdsfsa76f7sa69-Sep-10 1:27 
GeneralRe: Dammit, stop blabbing and give him the code already... Pin
lepipele9-Sep-10 6:20
lepipele9-Sep-10 6:20 
QuestionRe: Dammit, stop blabbing and give him the code already... [modified] Pin
fdsfsa76f7sa69-Sep-10 22:37
fdsfsa76f7sa69-Sep-10 22:37 
AnswerRe: Dammit, stop blabbing and give him the code already... Pin
lepipele13-Sep-10 9:30
lepipele13-Sep-10 9:30 
GeneralRe: Dammit, stop blabbing and give him the code already... Pin
fdsfsa76f7sa67-Sep-10 5:24
fdsfsa76f7sa67-Sep-10 5:24 
QuestionWindows Task Scheduler problem Pin
Tridip Bhattacharjee6-Sep-10 1:45
professionalTridip Bhattacharjee6-Sep-10 1:45 
AnswerRe: Windows Task Scheduler problem Pin
Pete O'Hanlon6-Sep-10 2:12
mvePete O'Hanlon6-Sep-10 2:12 
GeneralBSCS FINAL PROJECT Pin
HUSSNAIN TOUFIQ6-Sep-10 0:53
HUSSNAIN TOUFIQ6-Sep-10 0:53 
GeneralRe: BSCS FINAL PROJECT PinPopular
OriginalGriff6-Sep-10 0:57
mveOriginalGriff6-Sep-10 0:57 
GeneralRe: BSCS FINAL PROJECT Pin
dan!sh 6-Sep-10 0:58
professional dan!sh 6-Sep-10 0:58 
GeneralRe: BSCS FINAL PROJECT Pin
Eddy Vluggen6-Sep-10 1:14
professionalEddy Vluggen6-Sep-10 1:14 
GeneralRe: BSCS FINAL PROJECT PinPopular
J4amieC6-Sep-10 1:28
J4amieC6-Sep-10 1:28 
GeneralRe: BSCS FINAL PROJECT Pin
DaveyM696-Sep-10 1:35
professionalDaveyM696-Sep-10 1:35 
GeneralRe: BSCS FINAL PROJECT Pin
Smithers-Jones6-Sep-10 2:12
Smithers-Jones6-Sep-10 2:12 
GeneralRe: BSCS FINAL PROJECT PinPopular
Pete O'Hanlon6-Sep-10 2:25
mvePete O'Hanlon6-Sep-10 2:25 
GeneralRe: BSCS FINAL PROJECT Pin
Smithers-Jones6-Sep-10 2:31
Smithers-Jones6-Sep-10 2:31 

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.