Showing posts with label c# threads. Show all posts
Showing posts with label c# threads. Show all posts
wcf multithreading
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall,
ConcurrencyMode=ConcurrencyMode.Single)]
async await c# simple example
public class MegaClass { public async void StartAsync() { await Task.Run(() => { LongProcedure(); }); Console.WriteLine("End"); } public void Start() { LongProcedure(); Console.WriteLine("End"); } private void LongProcedure() { Thread.Sleep(3000); } public async Task MyMethodAsync() { int x = await LongRunningOperationAsync(); Console.WriteLine(x); } public async TaskLongRunningOperationAsync() { await Task.Delay(3000); return 1; } public async Task LongRunningOperationAsync(int _param) { await Task.Delay(3000); return _param; } } class Program { static void Main(string[] args) { var _c = new MegaClass(); //1 Sync //_c.Start(); //2 Async //c.StartAsync(); //3 Start and return task //Task t = _c.MyMethodAsync(); //t.Wait(); //4 //Task task = new Task(_c.StartAsync); //task.Start(); //5 GetResult //Task x = _c.LongRunningOperationAsync(); //x.Wait(); //Console.WriteLine(x.Result); //6 with param //Task x = _c.LongRunningOperationAsync(3); //x.Wait(); //Console.WriteLine(x.Result); Console.WriteLine("Wait"); Console.ReadLine(); } }
c# run thread in threadpool
This is the way of running thread in ThreadPool
1 only for 4.5 Framework
1 only for 4.5 Framework
Task.Run(() => doStuff("hello world"));2
Task.Factory.StartNew(() => doStuff("hello world"));3
ThreadPool.QueueUserWorkItem(a => doStuff("hello world"));4
void Example() { // Hook up the ProcessFile method to the ThreadPool. // Note: 'a' is an argument name. Read more on arguments. ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessFile), a); } private void ProcessFile(object a) { // I was hooked up to the ThreadPool by WaitCallback. }
c# timer without timer
public void OnStart() { _stop.Reset(); _registeredWait = ThreadPool.RegisterWaitForSingleObject(_stop, new WaitOrTimerCallback(PeriodicProcess), null, 5000, false); } public void OnStop() { _stop.Set(); } private void PeriodicProcess(object state, bool timeout) { if (timeout) { // Periodic processing here } else // Stop any more events coming along _registeredWait.Unregister(null); } private ManualResetEvent _stop = new ManualResetEvent(false); private RegisteredWaitHandle _registeredWait;
//It's the method for periodically calling some methothds without using Timers. I think it is more elegant solution for this.
//Waiting process works in separate thread pool. Calling method works in main thread. Be carefull with that.
C# How to change UI control from other thread (WinForms and WPF)
WinForms:
public delegate void updateTextBoxDelegate(String textBoxString); public updateTextBoxDelegate updateTextBox; void updateTextBox1(string str ) { textBox1.Text = str1; } void display( string strItem ) { Form1.Invoke( Form1.updateTextBox, strItem ); }
WPF:
tempWindow.Dispatcher.BeginInvoke(new Action(delegate() { tempWindow.ChangeControlMethod(); }));
Subscribe to:
Posts (Atom)