Click here to Skip to main content
16,012,223 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi all,
I need to implement Multithreading on button_click event.i am doing on buttion click i am updateing database values.if one my client is clicking continuesly with out break i need to create thread add in button_click and i need call method for update database value.can any one help me in this.
Thanks,
Naresh.G
Posted
Comments
[no name] 27-Feb-12 9:20am    
What have you tried so far?
Amund Gjersøe 27-Feb-12 9:23am    
Have you considered using asynchronous updating of the database? Asynchronous methods often starts with Begin.....
Sergey Alexandrovich Kryukov 27-Feb-12 12:39pm    
To start with, tag your UI library and application type (WPF, Forms, ASP.NET, Silverlight, what?)
--SA

Here is the sample code to execute multi threading using button_click events

C#
public void ThreadRun( )
{
  // ur multi thread operation
}

Thread oThread = new Thread(new ThreadStart(ThreadRun));

private void Start_Click(object sender, EventArgs e)
{
    oThread.Start();
}

private void Cancel_Click(object sender, EventArgs e)
{
    oThread.Abort();

}
 
Share this answer
 
Take a look at the System.Threading.Tasks.Task class which makes it easier to work with tasks and also supports cancelation.

Uros
 
Share this answer
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
int _digitsToCalc = 20;
private Thread currentTimeThread = null;
public Form1()
{
InitializeComponent();

currentTimeThread = new Thread(new ThreadStart(CountTime));
currentTimeThread.IsBackground = true;
}


public void CountTime()
{
while (true)
{
Invoke(new UpdateTimeDelegate(updateCurrentTime));
Thread.Sleep(1000);
}
}
private void updateCurrentTime()
{
randomNumbers.Text = DateTime.Now.ToLongTimeString();

currentTimeThread.Abort();
}
static List <string> _list = new List <string>();
private void start_Click(object sender, EventArgs e)
{

currentTimeThread.Start();//error is:Thread is running or terminated; it cannot restart.

}
}
public delegate void UpdateTimeDelegate();
}


Hi all this my code and i am geting error like this:Thread is running or terminated; it cannot restart.
 
Share this answer
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
int _digitsToCalc = 20;
private Thread currentTimeThread = null;
public Form1()
{
InitializeComponent();
}


public void CountTime()
{
while (true)
{
Invoke(new UpdateTimeDelegate(updateCurrentTime));
Thread.Sleep(1000);
}
}
private void updateCurrentTime()
{
randomNumbers.Text = DateTime.Now.ToLongTimeString();

ManualResetEvent stopEvent = new ManualResetEvent(false);

}

int threadCount = 0;
private void start_Click(object sender, EventArgs e)
{
currentTimeThread = new Thread(new ThreadStart(CountTime));
currentTimeThread.IsBackground = true;
currentTimeThread.Name = "Thread ID: " + threadCount++;
currentTimeThread.Start();
label1.Text = currentTimeThread.Name.ToString();
}
}
public delegate void UpdateTimeDelegate();
}
 
Share this answer
 

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