0

I have currently been assigned a project that was implemented couple of years ago in C#. It consists of frames and some excel-files extraction, but the problem is that this project is extremly broken/outdated and I have to change some functionalities and add some new feature.

There are some libraries that are supported only after .NET framework 3.0 that I need to use in the new features,and the framework that the current project was built on is 2.0 or less,so changing the framework raised some thread safety problems.

Between two partial classes for example which are the design file code and the backend code trying to change one label's value raises an error saying that I can not edit something that is being used at the moment... I used delegates for one of the labels, and it worked fine..then the other textbox raised another error.. So is there an option to encapsulate the whole thing and apply thread safety to the whole code without having to follow each line where the error happens?


Edit from comments

Exception:

Exception while Saving Links. Exception: System.InvalidOperationException: Cross-thread operation not valid: Control 'picLinkUBKCross' accessed from a thread other than the thread it was created on.

Code who seams to fail

this.picLinkUBKCross.Image = ((System.Drawing.Image) resources.GetObject("picLinkUBKCross.Image"))); 
this.picLinkUBKCross.Location = new System.Drawing.Point(367, 57);
this.picLinkUBKCross.Name = "picLinkUBKCross"; 
this.picLinkUBKCross.Size = new System.Drawing.Size(56, 35); 
this.picLinkUBKCross.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.picLinkUBKCross.TabIndex = 14;
this.picLinkUBKCross.TabStop = false; 
Noel
  • 10,152
  • 30
  • 45
  • 67
eMizo
  • 269
  • 1
  • 6
  • 16
  • what kind of project is it, winforms? post at least part of the code which raises an exception along with the exception's details. – Ventsyslav Raikov Sep 12 '13 at 07:16
  • post down error, else it would be difficult to understand the real problem. – Digital Alchemist Sep 12 '13 at 07:18
  • Yes it is winforms. `Exception while Saving Links. Exception: System.InvalidOperationException: Cross-thread operation not valid: Control 'picLinkUBKCross' accessed from a thread other than the thread it was created on.` – eMizo Sep 12 '13 at 07:22
  • `this.picLinkUBKCross.Image = ((System.Drawing.Image)(resources.GetObject("picLinkUBKCross.Image"))); this.picLinkUBKCross.Location = new System.Drawing.Point(367, 57); this.picLinkUBKCross.Name = "picLinkUBKCross"; this.picLinkUBKCross.Size = new System.Drawing.Size(56, 35); this.picLinkUBKCross.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; this.picLinkUBKCross.TabIndex = 14; this.picLinkUBKCross.TabStop = false;` – eMizo Sep 12 '13 at 07:26

2 Answers2

1

If your project contains errors in it's multi-thrading code that with the new Framework and Thread-handling advance from weird glitches to actual exceptions (which is a good thing) there is nothing you can do but tackle these errors one after another. There is no magic to fix all buggy code with one silver bullet.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • `public void SetLabel(string p_sNote) { if (lblLoadStatus.InvokeRequired) { lblLoadStatus.BeginInvoke(new MethodInvoker(delegate { SetLabel(p_sNote); })); } else { lblLoadStatus.Text = p_sNote; } }` This is one of the fixed lines that works properly now, so I have to apply the same thing to all other lines that are causing problems? – eMizo Sep 12 '13 at 07:29
  • Apart from obvious changes for the referenced controls in each case, yes. Basically, each non-UI thread function needs to check if an invoke is required and if it is, invoke. – nvoigt Sep 12 '13 at 08:05
1

So if you have switched the framework i think you also switched your Visual Studio version?

Within VS you can configure if you like to see the Cross-Thread exception and i think in your old version this hint was simply switched off. So all your problems already exists in your old code but now you see it again.

If you try to set (and sometimes even get) some information from a gui control you must ensure that you do this within the gui thread and not from any other. The problem is that it could work, but that can't be ensured (and maybe only crashs on the customer machine).

So please consider these exception as a real problem within your tool that must be fixed as soon as possible.

Either you go within your gui thread and call WindowsFormsSynchronizationContext.Current; and pass the received object to your other threads so that they can call Post() or Send() on them or if you have direct access to a control you simply call control.Invoke() or control.BeginInvoke() on it.

Maybe you should also take a look at this SO question.

Community
  • 1
  • 1
Oliver
  • 43,366
  • 8
  • 94
  • 151