 |
|
 |
I hope u dont mind i coverted it to vb.net .net 2.0 to use in my project.
|
|
|
|
 |
|
 |
Thanks for the code i changed some of the code you can check it below
|
|
|
|
 |
|
 |
I like it, especially how implementing the IThreadedExecuterView interface in a base form can standardize custom wait functionality across an entire application; however, how can I use this to avoid checking for InvokeRequired before executing, say, from an event thrown on another thread? I copied the contents of button1_click to the event handler of a timer, and a "Cross-thread operation not valid" exception was thrown in the SetWait interface method here, "this.Cursor = (isEnabled ? Cursors.Default : Cursors.WaitCursor);" when the form cursor is changed from the timer thread...
|
|
|
|
 |
|
 |
You only put the multi threaded stuff in the first delegate and the second delegate will happen on the UI thread. If you are retrieving data via events, you would have to collect that in the first delegate and return it to the second delegate and then set it all. For everything I do multi threaded it is a get/return motif, not a connect/event/event/event type thing, so perhaps it isn't good for that without some changes.
|
|
|
|
 |
|
 |
Hi paul!
Is it possible to use your ThreadedExecuter class if the method that is executed returns void?
something like this?
using (ThreadedExecuter<void> executer = new ThreadedExecuter<void>(this))
{
executer
.Process(() =>
{
foo();
})
.WhenFinished(null =>
{
foofoo();
})
.Run();
}
</void></void>
|
|
|
|
 |
|
|
 |
|
 |
Two classes with the same name in the same namespace and two delegates with the same name ??
|
|
|
|
 |
|
 |
You sound confused. Try it it works
|
|
|
|
 |
|
 |
O i c your problem
heres a better view
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
namespace Library
{
public interface IThreadedExecuterView
{
void SetWait(bool isEnabled);
void HandleException(Exception ex);
}
public delegate void ThreadedExecuterDelegate();
public class ThreadedExecuter : IDisposable
{
public ThreadedExecuter()
{
}
public ThreadedExecuter(IThreadedExecuterView View)
{
this.View = View;
}
BackgroundWorker worker = null;
IThreadedExecuterView View = null;
public ThreadedExecuter Process(ThreadedExecuterDelegate GetResultsAction)
{
View.SetWait(false);
worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(delegate(object sender, DoWorkEventArgs e)
{
try
{
GetResultsAction();
}
catch (Exception ex)
{
View.HandleException(ex);
}
});
return this;
}
public ThreadedExecuter WhenFinished(Action ProcessResultsAction)
{
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate(object sender, RunWorkerCompletedEventArgs e)
{
try
{
ProcessResultsAction();
}
finally
{
try
{
View.SetWait(true);
}
catch (Exception ex)
{
Console.WriteLine("Error setting view wait: " + ex.ToString());
}
}
});
return this;
}
Action FinallyDelegate = null;
public ThreadedExecuter Finally(Action FinallyDelegate)
{
this.FinallyDelegate = FinallyDelegate;
return this;
}
public ThreadedExecuter Run()
{
if (FinallyDelegate != null)
{
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate(object sender, RunWorkerCompletedEventArgs e)
{
try
{
FinallyDelegate();
}
catch (Exception ex)
{
View.HandleException(ex);
}
});
}
worker.RunWorkerAsync();
return this;
}
public void Dispose()
{
if (worker != null)
worker.Dispose();
}
}
public delegate T ThreadedExecuterDelegate<T>();
public class ThreadedExecuter<T> : IDisposable
{
public ThreadedExecuter()
{
}
public ThreadedExecuter(IThreadedExecuterView View)
{
this.View = View;
}
BackgroundWorker worker = null;
IThreadedExecuterView View = null;
T actionValue = default(T);
public ThreadedExecuter<T> Process(ThreadedExecuterDelegate<T> GetResultsAction)
{
View.SetWait(false);
worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(delegate(object sender, DoWorkEventArgs e)
{
try
{
e.Result = GetResultsAction();
}
catch (Exception ex)
{
View.HandleException(ex);
}
});
return this;
}
public ThreadedExecuter<T> WhenFinished(Action<T> ProcessResultsAction)
{
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate(object sender, RunWorkerCompletedEventArgs e)
{
if (e != null && e.Result != null)
actionValue = (T)e.Result;
try
{
ProcessResultsAction(actionValue);
}
finally
{
try
{
View.SetWait(true);
}
catch (Exception ex)
{
Console.WriteLine("Error setting view wait: " + ex.ToString());
}
}
});
return this;
}
Action FinallyDelegate = null;
public ThreadedExecuter<T> Finally(Action FinallyDelegate)
{
this.FinallyDelegate = FinallyDelegate;
return this;
}
public ThreadedExecuter<T> Run()
{
if (FinallyDelegate != null)
{
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate(object sender, RunWorkerCompletedEventArgs e)
{
try
{
FinallyDelegate();
}
catch (Exception ex)
{
View.HandleException(ex);
}
});
}
worker.RunWorkerAsync();
return this;
}
public void Dispose()
{
if (worker != null)
worker.Dispose();
}
}
}
|
|
|
|
 |
