Click here to Skip to main content
15,887,027 members
Home / Discussions / C#
   

C#

 
GeneralRe: Using strings from external .txt file Pin
Tetra10443-Feb-15 4:41
Tetra10443-Feb-15 4:41 
AnswerRe: Using strings from external .txt file Pin
BillWoodruff4-Feb-15 6:21
professionalBillWoodruff4-Feb-15 6:21 
GeneralRe: Using strings from external .txt file Pin
Tetra10445-Feb-15 3:06
Tetra10445-Feb-15 3:06 
QuestionHow to share a server connection between different powershell runspaces in c# on a web application Pin
tasoss2-Feb-15 21:37
tasoss2-Feb-15 21:37 
QuestionRe: How to share a server connection between different powershell runspaces in c# on a web application Pin
Richard MacCutchan3-Feb-15 0:38
mveRichard MacCutchan3-Feb-15 0:38 
AnswerRe: How to share a server connection between different powershell runspaces in c# on a web application Pin
tasoss3-Feb-15 0:59
tasoss3-Feb-15 0:59 
SuggestionReceiving Windows Message in class library asynchronously in C# Pin
Ram Kumar2-Feb-15 18:07
Ram Kumar2-Feb-15 18:07 
GeneralRe: Receiving Windows Message in class library asynchronously in C# Pin
Richard Deeming4-Feb-15 1:53
mveRichard Deeming4-Feb-15 1:53 
Assuming you're using .NET 4.0 or higher, something like this should work:
C#
using System;
using System.Threading.Tasks;
using System.Windows.Forms;

public static class MessageReceiver
{
    public static Task<string> WaitForMessage(Control window)
    {
        if (window == null) throw new ArgumentNullException("window");
        return new MessageListener(window).Status;
    }
    
    private sealed class MessageListener : NativeWindow
    {
        private const int WM_SUCCESS = 0x2000;
        private const int WM_FAILURE = 0x2001;
        
        private readonly TaskCompletionSource<string&gt; _status = new TaskCompletionSource<string&gt;();
        
        public MessageListener(Control parent)
        {
            _parent.HandleCreated += OnHandleCreated;
            _parent.HandleDestroyed += OnHandleDestroyed;
            
            if (_parent.IsHandleCreated)
            {
                OnHandleCreated(_parent, EventArgs.Empty);
            }
        }
        
        private void OnHandleCreated(object sender, EventArgs e)
        {
            AssignHandle(((IWin32Window)sender).Handle);
        }
        
        private void OnHandleDestroyed(object sender, EventArgs e)
        {
            ReleaseHandle();
        }
        
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_SUCCESS:
                {
                    _status.TrySetResult("SUCCESS");
                    base.WndProc(ref m);
                    ReleaseHandle();
                    break;
                }
                case WM_FAILURE:
                {
                    _status.TrySetResult("FAILURE");
                    base.WndProc(ref m);
                    ReleaseHandle();
                    break;
                }
                default:
                {
                    base.WndProc(ref m);
                    break;
                }
            }
        }
        
        public Task<string> Status
        {
            get { return _status.Task; }
        }
    }
}

With .NET 4.5, waiting for the message is as simple as:
C#
private async Task Foo()
{
    string status = await MessageReceiver.WaitForMessage(theForm);
    // Do something with the status here...
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


QuestionStruct and Enum Pin
Member 111616252-Feb-15 18:05
Member 111616252-Feb-15 18:05 
AnswerRe: Struct and Enum Pin
OriginalGriff2-Feb-15 21:13
mveOriginalGriff2-Feb-15 21:13 
AnswerRe: Struct and Enum Pin
BillWoodruff3-Feb-15 21:01
professionalBillWoodruff3-Feb-15 21:01 
AnswerRe: Struct and Enum Pin
V.3-Feb-15 22:02
professionalV.3-Feb-15 22:02 
Questionhow do I change a regex Pin
Member 113543862-Feb-15 11:55
Member 113543862-Feb-15 11:55 
AnswerRe: how do I change a regex Pin
Tomáš Podešva2-Feb-15 17:14
professionalTomáš Podešva2-Feb-15 17:14 
AnswerRe: how do I change a regex Pin
OriginalGriff2-Feb-15 21:18
mveOriginalGriff2-Feb-15 21:18 
GeneralRe: how do I change a regex Pin
Member 113543863-Feb-15 14:03
Member 113543863-Feb-15 14:03 
GeneralRe: how do I change a regex Pin
OriginalGriff3-Feb-15 23:28
mveOriginalGriff3-Feb-15 23:28 
Questionwindows 8 hybrid shutdown notification Pin
morglorf2-Feb-15 11:33
morglorf2-Feb-15 11:33 
AnswerRe: windows 8 hybrid shutdown notification Pin
Richard Andrew x642-Feb-15 11:53
professionalRichard Andrew x642-Feb-15 11:53 
GeneralRe: windows 8 hybrid shutdown notification Pin
morglorf2-Feb-15 12:17
morglorf2-Feb-15 12:17 
GeneralRe: windows 8 hybrid shutdown notification Pin
morglorf3-Feb-15 5:54
morglorf3-Feb-15 5:54 
QuestionHelp with Array search. Pin
Member 114193361-Feb-15 18:18
Member 114193361-Feb-15 18:18 
AnswerRe: Help with Array search. Pin
Richard Andrew x641-Feb-15 18:34
professionalRichard Andrew x641-Feb-15 18:34 
GeneralRe: Help with Array search. Pin
Member 114193361-Feb-15 19:28
Member 114193361-Feb-15 19:28 
AnswerRe: Help with Array search. Pin
BillWoodruff1-Feb-15 21:16
professionalBillWoodruff1-Feb-15 21:16 

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.