Re: Thread-Problem

From: Ronald Siegel (r.siegel_at_gmx.net)
Date: 05/14/04


Date: Fri, 14 May 2004 12:05:24 +0200

Hallo,

"Thomas Scheidegger [MVP]" <spam.netmaster@swissonline.ch> schrieb

> Hallo Ronald
>
>> ich verstehe nicht, warum ich im nachfolgenden Beispiel ich
wahrscheinllich
>> in der Endlosschleife hängen bleibe
>
> Threading hat noch andere Bedingungen, als
> man so allgemein von solchem Code 'logisch' erwarten würde...
>
>> while(ar){ }
>
> eine solche Endlos-Schleife ist im Prinzip
> schon nicht so toll.
> (aber nicht das Problem)

Folgendes hatte ich mir vorgestellt: In einem Thread wird ein Excel-Makro
ausgeführt, in einem anderen überprüft, ob die Verbindung zum Client noch
besteht und der andere Thread noch am Laufen ist. Die While-Schleife habe
ich nur als "Bremse", damit nachfolgender Code erst ausgeführt wird, wenn
ich weiss, ob Excel-Makro beendet oder Verbindung zum Client unterbrochen
ist.
Für einen Tipp, wie ich es besser (eleganter) gestalten könnte, wäre ich
dankbar.

>
> Das Problem ist die 'ar' Variable,
> Die vom JITer innerhalb der 'while' -Schlaufe
> zur Optimierung mehr oder weniger 'ignoriert' wird.
>
> Ändere einmal:
> protected bool ar;
> in:
> protected volatile bool ar;

Ja, danke, funktioniert.

> Bitte lies die MSDN zum 'volatile' keyword,
> und ggf. ein gutes Buch über Multithreading-Grundlagen.

Welches ist denn empfehlenswert? In den Büchern, die ich zu c# habe, steht
lediglich im Buch von "Moses/Nowak:C# Programmieren unter .NET" etwas.
Allerdings auf volatile wird dort auch nicht hingewiesen. Ansonsten sieht es
wie pures "Java" aus und daran hatte ich mich orientiert.
[...]

Vielen Dank.
Grüße
Ronald



Relevant Pages

  • Re: fire event & IUnknown
    ... Is your client single-threaded or multi-threaded? ... The client project consists of one ATL ActiveX ... the threading model is causing the event to fail. ... > - I do need to fire events from within worker threads; ...
    (microsoft.public.vc.atl)
  • Re: .NET 2.0 Remoting Bug?
    ... I am not entirely aware of all the remoting interals and threading issues, ... (identification only, I don't want to impersonate the client), and I ... Apparently Thread.CurrentPrincipal is set to a GenericEntity type ...
    (microsoft.public.dotnet.security)
  • RE: Maintaining State Between Web Pages
    ... The srever runs on the client machine. ... I used Apartment threading model. ... so creating multiple instances results in launching multiple ... You must protect instance data as well as global memory. ...
    (microsoft.public.scripting.vbscript)
  • Re: Threading in C#.Net Web application
    ... web server vs. clients browser. ... I have seen some "successful" implementation of threading in web ... mix of client and server side script, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Need Mechanism or code for wait state in asp.net web application
    ... You don't want to go anywhere near the threading classes for this one, ... You need to perform actions on the client, ... >to display user a cursor and also put application in wait state that user ... I want to add wait cursor code whenever page is post back. ...
    (microsoft.public.dotnet.framework.aspnet)