|
 |
Looks better
but this code will generate two exceptions
Error 1 : The namespace 'Library' already contains a definition for 'ThreadedExecuterDelegate
Error 2 : The namespace 'Library' already contains a definition for 'ThreadedExecuter'
|
|
|
|
 |
|
 |
I copied the code and it works for me
|
|
|
|
 |
|
|
 |
|
 |
This one was better..
Thx man
|
|
|
|
 |
|
 |
Thank you If you are making some changes yourself please post it
|
|
|
|
 |
|
 |
Would be nice if the code could be documented a bit better, something like this maybe?
Microsft StyleCop or some other code rule design program.
namespace Library
{
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
public class ThreadedExecuter : IDisposable
{
#region Fields
private BackgroundWorker worker;
private IThreadedExecuterView view;
private Action finallyDelegate;
private bool disposed;
#endregion
#region Constructor
public ThreadedExecuter()
{
}
public ThreadedExecuter(IThreadedExecuterView view)
{
this.view = view;
}
#endregion
#region Destructor
~ThreadedExecuter()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(true);
}
private void Dispose(bool disposing)
{
if (this.disposed)
{
return;
}
if (disposing)
{
if (worker != null)
{
worker.Dispose();
}
}
this.disposed = true;
}
#endregion
#region Methods
public ThreadedExecuter Process(ThreadedExecuterDelegate GetResultsAction)
{
view.SetWait(false);
worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(delegate(object sender, DoWorkEventArgs e)
{
try
{
GetResultsAction();
}
catch (Exception ex)
{
view.HandleException(ex);
}
});
return this;
}
public ThreadedExecuter WhenFinished(Action ProcessResultsAction)
{
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate(object sender, RunWorkerCompletedEventArgs e)
{
try
{
ProcessResultsAction();
}
finally
{
try
{
view.SetWait(true);
}
catch (Exception ex)
{
Console.WriteLine("Error setting view wait: " + ex.ToString());
}
}
});
return this;
}
public ThreadedExecuter Finally(Action FinallyDelegate)
{
this.finallyDelegate = FinallyDelegate;
return this;
}
public ThreadedExecuter Run()
{
if (finallyDelegate != null)
{
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate(object sender, RunWorkerCompletedEventArgs e)
{
try
{
finallyDelegate();
}
catch (Exception ex)
{
view.HandleException(ex);
}
});
}
worker.RunWorkerAsync();
return this;
}
#endregion
}
}
|
|
|
|
 |
|
 |
Yip sorry about that but i was just giving the solution. Are you working on server/client apps? There is a method that i'm busy with that compresses objects, strings,etc
For Example
DataTable table = test.Copy().Tables[0];
HelperClass.Compression.Compress(ref table);
HelperClass.Compression.Decompress(ref table);
DataSet dstable = test.Copy();
HelperClass.Compression.Compress(ref dstable);
HelperClass.Compression.Decompress(ref dstable);
String str = "Helo";
HelperClass.Compression.Compress(ref str);
HelperClass.Compression.Decompress(ref str);
|
|
|
|
 |
|
 |
yep I'm working with CS-app, but do not have any code for compressing datatables, do you need any help?
|
|
|
|
 |
|
 |
Yip. Give me your email address then we can chat via email
|
|
|
|
 |
|
|
 |
|
 |
Yes
|
|
|
|
 |
|
 |
Good class and code,
cheers,
Donsw
My Recent Article : Optimistic Concurrency with C# using the IOC and DI Design Patterns
|
|
|
|
 |
|
 |
What would be the best way to kill a particular thread started by this class?
|
|
|
|
 |
|
 |
What I would like to do is set a timeout for each excecuted thread and once that timeout has been reached to safely kill the thread. I think it would be a good addition to this great class.
|
|
|
|
 |
|
 |
I've added something like this by overloading the Run() method with a timeout. I'm not sure it is working.
Perhaps someone could review?
public ThreadedExecuter<T> Run(double timeOut)
{
if (timeOut > 0)
{
worker.WorkerSupportsCancellation = true;
timer = new System.Timers.Timer(timeOut);
timer.Elapsed += ((sender, e) =>
{
try
{
Terminate();
}
catch (Exception ex)
{
View.HandleException(ex);
}
});
timer.Start();
}
return Run();
}
System.Timers.Timer timer = new System.Timers.Timer();
public double TimeOut
{
get { return timer.Interval; }
}
public void Terminate()
{
if (timer.Enabled)
{
timer.Stop();
}
if (worker.IsBusy)
{
worker.CancelAsync();
}
}
|
|
|
|
 |
|
 |
I've been out of town for a while but I'll try and take a look in the next few days. What you are doing seems resonable and nothing jumps out at me at first glance, other than the normal issues with killing a thread instead of allowing it to exit gracefully. If possible it would be better to check a condition on a schedule and then just stop whatever work you were doing but sometimes that isn't possible depending on what you're doing.
|
|
|
|
 |