Click here to Skip to main content
15,880,796 members
Home / Discussions / C#
   

C#

 
GeneralRe: request.GetResponse() - The remote server returned an error: (403) Forbidden Pin
_Q12_5-Aug-19 18:22
_Q12_5-Aug-19 18:22 
GeneralRe: request.GetResponse() - The remote server returned an error: (403) Forbidden Pin
_Q12_5-Aug-19 19:17
_Q12_5-Aug-19 19:17 
GeneralRe: request.GetResponse() - The remote server returned an error: (403) Forbidden Pin
Dave Kreskowiak6-Aug-19 3:05
mveDave Kreskowiak6-Aug-19 3:05 
AnswerRe: request.GetResponse() - The remote server returned an error: (403) Forbidden Pin
Gerry Schmitz6-Aug-19 4:44
mveGerry Schmitz6-Aug-19 4:44 
QuestionSlight problem with my array program Pin
Member 129742354-Aug-19 12:51
Member 129742354-Aug-19 12:51 
AnswerRe: Slight problem with my array program Pin
Richard Andrew x644-Aug-19 13:06
professionalRichard Andrew x644-Aug-19 13:06 
AnswerRe: Slight problem with my array program Pin
OriginalGriff4-Aug-19 19:24
mveOriginalGriff4-Aug-19 19:24 
QuestionGenerics overload Pin
Bernhard Hiller2-Aug-19 0:13
Bernhard Hiller2-Aug-19 0:13 
I try to create a wrapper for measuring the execution time.
With actions its simple:
C#
public static class StopWatch
{
    public static TimeSpan MeasureAction(Action _action)
    {
        Stopwatch watch = Stopwatch.StartNew();
        _action();
        watch.Stop();
        return watch.Elapsed;
    }
}
Also, common return types are not a problem, I added an extra class for returning the duration plus the "normal" result (.Net 4.5, so the more modern Tuples whose contents have names beyond Item1 etc are not available):
C#
internal class MeasuredExecution<T> : IMeasuredExecution<T>
{
    public MeasuredExecution(TimeSpan _duration, T _result)
    {
        Duration = _duration;
        Result = _result;
    }

    public TimeSpan Duration { get; }
    public T Result { get; }
}

public static class StopWatch<T>
{
    public static IMeasuredExecution<T> MeasureFunction(Func<T> _function)
    {
        Stopwatch watch = Stopwatch.StartNew();
        T result = _function();
        watch.Stop();
        return new MeasuredExecution<T>(watch.Elapsed, result);
    }
}
Now comes the problem I am looking for a better solution: the return value may be an IDisposable. In order to properly use it with a using clause, I need a wrapper exposing the IDisposable interface and calling Dispose on the Result. That wrapper part is easy, but what about the StopWatch? I created another class:
C#
internal class DisposableMeasuredExecution<T> : MeasuredExecution<T>, IDisposableMeasuredExecution<T> where T : IDisposable
{
    public DisposableMeasuredExecution(TimeSpan _duration, T _result)
        : base(_duration, _result) { }


    public void Dispose()
    {
        Result.Dispose();
    }
}

public static class StopWatchD<T> where T : IDisposable
{
    public static IDisposableMeasuredExecution<T> MeasureFunction(Func<T> _function)
    {
        Stopwatch watch = Stopwatch.StartNew();
        T result = _function();
        watch.Stop();
        return new DisposableMeasuredExecution<T>(watch.Elapsed, result);
    }
}
Due to the extra constraint that T must now be an IDisposable, I failed to get that in the previous StopWatch class, even when using a different function name.
That feels like a WTF. Do you have any ideas how to accomplish that?
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

AnswerRe: Generics overload Pin
Richard Deeming2-Aug-19 1:56
mveRichard Deeming2-Aug-19 1:56 
GeneralRe: Generics overload Pin
Bernhard Hiller2-Aug-19 3:17
Bernhard Hiller2-Aug-19 3:17 
GeneralRe: Generics overload Pin
Richard Deeming2-Aug-19 3:38
mveRichard Deeming2-Aug-19 3:38 
GeneralRe: Generics overload Pin
Bernhard Hiller4-Aug-19 21:50
Bernhard Hiller4-Aug-19 21:50 
AnswerRe: Generics overload Pin
Gerry Schmitz2-Aug-19 5:36
mveGerry Schmitz2-Aug-19 5:36 
QuestionEmbedding resources with static directories Pin
Member 87627471-Aug-19 20:32
Member 87627471-Aug-19 20:32 
Questionadding double quote around variable add one backslash which causing issue Pin
Mou_kol31-Jul-19 5:54
Mou_kol31-Jul-19 5:54 
AnswerRe: adding double quote around variable add one backslash which causing issue Pin
OriginalGriff31-Jul-19 6:18
mveOriginalGriff31-Jul-19 6:18 
GeneralRe: adding double quote around variable add one backslash which causing issue Pin
Mou_kol31-Jul-19 21:25
Mou_kol31-Jul-19 21:25 
AnswerRe: adding double quote around variable add one backslash which causing issue Pin
Richard Deeming31-Jul-19 7:46
mveRichard Deeming31-Jul-19 7:46 
GeneralRe: adding double quote around variable add one backslash which causing issue Pin
Mou_kol31-Jul-19 21:30
Mou_kol31-Jul-19 21:30 
GeneralRe: adding double quote around variable add one backslash which causing issue Pin
Richard Deeming1-Aug-19 1:30
mveRichard Deeming1-Aug-19 1:30 
GeneralRe: adding double quote around variable add one backslash which causing issue Pin
Mou_kol1-Aug-19 5:44
Mou_kol1-Aug-19 5:44 
QuestionIs the "using" statement really a good idea? Pin
MSBassSinger30-Jul-19 7:19
professionalMSBassSinger30-Jul-19 7:19 
AnswerRe: Is the "using" statement really a good idea? Pin
Richard Deeming30-Jul-19 7:42
mveRichard Deeming30-Jul-19 7:42 
GeneralRe: Is the "using" statement really a good idea? Pin
MSBassSinger30-Jul-19 8:05
professionalMSBassSinger30-Jul-19 8:05 
GeneralRe: Is the "using" statement really a good idea? Pin
Richard Deeming30-Jul-19 8:15
mveRichard Deeming30-Jul-19 8:15 

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.