473,385 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem with windows forms and delegates

I have an OPC server (an application server).

I write this server using C++/CLI.

This server contain callbacks (delegates)


When the client connects to the server (enter the core of a callback function onconnectclient) I want to change the text of a text box!!!

onclientcallback is defined in a seperate class called Callbacks

In this class I have an attribute called mainform which is the main form of my application (called by application::run)

so I call in the function mainform->TextBox->Text = "text" (1)

and nothing happen (compilation succeds)

I debuged the application and when attending (1) it retrun to the top of the function onclientcallback

I don't understand the problem, should I for example declare the delegate in the initialise componenet and if yes how and why??

Please help or give me any idea!
Apr 25 '08 #1
5 2417
Well I understand that the problem is a cross-thread exception


So, please tell me how can I fix this problem without changing the structure of my application.
Apr 25 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
Are you able to step through the code with the debugger?

I am curious about your comment of the "debugger returns to the top". If the debugger can't step through the code, the code was not compiled. Visual Studio will do that if there is no logical path to the code.
Apr 25 '08 #3
In my case the debugger can't step into the code when it attends the statement where the callback(that I defined) try to change the text of the label.

Now I understand more the problem, in winforms we cannot change the controls from an extern process(extern to the one that created the control)
==>an exeption is raised but don't don't block the execution, but the debugger return to the top of the callback

I find a solution using control::invoke()
Apr 30 '08 #4
pootle
68
In my case the debugger can't step into the code when it attends the statement where the callback(that I defined) try to change the text of the label.

Now I understand more the problem, in winforms we cannot change the controls from an extern process(extern to the one that created the control)
==>an exeption is raised but don't don't block the execution, but the debugger return to the top of the callback

I find a solution using control::invoke()
Hi Nabil,

You are right. In C++/CLI or in any other .NET language for that matter, you cannot change WinForms properties or members(e.g. via accessors) outside of the main gui thread. This is to prevent problems where two threads are modifying data concurrently.

However, I urge not to use Invoke. Please consider using BeginInvoke instead. You can find a lot of articles on the internet about Invoke vs. BeginInvoke.

Normally you would do something like:
Expand|Select|Wrap|Line Numbers
  1. delegate void InvokeDelegate();
  2. void MyForm::onAnEvent() {
  3.       if (InvokeRequired) {
  4.          BeginInvoke(gcnew InvokeDelegate(this, &MyForm::onAnEvent));      
  5.       }
  6.       else {
  7.          // Do your code here in the GUI thread
  8.       }
  9. }
  10.  
The whole "InvokeRequired" thing is a bit of a pain, but eventually you avoid problems such as the one you describe.

As an extra point, do you have CLR exceptions enabled in your debugger? If not, you will probably miss the problem altogether and end up at some totally unrealted part of the code.

HTH
Apr 30 '08 #5
Hi pootle,

thx for your clear ideas.


but I don't understand the last part of your reply, anyway I have CLR exceptions enabled in my debugger.
May 2 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: James | last post by:
In my code I have a problem in abtaining a windows handle. I do not know the namespace to obtain the windows hanle. here is the code. using System; using System.Collections; using...
1
by: tim cummings via .NET 247 | last post by:
I am writing a program that interfaces with a cars pcm via theserial port and recieves data (such as rpm) at an interval, oflets say 1 second, and then formats it. The problem is, I wouldlike to...
13
by: Rob Agar | last post by:
hi all, I have a problem which I believe others have experienced before, but as far as I can see has not been resolved. Basically, the form refuses to close. Neither clicking the 'X' button...
10
by: Opa | last post by:
I have tried for two days to solve this problem with no luck. I have a singleton class which has some events declared. When I inherit from this class the events don't seem to come along with it....
3
by: Rich | last post by:
Hello, The following Delegates example works with Option Strict Off, but if I change it to Option Strict On then VB.Net complains about the line dgY = System.Delegate.Combine(dgI, dgA) Is...
5
by: Paul Bromley | last post by:
For some time now I have been struggling trying to understand how to handle events originating in a User Control that I have designed when using this in an application. Basically I need to respond...
6
by: Joel | last post by:
2 Questions: (1) The documentation says application.run() creates a standard message loop on the current thread and "optionally" shows a form. This is really confusing because I was of the...
16
by: edepperson | last post by:
I've been looking for an answer to this problem and have seen the same question posted on other forums... but still no answer. My background is Delphi2 - 7 and I used to do this all the time with...
7
by: raylopez99 | last post by:
I find that I am using bool variables a lot when I code in Forms. I know how to overload event handlers, and that's great for offloading code from the 'base' event handler and/or creating helper...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.