Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
how to use backgroundworker thread call the API GetWindowRect?
can anybody give me a demo,tanks~!

C#
[DllImport("user32.dll")]
    private static extern int GetWindowRect(int hwnd, ref Rect lpRect);

    private void bgw_DoWork(object sender, DoWorkEventArgs e)
    {

        while (bgw.WorkerSupportsCancellation)
        {
            GetWindowRect(hWnd,ref lpRect);
        }


    }


the question importance is not at define hWnd and lpRect,
i just give a demo.
the API GetWindowRect must based on GUI thread,but backgroundworker is another thread not based on GUI thread.
how can solve this quetion, can anybody compilation right.
even though i use System.Windows.Forms.Control.Invoke method,
but the method only have two overload,one is not have params,
another's params is object array.
how can i use ref params in object array?
Posted
Updated 28-Sep-13 4:44am
v7
Comments
[no name] 28-Sep-13 7:44am    
You need to P/Invoke it then call it like http://msdn.microsoft.com/en-us/library/windows/desktop/ms633519%28v=vs.85%29.aspx
xuyunhai 28-Sep-13 8:48am    
no,can't solve this question.
BillWoodruff 28-Sep-13 9:10am    
Why not ?
[no name] 28-Sep-13 9:46am    
Well why can't you? We have no idea why you are unable to write some code or do some research or what you have tried to do for your self. You will never get anywhere unless you actually try to do it. Your "questions" show a definite lack of any kind of effort on your part. Do you expect us to spoon feed you?
xuyunhai 28-Sep-13 10:34am    
maybe the question to you is very simple,but i search in google,but couldn't slove,so ask the quesion in here.

I "guess" your problem can be solved by simply refactoring the code.
1. By the code given, the two parameter must be defined somewhere, in your case, are they defined in class.
2. You can simply write a function with no parameter to call the GetWindowRect, and call that function in your background worker through control.invoke or dispatcher.invoke. Syntactically, compiler should not complain.
3. I am not sure what you are trying to achieve by calling the getWindowRect this way? Background worker should be used to do some asynchronous call (triggered by network event or continuously working on something). SO you want to monitor your windows size continuously? You can use the windows size change event (check MSDN for exact event name).
4. Good luck.
 
Share this answer
 
Comments
xuyunhai 28-Sep-13 20:58pm    
1. the GetWindowRect is Mircosoft API,i couldn't change the parameter.
2. even if i write a function with no parameter to call the GetWindowRect,but the GetWindowRect API is based on GUI thread,so when i run the it,will throw error.
3.maybe i don't need to monitor windows size continuously,just once.
xuyunhai 28-Sep-13 21:53pm    
tanks for
1. By the code given, the two parameter must be defined somewhere, in your case, are they defined in class.
C#
[DllImport("user32.dll")]
private static extern int GetWindowRect(int hwnd, ref System.Drawing.Rectangle lpRect);


System.Drawing.Rectangle lpRect = new Rectangle();
GetWindowRect(hWnd,ref lpRect);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900