Problem with handling keyboard input

I have the following code:
[CODE]
....
public static int keypressed = 0;

private void button1_Click(object sender, EventArgs e)
{
while (keypressed != 1)
{
DrawProc();
}
}

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
keypressed = 1;
}
....
[/CODE]
DrawProc is procedure that keeps drawing pixels on my screen and i want to stop it by pressing any keyboard key but it dosnt work. After program goes into drawing cycle it stops responding to keyboard (KeyPress event never occur).
Can anyone help me please


Answer this question

Problem with handling keyboard input

  • cabilo

    SvenC wrote:

    I do not think that drawing can be done from the background thread as HWND and HDC have thread affinity and they can only be used from the thread they have been created from.

    Background worker is safe for Multi Threaded Environment. Drawing can be done using ProgressChanged Event!

    See my post here with the solution to same thing!

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=847014&SiteID=1

    Best Regards!



  • allpdoff

    sliderX wrote:
    I have the following code:
    [CODE]
    ....
    public static int keypressed = 0;

    private void button1_Click(object sender, EventArgs e)
    {
    while (keypressed != 1)
    {
    DrawProc();
    }
    }

    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
    keypressed = 1;
    }
    ....
    [/CODE]
    DrawProc is procedure that keeps drawing pixels on my screen and i want to stop it by pressing any keyboard key but it dosnt work. After program goes into drawing cycle it stops responding to keyboard (KeyPress event never occur).
    Can anyone help me please

    Dont use Application.DoEvents it will basically a bug if you use it and you'll force some unnatural thing on your application which will hurt performance of your application!

    Try to use BacgroundWorker component:

    See this for working Example:

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=847014&SiteID=1

    I hope this will solve your problem!

    Best Regards,



  • SQLme

    That is a lot of overhead to communicate between the threads and the OP has to make his drawing interruptable so that the progresschanged event can be called shortly and returns otherwise the OPs code will get stuck in that event code which would not buy him anything because the background thread could not send further events. Depending on how that is implemented it might as well block as long as the called event handler has returned.

    It might work if the OP could create an in memory bitmap which he paints into and every now and then it sends an event to the UI thread to display that bitmap. But that still might be quite slow and you have to make up some points in your code where you call the event to have the UI updated.

    Lets see what the OP thinks of that, didn't respond that much, did he

    --
    SvenC


  • SankaraNarayanan Nagalingam

    Hi,

    I do not think that drawing can be done from the background thread as HWND and HDC have thread affinity and they can only be used from the thread they have been created from.

    I agree that the design of the app does not sound very good when it needs DoEvents. Maybe the OP can clarify why it needs constant drawing without ever leaving the drawing function.

    --
    SvenC


  • ac2600

    Hi,

    call DoEvents in your loop that will process the message queue so your key press event can be handled.

    --
    SvenC


  • Problem with handling keyboard input